-
diff --git a/src/app/shopping-list/shopping-edit/shopping-edit.component.ts b/src/app/shopping-list/shopping-edit/shopping-edit.component.ts
index 8c42eca..645bb70 100644
--- a/src/app/shopping-list/shopping-edit/shopping-edit.component.ts
+++ b/src/app/shopping-list/shopping-edit/shopping-edit.component.ts
@@ -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();
}
}
diff --git a/src/app/shopping-list/shopping-list.component.html b/src/app/shopping-list/shopping-list.component.html
index 78ab2f9..ed3016e 100644
--- a/src/app/shopping-list/shopping-list.component.html
+++ b/src/app/shopping-list/shopping-list.component.html
@@ -6,7 +6,8 @@
{{ ingredient.name }} ({{ ingredient.amount }})
diff --git a/src/app/shopping-list/shopping-list.component.ts b/src/app/shopping-list/shopping-list.component.ts
index 11a5599..e6d6052 100644
--- a/src/app/shopping-list/shopping-list.component.ts
+++ b/src/app/shopping-list/shopping-list.component.ts
@@ -24,6 +24,10 @@ export class ShoppingListComponent implements OnInit, OnDestroy {
);
}
+ onEditItem(index: number) {
+ this.shoppingListService.startedEditing.next(index);
+ }
+
ngOnDestroy() {
this.subscription.unsubscribe();
}
diff --git a/src/app/shopping-list/shopping-list.service.ts b/src/app/shopping-list/shopping-list.service.ts
index c30a2f6..935899b 100644
--- a/src/app/shopping-list/shopping-list.service.ts
+++ b/src/app/shopping-list/shopping-list.service.ts
@@ -3,6 +3,7 @@ import { Subject } from "rxjs/Subject";
export class ShoppingListService {
ingredientsChanged = new Subject
();
+ startedEditing = new Subject();
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());
+ }
}
\ No newline at end of file