From ba064d00fb6523281b8920a24297b7d1bae2e87c Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Mon, 17 Jun 2024 13:56:35 +0200 Subject: [PATCH 1/2] Add and edit recipes --- src/app/app.config.ts | 5 +- src/app/app.routes.ts | 1 + src/app/recipe-add/recipe-add.component.html | 26 ++++++- src/app/recipe-add/recipe-add.component.ts | 72 +++++++++++++++++++- src/app/recipe.service.ts | 16 +++++ 5 files changed, 114 insertions(+), 6 deletions(-) diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 3be7a8a..e3dd5e0 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,10 +1,9 @@ -import { ApplicationConfig } from '@angular/core'; -import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; +import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; import { provideRouter } from '@angular/router'; import { withComponentInputBinding } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { - providers: [provideRouter(routes, withComponentInputBinding()), provideAnimationsAsync()], + providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes, withComponentInputBinding())], }; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index be6840b..775fbbb 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -7,4 +7,5 @@ export const routes: Routes = [ { path: 'recipes', component: RecipesComponent }, { path: 'recipe/add', component: RecipeAddComponent }, { path: 'recipe/:id', component: RecipeComponent }, + { path: 'recipe/:id/edit', component: RecipeAddComponent }, ]; diff --git a/src/app/recipe-add/recipe-add.component.html b/src/app/recipe-add/recipe-add.component.html index c7e1b36..9123e3d 100644 --- a/src/app/recipe-add/recipe-add.component.html +++ b/src/app/recipe-add/recipe-add.component.html @@ -1 +1,25 @@ -

recipe-add works!

+
+
+ + +
+
+ + +
+
+ +
    + @for (ingredient of ingredientEntries; track ingredient.idIngredient) { +
  • #{{ ingredient.idIngredient }}
  • + } +
+ + +
+ +
diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts index 0ca445c..96f9f61 100644 --- a/src/app/recipe-add/recipe-add.component.ts +++ b/src/app/recipe-add/recipe-add.component.ts @@ -1,10 +1,78 @@ -import { Component } from '@angular/core'; +import { Component, Input } from '@angular/core'; +import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; +import { Ingredient, IngredientEntry, Recipe } from '../../cookbook/type'; +import { RecipeService } from '../recipe.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-recipe-add', standalone: true, - imports: [], + imports: [ReactiveFormsModule, FormsModule], templateUrl: './recipe-add.component.html', }) export class RecipeAddComponent { + + createForm = this.formBuilder.group({ + name: ['', Validators.maxLength(256)], + description: ['', Validators.maxLength(512)], + selectedIngredient: '', + }); + + ingredientEntries: IngredientEntry[] = []; + ingredients: Ingredient[] = [{ + id: 1, + name: 'Paprika', + }]; + + #recipeId: number = -1; + @Input() + set recipeId(recipeId: string) { + if (recipeId === undefined) return; + this.#recipeId = parseInt(recipeId); + const recipe = this.recipes.get(this.#recipeId); + if (recipe === null) { + this.router.navigateByUrl('404'); + return; + } + this.createForm.patchValue({ + name: recipe.name, + description: recipe.description, + }) + } + + constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {} + + onSubmit(): void { + if (this.createForm.invalid) { + return; + } + const value = this.createForm.value; + const partial: Omit = { + name: value.name!, + description: value.description!, + image: '', + ingredients: this.ingredientEntries, + }; + if (this.#recipeId === -1) { + this.recipes.add(partial); + } else { + this.recipes.edit({ id: this.#recipeId, ...partial }); + } + } + + onAddIngredient(): void { + const value = this.createForm.value; + if (!value.selectedIngredient) { + return; + } + const id = parseInt(value.selectedIngredient!); + if (this.ingredientEntries.find((ingredient) => ingredient.idIngredient === id)) { + return; + } + this.ingredientEntries.push({ + idIngredient: id, + idRecipe: -1, + quantity: 1, + }) + } } diff --git a/src/app/recipe.service.ts b/src/app/recipe.service.ts index 2fe2052..3a0bdd9 100644 --- a/src/app/recipe.service.ts +++ b/src/app/recipe.service.ts @@ -29,4 +29,20 @@ export class RecipeService { get(id: number): Recipe | null { return this.#recipes.find((recipe) => recipe.id === id) || null; } + + add(recipe: Omit): void { + const id = this.#recipes.length ? Math.max(...this.#recipes.map((recipe) => recipe.id)) + 1 : 1; + this.#recipes.push({ + id, + ...recipe, + }); + } + + edit(recipe: Recipe): void { + for (let i = 0; i < this.#recipes.length; ++i) { + if (this.#recipes[i].id === recipe.id) { + this.#recipes[i] = recipe; + } + } + } } -- 2.36.3 From 60379fc2e1f371335e9691c8633fd993396272d0 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Mon, 17 Jun 2024 13:59:10 +0200 Subject: [PATCH 2/2] Format --- src/app/app.config.ts | 5 ++++- src/app/recipe-add/recipe-add.component.ts | 7 +++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/app/app.config.ts b/src/app/app.config.ts index e3dd5e0..751863a 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -5,5 +5,8 @@ import { withComponentInputBinding } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { - providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes, withComponentInputBinding())], + providers: [ + provideZoneChangeDetection({ eventCoalescing: true }), + provideRouter(routes, withComponentInputBinding()), + ], }; diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts index 96f9f61..5e52144 100644 --- a/src/app/recipe-add/recipe-add.component.ts +++ b/src/app/recipe-add/recipe-add.component.ts @@ -1,8 +1,8 @@ import { Component, Input } from '@angular/core'; import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; +import { Router } from '@angular/router'; import { Ingredient, IngredientEntry, Recipe } from '../../cookbook/type'; import { RecipeService } from '../recipe.service'; -import { Router } from '@angular/router'; @Component({ selector: 'app-recipe-add', @@ -11,7 +11,6 @@ import { Router } from '@angular/router'; templateUrl: './recipe-add.component.html', }) export class RecipeAddComponent { - createForm = this.formBuilder.group({ name: ['', Validators.maxLength(256)], description: ['', Validators.maxLength(512)], @@ -37,7 +36,7 @@ export class RecipeAddComponent { this.createForm.patchValue({ name: recipe.name, description: recipe.description, - }) + }); } constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {} @@ -73,6 +72,6 @@ export class RecipeAddComponent { idIngredient: id, idRecipe: -1, quantity: 1, - }) + }); } } -- 2.36.3