File tree Expand file tree Collapse file tree 5 files changed +48
-5
lines changed Expand file tree Collapse file tree 5 files changed +48
-5
lines changed Original file line number Diff line number Diff line change 1
1
import { Book } from "src/app/shared/models/book.model" ;
2
2
import { Action } from "@ngrx/store" ;
3
+
4
+ export enum BooksApiActionTypes {
5
+ BooksLoaded = '[Books API] Books Loaded Success' ,
6
+ }
7
+
8
+ export class BooksLoaded implements Action {
9
+ readonly type = BooksApiActionTypes . BooksLoaded ;
10
+
11
+ constructor ( public books : Book [ ] ) { }
12
+ }
13
+
14
+ export type BooksApiActions =
15
+ | BooksLoaded ;
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 { BooksPageActions , BooksApiActions } from './actions' ;
4
+ import { BooksService } from '../shared/services/book.service' ;
5
+ import { mergeMap , map , catchError } from 'rxjs/operators' ;
6
+ import { EMPTY } from 'rxjs' ;
7
+
8
+ @Injectable ( )
9
+ export class BooksApiEffects {
10
+
11
+ @Effect ( )
12
+ loadBooks$ = this . actions$ . pipe (
13
+ ofType ( BooksPageActions . BooksActionTypes . Enter ) ,
14
+ mergeMap ( ( ) =>
15
+ this . booksService . all ( )
16
+ . pipe (
17
+ map ( books => new BooksApiActions . BooksLoaded ( books ) ) ,
18
+ catchError ( ( ) => EMPTY )
19
+ )
20
+ )
21
+ ) ;
22
+
23
+ constructor (
24
+ private booksService : BooksService ,
25
+ private actions$ : Actions < BooksPageActions . BooksActions | BooksApiActions . BooksApiActions >
26
+ ) { }
27
+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ import { BookDetailComponent } from './components/book-detail/book-detail.compon
10
10
import { BooksListComponent } from './components/books-list/books-list.component' ;
11
11
import { BooksTotalComponent } from './components/books-total/books-total.component' ;
12
12
13
+ import { EffectsModule } from '@ngrx/effects' ;
14
+ import { BooksApiEffects } from './books-api.effects' ;
15
+
13
16
@NgModule ( {
14
17
imports : [
15
18
CommonModule ,
@@ -18,6 +21,7 @@ import { BooksTotalComponent } from './components/books-total/books-total.compon
18
21
RouterModule . forChild ( [
19
22
{ path : 'books' , component : BooksPageComponent }
20
23
] ) ,
24
+ EffectsModule . forFeature ( [ BooksApiEffects ] )
21
25
] ,
22
26
declarations : [
23
27
BooksPageComponent ,
Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ import { Book } from 'src/app/shared/models/book.model';
5
5
import { Observable } from 'rxjs' ;
6
6
import { Store , select } from '@ngrx/store' ;
7
7
import * as fromRoot from 'src/app/shared/state' ;
8
- import { map , tap } from 'rxjs/operators' ;
9
8
import { BooksPageActions } from '../../actions' ;
10
9
11
10
@Component ( {
Original file line number Diff line number Diff line change 1
1
import { createEntityAdapter , EntityAdapter , EntityState } from '@ngrx/entity' ;
2
2
import { Book } from 'src/app/shared/models/book.model' ;
3
- import { BooksPageActions } from 'src/app/books/actions' ;
3
+ import { BooksPageActions , BooksApiActions } from 'src/app/books/actions' ;
4
4
5
5
6
6
export const initialBooks : Book [ ] = [
@@ -34,10 +34,10 @@ export const initialState = adapter.getInitialState({
34
34
activeBookId : null
35
35
} ) ;
36
36
37
- export function reducer ( state = initialState , action : BooksPageActions . BooksActions ) : State {
37
+ export function reducer ( state = initialState , action : BooksPageActions . BooksActions | BooksApiActions . BooksApiActions ) : State {
38
38
switch ( action . type ) {
39
- case BooksPageActions . BooksActionTypes . Enter :
40
- return adapter . addAll ( initialBooks , state ) ;
39
+ case BooksApiActions . BooksApiActionTypes . BooksLoaded :
40
+ return adapter . addAll ( action . books , state ) ;
41
41
42
42
case BooksPageActions . BooksActionTypes . SelectBook :
43
43
return {
You can’t perform that action at this time.
0 commit comments