File tree 3 files changed +26
-11
lines changed
3 files changed +26
-11
lines changed Original file line number Diff line number Diff line change
1
+ import { Injectable } from "@angular/core" ;
2
+ import { Effect , Actions , ofType } from "@ngrx/effects" ;
3
+ import { mergeMap , map , catchError } from "rxjs/operators" ;
4
+ import { EMPTY } from "rxjs" ;
5
+ import { BooksService } from "../shared/services/book.service" ;
6
+ import { BooksPageActions , BooksApiActions } from "./actions" ;
7
+
8
+ @Injectable ( )
9
+ export class BooksApiEffects {
10
+ @Effect ( )
11
+ loadBooks$ = this . actions$ . pipe (
12
+ ofType ( BooksPageActions . enter ) ,
13
+ mergeMap ( ( ) =>
14
+ this . booksService . all ( ) . pipe (
15
+ map ( books => BooksApiActions . booksLoaded ( { books } ) ) ,
16
+ catchError ( ( ) => EMPTY )
17
+ )
18
+ )
19
+ ) ;
20
+
21
+ constructor ( private booksService : BooksService , private actions$ : Actions ) { }
22
+ }
Original file line number Diff line number Diff line change @@ -2,18 +2,21 @@ import { NgModule } from "@angular/core";
2
2
import { CommonModule } from "@angular/common" ;
3
3
import { RouterModule } from "@angular/router" ;
4
4
import { ReactiveFormsModule } from "@angular/forms" ;
5
+ import { EffectsModule } from "@ngrx/effects" ;
5
6
import { MaterialModule } from "src/app/material.module" ;
6
7
import { BooksPageComponent } from "./components/books-page/books-page.component" ;
7
8
import { BookDetailComponent } from "./components/book-detail/book-detail.component" ;
8
9
import { BooksListComponent } from "./components/books-list/books-list.component" ;
9
10
import { BooksTotalComponent } from "./components/books-total/books-total.component" ;
11
+ import { BooksApiEffects } from "./books-api.effects" ;
10
12
11
13
@NgModule ( {
12
14
imports : [
13
15
CommonModule ,
14
16
ReactiveFormsModule ,
15
17
MaterialModule ,
16
- RouterModule . forChild ( [ { path : "books" , component : BooksPageComponent } ] )
18
+ RouterModule . forChild ( [ { path : "books" , component : BooksPageComponent } ] ) ,
19
+ EffectsModule . forFeature ( [ BooksApiEffects ] )
17
20
] ,
18
21
declarations : [
19
22
BooksPageComponent ,
Original file line number Diff line number Diff line change @@ -29,14 +29,6 @@ export class BooksPageComponent implements OnInit {
29
29
30
30
ngOnInit ( ) {
31
31
this . store . dispatch ( BooksPageActions . enter ( ) ) ;
32
-
33
- this . getBooks ( ) ;
34
- }
35
-
36
- getBooks ( ) {
37
- this . booksService . all ( ) . subscribe ( books => {
38
- this . store . dispatch ( BooksApiActions . booksLoaded ( { books } ) ) ;
39
- } ) ;
40
32
}
41
33
42
34
onSelect ( book : BookModel ) {
@@ -63,7 +55,6 @@ export class BooksPageComponent implements OnInit {
63
55
this . store . dispatch ( BooksPageActions . createBook ( { book : bookProps } ) ) ;
64
56
65
57
this . booksService . create ( bookProps ) . subscribe ( book => {
66
- this . getBooks ( ) ;
67
58
this . removeSelectedBook ( ) ;
68
59
69
60
this . store . dispatch ( BooksApiActions . bookCreated ( { book } ) ) ;
@@ -84,7 +75,6 @@ export class BooksPageComponent implements OnInit {
84
75
this . store . dispatch ( BooksPageActions . deleteBook ( { bookId : book . id } ) ) ;
85
76
86
77
this . booksService . delete ( book . id ) . subscribe ( ( ) => {
87
- this . getBooks ( ) ;
88
78
this . removeSelectedBook ( ) ;
89
79
90
80
this . store . dispatch ( BooksApiActions . bookDeleted ( { bookId : book . id } ) ) ;
You can’t perform that action at this time.
0 commit comments