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,12 +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
20
constructor (
23
- private booksService : BooksService ,
24
21
private store : Store < fromRoot . State >
25
22
) {
26
23
this . books$ = this . store . pipe (
@@ -35,11 +32,7 @@ export class BooksPageComponent implements OnInit {
35
32
}
36
33
37
34
getBooks ( ) {
38
- this . booksService . all ( )
39
- . subscribe ( books => {
40
- this . books = books ;
41
- this . updateTotals ( books ) ;
42
- } ) ;
35
+ // Pending
43
36
}
44
37
45
38
updateTotals ( books : Book [ ] ) {
@@ -49,6 +42,7 @@ export class BooksPageComponent implements OnInit {
49
42
}
50
43
51
44
onSelect ( book : Book ) {
45
+ this . store . dispatch ( { type : 'select' , bookId : book . id } ) ;
52
46
this . currentBook = book ;
53
47
}
54
48
@@ -57,6 +51,7 @@ export class BooksPageComponent implements OnInit {
57
51
}
58
52
59
53
removeSelectedBook ( ) {
54
+ this . store . dispatch ( { type : 'clear select' } ) ;
60
55
this . currentBook = null ;
61
56
}
62
57
@@ -69,26 +64,14 @@ export class BooksPageComponent implements OnInit {
69
64
}
70
65
71
66
saveBook ( book : Book ) {
72
- this . booksService . create ( book )
73
- . subscribe ( ( ) => {
74
- this . getBooks ( ) ;
75
- this . removeSelectedBook ( ) ;
76
- } ) ;
67
+ this . store . dispatch ( { type : 'create' , book } ) ;
77
68
}
78
69
79
70
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 } ) ;
85
72
}
86
73
87
74
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 } ) ;
93
76
}
94
77
}
0 commit comments