File tree Expand file tree Collapse file tree 3 files changed +26
-11
lines changed Expand file tree Collapse file tree 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";
22import { CommonModule } from "@angular/common" ;
33import { RouterModule } from "@angular/router" ;
44import { ReactiveFormsModule } from "@angular/forms" ;
5+ import { EffectsModule } from "@ngrx/effects" ;
56import { MaterialModule } from "src/app/material.module" ;
67import { BooksPageComponent } from "./components/books-page/books-page.component" ;
78import { BookDetailComponent } from "./components/book-detail/book-detail.component" ;
89import { BooksListComponent } from "./components/books-list/books-list.component" ;
910import { BooksTotalComponent } from "./components/books-total/books-total.component" ;
11+ import { BooksApiEffects } from "./books-api.effects" ;
1012
1113@NgModule ( {
1214 imports : [
1315 CommonModule ,
1416 ReactiveFormsModule ,
1517 MaterialModule ,
16- RouterModule . forChild ( [ { path : "books" , component : BooksPageComponent } ] )
18+ RouterModule . forChild ( [ { path : "books" , component : BooksPageComponent } ] ) ,
19+ EffectsModule . forFeature ( [ BooksApiEffects ] )
1720 ] ,
1821 declarations : [
1922 BooksPageComponent ,
Original file line number Diff line number Diff line change @@ -29,14 +29,6 @@ export class BooksPageComponent implements OnInit {
2929
3030 ngOnInit ( ) {
3131 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- } ) ;
4032 }
4133
4234 onSelect ( book : BookModel ) {
@@ -63,7 +55,6 @@ export class BooksPageComponent implements OnInit {
6355 this . store . dispatch ( BooksPageActions . createBook ( { book : bookProps } ) ) ;
6456
6557 this . booksService . create ( bookProps ) . subscribe ( book => {
66- this . getBooks ( ) ;
6758 this . removeSelectedBook ( ) ;
6859
6960 this . store . dispatch ( BooksApiActions . bookCreated ( { book } ) ) ;
@@ -84,7 +75,6 @@ export class BooksPageComponent implements OnInit {
8475 this . store . dispatch ( BooksPageActions . deleteBook ( { bookId : book . id } ) ) ;
8576
8677 this . booksService . delete ( book . id ) . subscribe ( ( ) => {
87- this . getBooks ( ) ;
8878 this . removeSelectedBook ( ) ;
8979
9080 this . store . dispatch ( BooksApiActions . bookDeleted ( { bookId : book . id } ) ) ;
You can’t perform that action at this time.
0 commit comments