1
1
import { Component , OnInit } from "@angular/core" ;
2
2
3
3
import { Book } from "src/app/shared/models/book.model" ;
4
- import { BooksService } from "src/app/shared/services/book.service" ;
5
4
6
5
import { Observable } from "rxjs" ;
7
6
import { Store , select } from "@ngrx/store" ;
@@ -15,14 +14,10 @@ import { map } from "rxjs/operators";
15
14
} )
16
15
export class BooksPageComponent implements OnInit {
17
16
books$ : Observable < Book [ ] > ;
18
- books : Book [ ] ;
19
17
currentBook : Book ;
20
18
total : number ;
21
19
22
- constructor (
23
- private booksService : BooksService ,
24
- private store : Store < fromRoot . State >
25
- ) {
20
+ constructor ( private store : Store < fromRoot . State > ) {
26
21
this . books$ = this . store . pipe (
27
22
select ( state => state . books ) ,
28
23
map ( booksState => booksState . books )
@@ -35,10 +30,7 @@ export class BooksPageComponent implements OnInit {
35
30
}
36
31
37
32
getBooks ( ) {
38
- this . booksService . all ( ) . subscribe ( books => {
39
- this . books = books ;
40
- this . updateTotals ( books ) ;
41
- } ) ;
33
+ // Pending
42
34
}
43
35
44
36
updateTotals ( books : Book [ ] ) {
@@ -48,6 +40,7 @@ export class BooksPageComponent implements OnInit {
48
40
}
49
41
50
42
onSelect ( book : Book ) {
43
+ this . store . dispatch ( { type : "select" , bookId : book . id } ) ;
51
44
this . currentBook = book ;
52
45
}
53
46
@@ -56,6 +49,7 @@ export class BooksPageComponent implements OnInit {
56
49
}
57
50
58
51
removeSelectedBook ( ) {
52
+ this . store . dispatch ( { type : "clear select" } ) ;
59
53
this . currentBook = null ;
60
54
}
61
55
@@ -68,23 +62,14 @@ export class BooksPageComponent implements OnInit {
68
62
}
69
63
70
64
saveBook ( book : Book ) {
71
- this . booksService . create ( book ) . subscribe ( ( ) => {
72
- this . getBooks ( ) ;
73
- this . removeSelectedBook ( ) ;
74
- } ) ;
65
+ this . store . dispatch ( { type : "create" , book } ) ;
75
66
}
76
67
77
68
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 } ) ;
82
70
}
83
71
84
72
onDelete ( book : Book ) {
85
- this . booksService . delete ( book . id ) . subscribe ( ( ) => {
86
- this . getBooks ( ) ;
87
- this . removeSelectedBook ( ) ;
88
- } ) ;
73
+ this . store . dispatch ( { type : "delete" , book } ) ;
89
74
}
90
75
}
0 commit comments