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,12 +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
2220 constructor (
23- private booksService : BooksService ,
2421 private store : Store < fromRoot . State >
2522 ) {
2623 this . books$ = this . store . pipe (
@@ -35,11 +32,7 @@ export class BooksPageComponent implements OnInit {
3532 }
3633
3734 getBooks ( ) {
38- this . booksService . all ( )
39- . subscribe ( books => {
40- this . books = books ;
41- this . updateTotals ( books ) ;
42- } ) ;
35+ // Pending
4336 }
4437
4538 updateTotals ( books : Book [ ] ) {
@@ -49,6 +42,7 @@ export class BooksPageComponent implements OnInit {
4942 }
5043
5144 onSelect ( book : Book ) {
45+ this . store . dispatch ( { type : 'select' , bookId : book . id } ) ;
5246 this . currentBook = book ;
5347 }
5448
@@ -57,6 +51,7 @@ export class BooksPageComponent implements OnInit {
5751 }
5852
5953 removeSelectedBook ( ) {
54+ this . store . dispatch ( { type : 'clear select' } ) ;
6055 this . currentBook = null ;
6156 }
6257
@@ -69,26 +64,14 @@ export class BooksPageComponent implements OnInit {
6964 }
7065
7166 saveBook ( book : Book ) {
72- this . booksService . create ( book )
73- . subscribe ( ( ) => {
74- this . getBooks ( ) ;
75- this . removeSelectedBook ( ) ;
76- } ) ;
67+ this . store . dispatch ( { type : 'create' , book } ) ;
7768 }
7869
7970 updateBook ( book : Book ) {
80- this . booksService . update ( book . id , book )
81- . subscribe ( ( ) => {
82- this . getBooks ( ) ;
83- this . removeSelectedBook ( ) ;
84- } ) ;
71+ this . store . dispatch ( { type : 'update' , book } ) ;
8572 }
8673
8774 onDelete ( book : Book ) {
88- this . booksService . delete ( book . id )
89- . subscribe ( ( ) => {
90- this . getBooks ( ) ;
91- this . removeSelectedBook ( ) ;
92- } ) ;
75+ this . store . dispatch ( { type : 'delete' , book } ) ;
9376 }
9477}
0 commit comments