diff --git a/src/app/app.component.html b/src/app/app.component.html index 12c3660..481dd91 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,3 +1,6 @@ -

App HTML

+

Ratatouille

- +
+ + +
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index dba27a1..ac799f8 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,16 +1,20 @@ import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; +import { RecipeFormComponent } from './recipe-form/recipe-form.component'; import { RecipeService } from './services/recipe.service'; -import { Recipe } from './model/recipe'; +import { Recipe } from './model/recipe.model'; import { RecipeListComponent } from './components/recipe-list/recipe-list.component'; @Component({ selector: 'app-root', standalone: true, - imports: [RouterOutlet,RecipeListComponent], + imports: [ + RouterOutlet, + RecipeFormComponent, + RecipeListComponent, + ], providers: [RecipeService], templateUrl: './app.component.html', - styleUrl: './app.component.css' }) export class AppComponent { title = 'bromista-nisqa-receta'; diff --git a/src/app/components/recipe-list/recipe-list.component.ts b/src/app/components/recipe-list/recipe-list.component.ts index 48465e0..e1cbb57 100644 --- a/src/app/components/recipe-list/recipe-list.component.ts +++ b/src/app/components/recipe-list/recipe-list.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { Recipe } from '../../model/recipe'; +import { Recipe } from '../../model/recipe.model'; import { RecipeService } from '../../services/recipe.service'; import { NgFor } from '@angular/common'; import { RecipeMiniComponent } from '../recipe-mini/recipe-mini.component'; @@ -17,7 +17,7 @@ export class RecipeListComponent { constructor(protected recipeService: RecipeService){} ngOnInit(){ - this.recipes = this.recipeService.getAll(); + this.recipes = this.recipeService.getRecipes(); } diff --git a/src/app/components/recipe-mini/recipe-mini.component.ts b/src/app/components/recipe-mini/recipe-mini.component.ts index 8e1f1eb..09aa671 100644 --- a/src/app/components/recipe-mini/recipe-mini.component.ts +++ b/src/app/components/recipe-mini/recipe-mini.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { Recipe } from '../../model/recipe'; +import { Recipe } from '../../model/recipe.model'; import { Input } from '@angular/core'; diff --git a/src/app/model/ingredient.ts b/src/app/model/ingredient.model.ts similarity index 100% rename from src/app/model/ingredient.ts rename to src/app/model/ingredient.model.ts diff --git a/src/app/model/recipe.ts b/src/app/model/recipe.model.ts similarity index 72% rename from src/app/model/recipe.ts rename to src/app/model/recipe.model.ts index e3bbdba..a86d255 100644 --- a/src/app/model/recipe.ts +++ b/src/app/model/recipe.model.ts @@ -1,4 +1,4 @@ -import { Ingredient } from "./ingredient"; +import { Ingredient } from "./ingredient.model"; export interface Recipe { id: number; diff --git a/src/app/app.component.css b/src/app/recipe-form/recipe-form.component.css similarity index 100% rename from src/app/app.component.css rename to src/app/recipe-form/recipe-form.component.css diff --git a/src/app/recipe-form/recipe-form.component.html b/src/app/recipe-form/recipe-form.component.html new file mode 100644 index 0000000..2badad1 --- /dev/null +++ b/src/app/recipe-form/recipe-form.component.html @@ -0,0 +1,32 @@ +

New Recipe

+
+ + +
+ + + +
+ + + +
+ +
+

Ingredients

+ +
+ + + + + +
+
+
+ + +
\ No newline at end of file diff --git a/src/app/recipe-form/recipe-form.component.spec.ts b/src/app/recipe-form/recipe-form.component.spec.ts new file mode 100644 index 0000000..acc4e95 --- /dev/null +++ b/src/app/recipe-form/recipe-form.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { RecipeFormComponent } from './recipe-form.component'; + +describe('RecipeFormComponent', () => { + let component: RecipeFormComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [RecipeFormComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(RecipeFormComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/recipe-form/recipe-form.component.ts b/src/app/recipe-form/recipe-form.component.ts new file mode 100644 index 0000000..9adf8d5 --- /dev/null +++ b/src/app/recipe-form/recipe-form.component.ts @@ -0,0 +1,83 @@ +import { Component } from '@angular/core'; +import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { Recipe } from '../model/recipe.model'; +import { Ingredient } from '../model/ingredient.model'; +import { CommonModule } from '@angular/common'; +import { ReactiveFormsModule } from '@angular/forms'; +import { RecipeService } from '../services/recipe.service'; + +@Component({ + selector: 'app-recipe-form', + standalone: true, + imports: [ + CommonModule, + ReactiveFormsModule + ], + templateUrl: './recipe-form.component.html', + styleUrls: ['./recipe-form.component.css'] +}) + +export class RecipeFormComponent { + recipeForm: FormGroup; + base64Image: string | ArrayBuffer | null = null; + ingredientsOptions = ['Champignon', 'Tomata', 'Mozarella']; + defaultOption: string = this.ingredientsOptions[2]; + + constructor(private fb: FormBuilder, private recipeService: RecipeService) { + this.recipeForm = this.fb.group({ + name: ['', Validators.required], + description: ['', Validators.required], + image: ['', Validators.required], + ingredients: this.fb.array([]), + }); + } + + get ingredients(): FormArray { + return this.recipeForm.get('ingredients') as FormArray; + } + + addIngredient(): void { + this.ingredients.push(this.fb.group({ + name: ['', Validators.required], + qty: ['', Validators.required] + })); + } + + removeIngredient(index: number): void { + this.ingredients.removeAt(index); + } + + onFileChange(event: Event) { + const input = event.target as HTMLInputElement; + if (input.files && input.files[0]) { + const file = input.files[0]; + const reader = new FileReader(); + reader.onload = () => { + this.base64Image = reader.result; + }; + reader.readAsDataURL(file); + } + } + + onSubmit(): void { + if (this.recipeForm.valid) { + const newRecipe: Recipe = { + id: 0, + name: this.recipeForm.value.name, + description: this.recipeForm.value.description, + image: this.base64Image?.toString() || "no-data", + ingredients: this.recipeForm.value.ingredients.map((ingredient: Ingredient, idx: number) => ({ + id: idx, + name: ingredient.name, + qty: ingredient.qty, + })) + }; + console.log('Recipe added:', newRecipe); + this.recipeService.addRecipe(newRecipe); + this.recipeForm.reset(); + } else { + console.log('Form is invalid'); + } + } +} + diff --git a/src/app/services/recipe.service.ts b/src/app/services/recipe.service.ts index 4819ce9..ee27432 100644 --- a/src/app/services/recipe.service.ts +++ b/src/app/services/recipe.service.ts @@ -1,20 +1,31 @@ -import { Recipe } from "../model/recipe"; -import { RECIPES } from "../datas/recipe.stub"; -import { Injectable } from "@angular/core"; +import { Injectable } from '@angular/core'; +import { Recipe } from '../model/recipe.model'; -@Injectable() +@Injectable({ + providedIn: 'root' +}) export class RecipeService { - private recipes: Recipe[]; + private localStorageKey = 'recipes'; + private recipes: Recipe[] = []; - constructor(){ - this.recipes = RECIPES; - } + constructor() { } - getAll(): Recipe[]{ - return this.recipes; - } + // Get recipes from local storage + getRecipes(): Recipe[] { + const recipesJson = localStorage.getItem(this.localStorageKey) || "[]"; + this.recipes = JSON.parse(recipesJson) || []; + return this.recipes; + } - addRecipe(recipe: Recipe): void { - this.recipes.push(recipe); - } + // Add a new recipe + addRecipe(recipe: Recipe): void { + this.getRecipes(); + this.recipes.push(recipe); + localStorage.setItem(this.localStorageKey, JSON.stringify(this.recipes)); + } + + // Clear all recipes (for example, if needed) + clearRecipes(): void { + localStorage.removeItem(this.localStorageKey); + } }