| @@ -0,0 +1,4 @@ | |||||
| input.ng-invalid.ng-touched, | |||||
| textare.ng-invalid.ng-touched { | |||||
| border: 1px solid red; | |||||
| } | |||||
| @@ -3,7 +3,7 @@ | |||||
| <form [formGroup]="recipeForm" (ngSubmit)="onSubmit()"> | <form [formGroup]="recipeForm" (ngSubmit)="onSubmit()"> | ||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-xs-12"> | <div class="col-xs-12"> | ||||
| <button type="submit" class="btn btn-success">Save</button> | |||||
| <button type="submit" class="btn btn-success" [disabled]="!recipeForm.valid">Save</button> | |||||
| <button type="button" class="btn btn-danger">Cancel</button> | <button type="button" class="btn btn-danger">Cancel</button> | ||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| @@ -56,22 +56,32 @@ | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| <div class="row"> | <div class="row"> | ||||
| <div class="col-xs-12"> | |||||
| <div class="form-group"> | |||||
| <div class="row"> | |||||
| <div class="col-xs-8"> | |||||
| <input | |||||
| type="text" | |||||
| class="form-control"> | |||||
| </div> | |||||
| <div class="col-xs-2"> | |||||
| <input | |||||
| type="text" | |||||
| class="form-control"> | |||||
| </div> | |||||
| <div class="col-xs-2"> | |||||
| <button class="btn btn-danger">x</button> | |||||
| </div> | |||||
| <div class="col-xs-12" formArrayName="ingredients"> | |||||
| <div | |||||
| class="row" | |||||
| *ngFor="let ingredientCtrl of recipeForm.get('ingredients').controls; let i = index" | |||||
| [formGroupName]="i" | |||||
| style="margin-top: 10px;"> | |||||
| <div class="col-xs-8"> | |||||
| <input | |||||
| type="text" | |||||
| class="form-control" | |||||
| formControlName="name"> | |||||
| </div> | |||||
| <div class="col-xs-2"> | |||||
| <input | |||||
| type="number" | |||||
| class="form-control" | |||||
| formControlName="amount"> | |||||
| </div> | |||||
| <div class="col-xs-2"> | |||||
| <button class="btn btn-danger">x</button> | |||||
| </div> | |||||
| </div> | |||||
| <hr> | |||||
| <div class="row"> | |||||
| <div class="col-xs-12"> | |||||
| <button type="button" class="btn btn-success" (click)="onAddIngredient()">Add Ingredient</button> | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| @@ -1,7 +1,8 @@ | |||||
| import { Component, OnInit } from '@angular/core'; | import { Component, OnInit } from '@angular/core'; | ||||
| import { ActivatedRoute, Params } from '@angular/router'; | 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 { RecipeService } from '../recipe.service'; | ||||
| import { Recipe } from '../recipe.model'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-recipe-edit', | selector: 'app-recipe-edit', | ||||
| @@ -27,25 +28,54 @@ export class RecipeEditComponent implements OnInit { | |||||
| } | } | ||||
| onSubmit() { | 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() { | |||||
| (<FormArray>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() { | private initForm() { | ||||
| let recipeName = ''; | let recipeName = ''; | ||||
| let recipeImagePath = ''; | let recipeImagePath = ''; | ||||
| let recipeDescription = ''; | let recipeDescription = ''; | ||||
| let recipeIngredients = new FormArray([]); | |||||
| if (this.editMode) { | if (this.editMode) { | ||||
| const recipe = this.recipeService.getRecipe(this.id); | const recipe = this.recipeService.getRecipe(this.id); | ||||
| recipeName = recipe.name; | recipeName = recipe.name; | ||||
| recipeImagePath = recipe.imagePath; | recipeImagePath = recipe.imagePath; | ||||
| recipeDescription = recipe.description; | 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({ | 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 | |||||
| }); | }); | ||||
| } | } | ||||
| } | } | ||||
| @@ -18,6 +18,11 @@ export class RecipeListComponent implements OnInit { | |||||
| private route: ActivatedRoute) { } | private route: ActivatedRoute) { } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.recipeService.recipesChanged.subscribe( | |||||
| (recipes: Recipe[]) => { | |||||
| this.recipes = recipes; | |||||
| } | |||||
| ); | |||||
| this.recipes = this.recipeService.getRecipes(); | this.recipes = this.recipeService.getRecipes(); | ||||
| } | } | ||||
| @@ -2,9 +2,11 @@ import { Recipe } from "./recipe.model"; | |||||
| import { EventEmitter, Injectable } from "@angular/core"; | import { EventEmitter, Injectable } from "@angular/core"; | ||||
| import { Ingredient } from "../shared/ingredient.model"; | import { Ingredient } from "../shared/ingredient.model"; | ||||
| import { ShoppingListService } from "../shopping-list/shopping-list.service"; | import { ShoppingListService } from "../shopping-list/shopping-list.service"; | ||||
| import { Subject } from "rxjs/Subject"; | |||||
| @Injectable() | @Injectable() | ||||
| export class RecipeService { | export class RecipeService { | ||||
| recipesChanged = new Subject<Recipe[]>(); | |||||
| private recipes: Recipe[] = [ | private recipes: Recipe[] = [ | ||||
| new Recipe( | new Recipe( | ||||
| 'A Test Recipe', | 'A Test Recipe', | ||||
| @@ -39,4 +41,14 @@ export class RecipeService { | |||||
| addIngredientsToShoppingList(ingredients: Ingredient[]) { | addIngredientsToShoppingList(ingredients: Ingredient[]) { | ||||
| this.shoppingListService.addIngredients(ingredients); | 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()); | |||||
| } | |||||
| } | } | ||||