| @@ -1,6 +1,6 @@ | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <form> | |||
| <form (ngSubmit)="onSubmit(f)" #f="ngForm"> | |||
| <div class="row"> | |||
| <div class="col-sm-5 form-group"> | |||
| <label for="name">Name</label> | |||
| @@ -8,7 +8,9 @@ | |||
| type="text" | |||
| id="name" | |||
| class="form-control" | |||
| #nameInput> | |||
| name="name" | |||
| ngModel | |||
| required> | |||
| </div> | |||
| <div class="col-sm-2 form-group"> | |||
| <label for="amount">Amount</label> | |||
| @@ -16,14 +18,17 @@ | |||
| type="number" | |||
| id="amount" | |||
| class="form-control" | |||
| #amountInput> | |||
| name="amount" | |||
| ngModel | |||
| required | |||
| pattern="^[1-9]+[0-9]*$"> | |||
| </div> | |||
| </div> | |||
| <div class="row"> | |||
| <div class="col-xs-12"> | |||
| <button class="btn btn-success" type="submit" (click)="onAddItem()">Add</button> | |||
| <button class="btn btn-danger" type="button">Delete</button> | |||
| <button class="btn btn-primary" type="button">Clear</button> | |||
| <button class="btn btn-success" type="submit" [disabled]="!f.valid">{{ editMode ? 'Update' : 'Add' }}</button> | |||
| <button *ngIf="editMode" class="btn btn-danger" type="button" (click)="onDelete()">Delete</button> | |||
| <button class="btn btn-primary" type="button" (click)="onClear()">Clear</button> | |||
| </div> | |||
| </div> | |||
| </form> | |||
| @@ -1,4 +1,7 @@ | |||
| import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; | |||
| import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; | |||
| import { NgForm } from '@angular/forms'; | |||
| import { Subscription } from 'rxjs/Subscription'; | |||
| import { Ingredient } from '../../shared/ingredient.model'; | |||
| import { ShoppingListService } from '../shopping-list.service'; | |||
| @@ -7,18 +10,52 @@ import { ShoppingListService } from '../shopping-list.service'; | |||
| templateUrl: './shopping-edit.component.html', | |||
| styleUrls: ['./shopping-edit.component.css'] | |||
| }) | |||
| export class ShoppingEditComponent implements OnInit { | |||
| @ViewChild('nameInput') nameInputRef: ElementRef; | |||
| @ViewChild('amountInput') amountInputRef: ElementRef; | |||
| export class ShoppingEditComponent implements OnInit, OnDestroy { | |||
| @ViewChild('f') shoppingListForm: NgForm; | |||
| subscription: Subscription | |||
| editMode = false; | |||
| editedItemIndex: number; | |||
| editedItem: Ingredient; | |||
| constructor(private shoppingListService: ShoppingListService) { } | |||
| ngOnInit() { | |||
| this.subscription = this.shoppingListService.startedEditing.subscribe( | |||
| (index: number) => { | |||
| this.editedItemIndex = index; | |||
| this.editMode = true; | |||
| this.editedItem = this.shoppingListService.getIngredient(index); | |||
| this.shoppingListForm.setValue({ | |||
| name: this.editedItem.name, | |||
| amount: this.editedItem.amount | |||
| }) | |||
| } | |||
| ); | |||
| } | |||
| onSubmit(form: NgForm) { | |||
| const value = form.value | |||
| const newIngredient = new Ingredient(value.name, value.amount); | |||
| if (this.editMode) { | |||
| this.shoppingListService.updateIngredient(this.editedItemIndex, newIngredient); | |||
| } else { | |||
| this.shoppingListService.addIngredient(newIngredient); | |||
| } | |||
| this.editMode = false; | |||
| form.reset(); | |||
| } | |||
| onClear() { | |||
| this.shoppingListForm.reset(); | |||
| this.editMode = false; | |||
| } | |||
| onDelete() { | |||
| this.shoppingListService.deleteIngredient(this.editedItemIndex); | |||
| this.onClear(); | |||
| } | |||
| onAddItem() { | |||
| const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value, | |||
| this.amountInputRef.nativeElement.value); | |||
| this.shoppingListService.addIngredient(newIngredient); | |||
| ngOnDestroy() { | |||
| this.subscription.unsubscribe(); | |||
| } | |||
| } | |||
| @@ -6,7 +6,8 @@ | |||
| <a | |||
| class="list-group-item" | |||
| style="cursor: pointer;" | |||
| *ngFor="let ingredient of ingredients" | |||
| *ngFor="let ingredient of ingredients; let i = index" | |||
| (click)="onEditItem(i)" | |||
| > | |||
| {{ ingredient.name }} ({{ ingredient.amount }}) | |||
| </a> | |||
| @@ -24,6 +24,10 @@ export class ShoppingListComponent implements OnInit, OnDestroy { | |||
| ); | |||
| } | |||
| onEditItem(index: number) { | |||
| this.shoppingListService.startedEditing.next(index); | |||
| } | |||
| ngOnDestroy() { | |||
| this.subscription.unsubscribe(); | |||
| } | |||
| @@ -3,6 +3,7 @@ import { Subject } from "rxjs/Subject"; | |||
| export class ShoppingListService { | |||
| ingredientsChanged = new Subject<Ingredient[]>(); | |||
| startedEditing = new Subject<number>(); | |||
| private ingredients: Ingredient[] = [ | |||
| new Ingredient('Apples', 5), | |||
| new Ingredient('Tomatoes', 10) | |||
| @@ -11,6 +12,10 @@ export class ShoppingListService { | |||
| getIngredients() { | |||
| return this.ingredients.slice(); | |||
| } | |||
| getIngredient(index: number) { | |||
| return this.ingredients[index]; | |||
| } | |||
| addIngredient(ingredient: Ingredient) { | |||
| this.ingredients.push(ingredient); | |||
| @@ -21,4 +26,14 @@ export class ShoppingListService { | |||
| this.ingredients.push(...ingredients); | |||
| this.ingredientsChanged.next(this.ingredients.slice()); | |||
| } | |||
| updateIngredient(index: number, newIngredient: Ingredient) { | |||
| this.ingredients[index] = newIngredient; | |||
| this.ingredientsChanged.next(this.ingredients.slice()); | |||
| } | |||
| deleteIngredient(index: number) { | |||
| this.ingredients.splice(index, 1); | |||
| this.ingredientsChanged.next(this.ingredients.slice()); | |||
| } | |||
| } | |||