11import { Component , OnInit } from "@angular/core" ;
22
33import { Book } from "src/app/shared/models/book.model" ;
4- import { BooksService } from "src/app/shared/services/book.service" ;
54
65import { Observable } from "rxjs" ;
76import { Store , select } from "@ngrx/store" ;
@@ -15,14 +14,10 @@ import { map } from "rxjs/operators";
1514} )
1615export class BooksPageComponent implements OnInit {
1716 books$ : Observable < Book [ ] > ;
18- books : Book [ ] ;
1917 currentBook : Book ;
2018 total : number ;
2119
22- constructor (
23- private booksService : BooksService ,
24- private store : Store < fromRoot . State >
25- ) {
20+ constructor ( private store : Store < fromRoot . State > ) {
2621 this . books$ = this . store . pipe (
2722 select ( state => state . books ) ,
2823 map ( booksState => booksState . books )
@@ -35,10 +30,7 @@ export class BooksPageComponent implements OnInit {
3530 }
3631
3732 getBooks ( ) {
38- this . booksService . all ( ) . subscribe ( books => {
39- this . books = books ;
40- this . updateTotals ( books ) ;
41- } ) ;
33+ // Pending
4234 }
4335
4436 updateTotals ( books : Book [ ] ) {
@@ -48,6 +40,7 @@ export class BooksPageComponent implements OnInit {
4840 }
4941
5042 onSelect ( book : Book ) {
43+ this . store . dispatch ( { type : "select" , bookId : book . id } ) ;
5144 this . currentBook = book ;
5245 }
5346
@@ -56,6 +49,7 @@ export class BooksPageComponent implements OnInit {
5649 }
5750
5851 removeSelectedBook ( ) {
52+ this . store . dispatch ( { type : "clear select" } ) ;
5953 this . currentBook = null ;
6054 }
6155
@@ -68,23 +62,14 @@ export class BooksPageComponent implements OnInit {
6862 }
6963
7064 saveBook ( book : Book ) {
71- this . booksService . create ( book ) . subscribe ( ( ) => {
72- this . getBooks ( ) ;
73- this . removeSelectedBook ( ) ;
74- } ) ;
65+ this . store . dispatch ( { type : "create" , book } ) ;
7566 }
7667
7768 updateBook ( book : Book ) {
78- this . booksService . update ( book . id , book ) . subscribe ( ( ) => {
79- this . getBooks ( ) ;
80- this . removeSelectedBook ( ) ;
81- } ) ;
69+ this . store . dispatch ( { type : "update" , book } ) ;
8270 }
8371
8472 onDelete ( book : Book ) {
85- this . booksService . delete ( book . id ) . subscribe ( ( ) => {
86- this . getBooks ( ) ;
87- this . removeSelectedBook ( ) ;
88- } ) ;
73+ this . store . dispatch ( { type : "delete" , book } ) ;
8974 }
9075}
0 commit comments