-
-
-
-
-
-
-
-
-
+
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