11import { Component , OnInit } from "@angular/core" ;
2+ import { Store } from "@ngrx/store" ;
3+ import { State } from "src/app/shared/state" ;
24import {
35 BookModel ,
46 calculateBooksGrossEarnings ,
57 BookRequiredProps
68} from "src/app/shared/models/book.model" ;
79import { BooksService } from "src/app/shared/services/book.service" ;
10+ import { BooksPageActions } from "../../actions" ;
811
912@Component ( {
1013 selector : "app-books" ,
@@ -16,9 +19,14 @@ export class BooksPageComponent implements OnInit {
1619 currentBook : BookModel | null = null ;
1720 total : number = 0 ;
1821
19- constructor ( private booksService : BooksService ) { }
22+ constructor (
23+ private booksService : BooksService ,
24+ private store : Store < State >
25+ ) { }
2026
2127 ngOnInit ( ) {
28+ this . store . dispatch ( BooksPageActions . enter ( ) ) ;
29+
2230 this . getBooks ( ) ;
2331 this . removeSelectedBook ( ) ;
2432 }
@@ -35,6 +43,8 @@ export class BooksPageComponent implements OnInit {
3543 }
3644
3745 onSelect ( book : BookModel ) {
46+ this . store . dispatch ( BooksPageActions . selectBook ( { bookId : book . id } ) ) ;
47+
3848 this . currentBook = book ;
3949 }
4050
@@ -43,6 +53,8 @@ export class BooksPageComponent implements OnInit {
4353 }
4454
4555 removeSelectedBook ( ) {
56+ this . store . dispatch ( BooksPageActions . clearSelectedBook ( ) ) ;
57+
4658 this . currentBook = null ;
4759 }
4860
@@ -55,20 +67,28 @@ export class BooksPageComponent implements OnInit {
5567 }
5668
5769 saveBook ( bookProps : BookRequiredProps ) {
70+ this . store . dispatch ( BooksPageActions . createBook ( { book : bookProps } ) ) ;
71+
5872 this . booksService . create ( bookProps ) . subscribe ( ( ) => {
5973 this . getBooks ( ) ;
6074 this . removeSelectedBook ( ) ;
6175 } ) ;
6276 }
6377
6478 updateBook ( book : BookModel ) {
79+ this . store . dispatch (
80+ BooksPageActions . updateBook ( { bookId : book . id , changes : book } )
81+ ) ;
82+
6583 this . booksService . update ( book . id , book ) . subscribe ( ( ) => {
6684 this . getBooks ( ) ;
6785 this . removeSelectedBook ( ) ;
6886 } ) ;
6987 }
7088
7189 onDelete ( book : BookModel ) {
90+ this . store . dispatch ( BooksPageActions . deleteBook ( { bookId : book . id } ) ) ;
91+
7292 this . booksService . delete ( book . id ) . subscribe ( ( ) => {
7393 this . getBooks ( ) ;
7494 this . removeSelectedBook ( ) ;
0 commit comments