From aa13a3f60083264d5a357f1d98da628e8443c26a Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Thu, 23 Nov 2017 14:00:57 +0100 Subject: [PATCH] 16-215 Adding and editing recipes --- .../recipes/recipe-edit/recipe-edit.component.css | 4 ++ .../recipes/recipe-edit/recipe-edit.component.html | 44 +++++++++++++--------- .../recipes/recipe-edit/recipe-edit.component.ts | 40 +++++++++++++++++--- .../recipes/recipe-list/recipe-list.component.ts | 5 +++ src/app/recipes/recipe.service.ts | 12 ++++++ 5 files changed, 83 insertions(+), 22 deletions(-) diff --git a/src/app/recipes/recipe-edit/recipe-edit.component.css b/src/app/recipes/recipe-edit/recipe-edit.component.css index e69de29..2ddacef 100644 --- a/src/app/recipes/recipe-edit/recipe-edit.component.css +++ b/src/app/recipes/recipe-edit/recipe-edit.component.css @@ -0,0 +1,4 @@ +input.ng-invalid.ng-touched, +textare.ng-invalid.ng-touched { + border: 1px solid red; +} \ No newline at end of file diff --git a/src/app/recipes/recipe-edit/recipe-edit.component.html b/src/app/recipes/recipe-edit/recipe-edit.component.html index 8540643..56ed7d9 100644 --- a/src/app/recipes/recipe-edit/recipe-edit.component.html +++ b/src/app/recipes/recipe-edit/recipe-edit.component.html @@ -3,7 +3,7 @@
- +
@@ -56,22 +56,32 @@
-
-
-
-
- -
-
- -
-
- -
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
diff --git a/src/app/recipes/recipe-edit/recipe-edit.component.ts b/src/app/recipes/recipe-edit/recipe-edit.component.ts index 093c1f3..7c2a506 100644 --- a/src/app/recipes/recipe-edit/recipe-edit.component.ts +++ b/src/app/recipes/recipe-edit/recipe-edit.component.ts @@ -1,7 +1,8 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Params } from '@angular/router'; -import { FormGroup, FormControl } from '@angular/forms'; +import { FormArray, FormGroup, FormControl, Validators } from '@angular/forms'; import { RecipeService } from '../recipe.service'; +import { Recipe } from '../recipe.model'; @Component({ selector: 'app-recipe-edit', @@ -27,25 +28,54 @@ export class RecipeEditComponent implements OnInit { } onSubmit() { - console.log(this.recipeForm); + /* const newRecipe = new Recipe(this.recipeForm.value['name'], + this.recipeForm.valid['description'], + this.recipeForm.valid['imagePath'], + this.recipeForm.value['ingredients']); */ + if (this.editMode) { + this.recipeService.updateRecpe(this.id, this.recipeForm.value); + } else { + this.recipeService.addRecipe(this.recipeForm.value); + } + } + + onAddIngredient() { + (this.recipeForm.get('ingredients')).push( + new FormGroup({ + 'name': new FormControl(null, Validators.required), + 'amount': new FormControl(null, [Validators.required, Validators.pattern(/^[1-9]+[0-9]*$/)]) + }) + ) } private initForm() { let recipeName = ''; let recipeImagePath = ''; let recipeDescription = ''; + let recipeIngredients = new FormArray([]); if (this.editMode) { const recipe = this.recipeService.getRecipe(this.id); recipeName = recipe.name; recipeImagePath = recipe.imagePath; recipeDescription = recipe.description; + if (recipe['ingredients']) { + for (let ingredient of recipe.ingredients) { + recipeIngredients.push( + new FormGroup({ + 'name': new FormControl(ingredient.name, Validators.required), + 'amount': new FormControl(ingredient.amount, [Validators.required, Validators.pattern(/^[1-9]+[0-9]*$/)]) + }) + ); + } + } } this.recipeForm = new FormGroup({ - 'name': new FormControl(recipeName), - 'imagePath': new FormControl(recipeImagePath), - 'description': new FormControl(recipeDescription) + 'name': new FormControl(recipeName, Validators.required), + 'imagePath': new FormControl(recipeImagePath, Validators.required), + 'description': new FormControl(recipeDescription, Validators.required), + 'ingredients': recipeIngredients }); } } diff --git a/src/app/recipes/recipe-list/recipe-list.component.ts b/src/app/recipes/recipe-list/recipe-list.component.ts index 8344e46..3652b0b 100644 --- a/src/app/recipes/recipe-list/recipe-list.component.ts +++ b/src/app/recipes/recipe-list/recipe-list.component.ts @@ -18,6 +18,11 @@ export class RecipeListComponent implements OnInit { private route: ActivatedRoute) { } ngOnInit() { + this.recipeService.recipesChanged.subscribe( + (recipes: Recipe[]) => { + this.recipes = recipes; + } + ); this.recipes = this.recipeService.getRecipes(); } diff --git a/src/app/recipes/recipe.service.ts b/src/app/recipes/recipe.service.ts index 9f02c1c..c5304a5 100644 --- a/src/app/recipes/recipe.service.ts +++ b/src/app/recipes/recipe.service.ts @@ -2,9 +2,11 @@ import { Recipe } from "./recipe.model"; import { EventEmitter, Injectable } from "@angular/core"; import { Ingredient } from "../shared/ingredient.model"; import { ShoppingListService } from "../shopping-list/shopping-list.service"; +import { Subject } from "rxjs/Subject"; @Injectable() export class RecipeService { + recipesChanged = new Subject(); private recipes: Recipe[] = [ new Recipe( 'A Test Recipe', @@ -39,4 +41,14 @@ export class RecipeService { addIngredientsToShoppingList(ingredients: Ingredient[]) { this.shoppingListService.addIngredients(ingredients); } + + addRecipe(recipe: Recipe) { + this.recipes.push(recipe); + this.recipesChanged.next(this.recipes.slice()); + } + + updateRecpe(index: number, newRecipe: Recipe) { + this.recipes[index] = newRecipe; + this.recipesChanged.next(this.recipes.slice()); + } } \ No newline at end of file