Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import { Component, OnInit } from "@angular/core";
  2. import { Store } from "@ngrx/store";
  3. import { selectHero, State } from "../../reducers";
  4.  
  5. import { Hero } from "../../models/hero.model";
  6. import * as heroActions from "../../actions/hero.actions";
  7. import { FormBuilder, FormGroup, Validators } from "@angular/forms";
  8.  
  9. @Component({
  10. selector: "app-heroes",
  11. templateUrl: "./heroes.component.html",
  12. styleUrls: ["./heroes.component.css"]
  13. })
  14. /*
  15. * This is a container component
  16. * contains different presentational components
  17. */
  18. export class HeroesComponent implements OnInit {
  19. editItemUrl: string = "/heroes/edit-hero/";
  20. list$: any;
  21. list: Hero[];
  22. newItemForm: FormGroup;
  23. isShowNewItemForm: boolean = false;
  24.  
  25. constructor(private store: Store<State>, private fb: FormBuilder) {}
  26.  
  27. ngOnInit() {
  28. this.formBuilderInit();
  29. this.store.dispatch(new heroActions.LoadHeroes()); // dispatching a LoadHeroes action
  30. this.list$ = this.store.select(selectHero); // picking up the selectHero selector which returns state.heroes
  31. }
  32.  
  33. onSubmit() {
  34. this.store.dispatch(new heroActions.CreateHero(this.newItemForm.value)); // dispatching a CreateHero action
  35. this.newItemForm.reset();
  36. this.isShowNewItemForm = !this.isShowNewItemForm;
  37. }
  38.  
  39. showNewItemForm() {
  40. this.isShowNewItemForm = !this.isShowNewItemForm;
  41. }
  42.  
  43. cancelForm() {
  44. this.isShowNewItemForm = !this.isShowNewItemForm;
  45. }
  46.  
  47. removeItem(hero: Hero) {
  48. const isConfirmed = confirm(`Delete ${hero.firstName}`);
  49. if (!isConfirmed) return;
  50.  
  51. this.store.dispatch(new heroActions.DeleteHero(hero)); // dispatching a DeleteHero action
  52. }
  53.  
  54. private formBuilderInit(): void {
  55. this.newItemForm = this.fb.group({
  56. firstName: ["", Validators.required],
  57. lastName: [""],
  58. house: [""],
  59. knownAs: [""]
  60. });
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement