Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

52 linhas
1.5 KiB

  1. import { Component, OnInit } from '@angular/core';
  2. import { Router, ActivatedRoute, Params } from '@angular/router';
  3. import { Store } from '@ngrx/store';
  4. import { Observable } from 'rxjs/Observable';
  5. import { Recipe } from '../recipe.model';
  6. import * as ShoppingListActions from '../../shopping-list/ngrx/shopping-list.actions';
  7. import * as fromApp from '../../ngrx/app.reducers';
  8. import * as fromRecipe from '../ngrx/recipe.reducers';
  9. import * as RecipeACtions from '../ngrx/recipe.actions';
  10. @Component({
  11. selector: 'app-recipe-detail',
  12. templateUrl: './recipe-detail.component.html',
  13. styleUrls: ['./recipe-detail.component.css']
  14. })
  15. export class RecipeDetailComponent implements OnInit {
  16. recipeState: Observable<fromRecipe.State>;
  17. id: number;
  18. constructor(private route: ActivatedRoute,
  19. private router: Router,
  20. private store: Store<fromRecipe.FeatureState>) { }
  21. ngOnInit() {
  22. this.route.params.subscribe(
  23. (params: Params) => {
  24. this.id = +params['id'];
  25. this.recipeState = this.store.select('recipes');
  26. }
  27. );
  28. }
  29. onAddToShoppingList() {
  30. this.store.select('recipes')
  31. .take(1)
  32. .subscribe((recipeState: fromRecipe.State) => {
  33. this.store.dispatch(new ShoppingListActions.AddIngredients(recipeState.recipes[this.id].ingredients));
  34. });
  35. }
  36. onEditRecipe() {
  37. this.router.navigate(['edit'], {relativeTo: this.route});
  38. }
  39. onDelete() {
  40. this.store.dispatch(new RecipeACtions.DeleteRecipe(this.id));
  41. this.router.navigate(['/recipes']);
  42. }
  43. }