| @@ -6,7 +6,8 @@ export const ADD_INGREDIENT = 'ADD_INGREDIENT'; | |||||
| export class AddIngredient implements Action { | export class AddIngredient implements Action { | ||||
| readonly type = ADD_INGREDIENT; | readonly type = ADD_INGREDIENT; | ||||
| payload: Ingredient; | |||||
| constructor(public payload: Ingredient) {} | |||||
| } | } | ||||
| export type ShoppingListActions = AddIngredient; | export type ShoppingListActions = AddIngredient; | ||||
| @@ -1,9 +1,11 @@ | |||||
| import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; | import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; | ||||
| import { NgForm } from '@angular/forms'; | import { NgForm } from '@angular/forms'; | ||||
| import { Subscription } from 'rxjs/Subscription'; | import { Subscription } from 'rxjs/Subscription'; | ||||
| import { Store } from '@ngrx/store'; | |||||
| import { Ingredient } from '../../shared/ingredient.model'; | import { Ingredient } from '../../shared/ingredient.model'; | ||||
| import { ShoppingListService } from '../shopping-list.service'; | import { ShoppingListService } from '../shopping-list.service'; | ||||
| import * as ShoppingListActions from '../ngrx/shopping-list.actions'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-shopping-edit', | selector: 'app-shopping-edit', | ||||
| @@ -17,7 +19,8 @@ export class ShoppingEditComponent implements OnInit, OnDestroy { | |||||
| editedItemIndex: number; | editedItemIndex: number; | ||||
| editedItem: Ingredient; | editedItem: Ingredient; | ||||
| constructor(private shoppingListService: ShoppingListService) { } | |||||
| constructor(private shoppingListService: ShoppingListService, | |||||
| private store: Store<{shoppingList: {ingredients: Ingredient[]}}>) { } | |||||
| ngOnInit() { | ngOnInit() { | ||||
| this.subscription = this.shoppingListService.startedEditing.subscribe( | this.subscription = this.shoppingListService.startedEditing.subscribe( | ||||
| @@ -39,7 +42,7 @@ export class ShoppingEditComponent implements OnInit, OnDestroy { | |||||
| if (this.editMode) { | if (this.editMode) { | ||||
| this.shoppingListService.updateIngredient(this.editedItemIndex, newIngredient); | this.shoppingListService.updateIngredient(this.editedItemIndex, newIngredient); | ||||
| } else { | } else { | ||||
| this.shoppingListService.addIngredient(newIngredient); | |||||
| this.store.dispatch(new ShoppingListActions.AddIngredient(newIngredient)); | |||||
| } | } | ||||
| this.editMode = false; | this.editMode = false; | ||||
| form.reset(); | form.reset(); | ||||
| @@ -1,5 +1,4 @@ | |||||
| import { Component, OnInit, OnDestroy } from '@angular/core'; | |||||
| import { Subscription } from 'rxjs/Subscription'; | |||||
| import { Component, OnInit } from '@angular/core'; | |||||
| import { Store } from '@ngrx/store'; | import { Store } from '@ngrx/store'; | ||||
| import { Observable } from 'rxjs/Observable'; | import { Observable } from 'rxjs/Observable'; | ||||
| @@ -11,27 +10,17 @@ import { ShoppingListService } from './shopping-list.service'; | |||||
| templateUrl: './shopping-list.component.html', | templateUrl: './shopping-list.component.html', | ||||
| styleUrls: ['./shopping-list.component.css'] | styleUrls: ['./shopping-list.component.css'] | ||||
| }) | }) | ||||
| export class ShoppingListComponent implements OnInit, OnDestroy { | |||||
| export class ShoppingListComponent implements OnInit { | |||||
| shoppingListState: Observable<{ingredients: Ingredient[]}>; | shoppingListState: Observable<{ingredients: Ingredient[]}>; | ||||
| private subscription: Subscription | |||||
| constructor(private shoppingListService: ShoppingListService, | constructor(private shoppingListService: ShoppingListService, | ||||
| private store: Store<{shoppingList: {ingredients: Ingredient[]}}>) { } | private store: Store<{shoppingList: {ingredients: Ingredient[]}}>) { } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.shoppingListState = this.store.select('shoppingList'); | this.shoppingListState = this.store.select('shoppingList'); | ||||
| // this.subscription = this.shoppingListService.ingredientsChanged.subscribe( | |||||
| // (ingredients: Ingredient[]) => { | |||||
| // this.ingredients = ingredients; | |||||
| // } | |||||
| // ); | |||||
| } | } | ||||
| onEditItem(index: number) { | onEditItem(index: number) { | ||||
| this.shoppingListService.startedEditing.next(index); | this.shoppingListService.startedEditing.next(index); | ||||
| } | } | ||||
| ngOnDestroy() { | |||||
| this.subscription.unsubscribe(); | |||||
| } | |||||
| } | } | ||||
| @@ -5,20 +5,11 @@ export class ShoppingListService { | |||||
| ingredientsChanged = new Subject<Ingredient[]>(); | ingredientsChanged = new Subject<Ingredient[]>(); | ||||
| startedEditing = new Subject<number>(); | startedEditing = new Subject<number>(); | ||||
| private ingredients: Ingredient[] = []; | private ingredients: Ingredient[] = []; | ||||
| getIngredients() { | |||||
| return this.ingredients.slice(); | |||||
| } | |||||
| getIngredient(index: number) { | getIngredient(index: number) { | ||||
| return this.ingredients[index]; | return this.ingredients[index]; | ||||
| } | } | ||||
| addIngredient(ingredient: Ingredient) { | |||||
| this.ingredients.push(ingredient); | |||||
| this.ingredientsChanged.next(this.ingredients.slice()); | |||||
| } | |||||
| addIngredients(ingredients: Ingredient[]) { | addIngredients(ingredients: Ingredient[]) { | ||||
| this.ingredients.push(...ingredients); | this.ingredients.push(...ingredients); | ||||
| this.ingredientsChanged.next(this.ingredients.slice()); | this.ingredientsChanged.next(this.ingredients.slice()); | ||||