1
1
import { Component , OnInit } from "@angular/core" ;
2
+ import { Store } from "@ngrx/store" ;
3
+ import { State } from "src/app/shared/state" ;
2
4
import {
3
5
BookModel ,
4
6
calculateBooksGrossEarnings ,
5
7
BookRequiredProps
6
8
} from "src/app/shared/models/book.model" ;
7
9
import { BooksService } from "src/app/shared/services/book.service" ;
10
+ import { BooksPageActions } from "../../actions" ;
8
11
9
12
@Component ( {
10
13
selector : "app-books" ,
@@ -16,9 +19,14 @@ export class BooksPageComponent implements OnInit {
16
19
currentBook : BookModel | null = null ;
17
20
total : number = 0 ;
18
21
19
- constructor ( private booksService : BooksService ) { }
22
+ constructor (
23
+ private booksService : BooksService ,
24
+ private store : Store < State >
25
+ ) { }
20
26
21
27
ngOnInit ( ) {
28
+ this . store . dispatch ( BooksPageActions . enter ( ) ) ;
29
+
22
30
this . getBooks ( ) ;
23
31
this . removeSelectedBook ( ) ;
24
32
}
@@ -35,6 +43,8 @@ export class BooksPageComponent implements OnInit {
35
43
}
36
44
37
45
onSelect ( book : BookModel ) {
46
+ this . store . dispatch ( BooksPageActions . selectBook ( { bookId : book . id } ) ) ;
47
+
38
48
this . currentBook = book ;
39
49
}
40
50
@@ -43,6 +53,8 @@ export class BooksPageComponent implements OnInit {
43
53
}
44
54
45
55
removeSelectedBook ( ) {
56
+ this . store . dispatch ( BooksPageActions . clearSelectedBook ( ) ) ;
57
+
46
58
this . currentBook = null ;
47
59
}
48
60
@@ -55,20 +67,28 @@ export class BooksPageComponent implements OnInit {
55
67
}
56
68
57
69
saveBook ( bookProps : BookRequiredProps ) {
70
+ this . store . dispatch ( BooksPageActions . createBook ( { book : bookProps } ) ) ;
71
+
58
72
this . booksService . create ( bookProps ) . subscribe ( ( ) => {
59
73
this . getBooks ( ) ;
60
74
this . removeSelectedBook ( ) ;
61
75
} ) ;
62
76
}
63
77
64
78
updateBook ( book : BookModel ) {
79
+ this . store . dispatch (
80
+ BooksPageActions . updateBook ( { bookId : book . id , changes : book } )
81
+ ) ;
82
+
65
83
this . booksService . update ( book . id , book ) . subscribe ( ( ) => {
66
84
this . getBooks ( ) ;
67
85
this . removeSelectedBook ( ) ;
68
86
} ) ;
69
87
}
70
88
71
89
onDelete ( book : BookModel ) {
90
+ this . store . dispatch ( BooksPageActions . deleteBook ( { bookId : book . id } ) ) ;
91
+
72
92
this . booksService . delete ( book . id ) . subscribe ( ( ) => {
73
93
this . getBooks ( ) ;
74
94
this . removeSelectedBook ( ) ;
0 commit comments