import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute, Params } from '@angular/router'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable'; import { Recipe } from '../recipe.model'; import * as ShoppingListActions from '../../shopping-list/ngrx/shopping-list.actions'; import * as fromApp from '../../ngrx/app.reducers'; import * as fromRecipe from '../ngrx/recipe.reducers'; import * as RecipeACtions from '../ngrx/recipe.actions'; @Component({ selector: 'app-recipe-detail', templateUrl: './recipe-detail.component.html', styleUrls: ['./recipe-detail.component.css'] }) export class RecipeDetailComponent implements OnInit { recipeState: Observable; id: number; constructor(private route: ActivatedRoute, private router: Router, private store: Store) { } ngOnInit() { this.route.params.subscribe( (params: Params) => { this.id = +params['id']; this.recipeState = this.store.select('recipes'); } ); } onAddToShoppingList() { this.store.select('recipes') .take(1) .subscribe((recipeState: fromRecipe.State) => { this.store.dispatch(new ShoppingListActions.AddIngredients(recipeState.recipes[this.id].ingredients)); }); } onEditRecipe() { this.router.navigate(['edit'], {relativeTo: this.route}); } onDelete() { this.store.dispatch(new RecipeACtions.DeleteRecipe(this.id)); this.router.navigate(['/recipes']); } }