Skip to content

Commit f137acf

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
05-effects loading complete
1 parent 902f2dc commit f137acf

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
import { Book } from "src/app/shared/models/book.model";
22
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;

src/app/books/books-api.effects.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
@Effect()
11+
loadBooks$ = this.actions$.pipe(
12+
ofType(BooksPageActions.BooksActionTypes.Enter),
13+
mergeMap(() =>
14+
this.booksService.all().pipe(
15+
map(books => new BooksApiActions.BooksLoaded(books)),
16+
catchError(() => EMPTY)
17+
)
18+
)
19+
);
20+
21+
constructor(
22+
private booksService: BooksService,
23+
private actions$: Actions<
24+
BooksPageActions.BooksActions | BooksApiActions.BooksApiActions
25+
>
26+
) {}
27+
}

src/app/books/books.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import { BookDetailComponent } from "./components/book-detail/book-detail.compon
1010
import { BooksListComponent } from "./components/books-list/books-list.component";
1111
import { BooksTotalComponent } from "./components/books-total/books-total.component";
1212

13+
import { EffectsModule } from "@ngrx/effects";
14+
import { BooksApiEffects } from "./books-api.effects";
15+
1316
@NgModule({
1417
imports: [
1518
CommonModule,
1619
ReactiveFormsModule,
1720
MaterialModule,
18-
RouterModule.forChild([{ path: "books", component: BooksPageComponent }])
21+
RouterModule.forChild([{ path: "books", component: BooksPageComponent }]),
22+
EffectsModule.forFeature([BooksApiEffects])
1923
],
2024
declarations: [
2125
BooksPageComponent,

src/app/books/components/books-page/books-page.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Book } from "src/app/shared/models/book.model";
55
import { Observable } from "rxjs";
66
import { Store, select } from "@ngrx/store";
77
import * as fromRoot from "src/app/shared/state";
8-
import { map, tap } from "rxjs/operators";
98
import { BooksPageActions } from "../../actions";
109

1110
@Component({

src/app/shared/state/books.reducer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createEntityAdapter, EntityAdapter, EntityState } from "@ngrx/entity";
22
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";
44
import { createSelector } from "@ngrx/store";
55

66
export const initialBooks: Book[] = [
@@ -36,11 +36,11 @@ export const initialState = adapter.getInitialState({
3636

3737
export function reducer(
3838
state = initialState,
39-
action: BooksPageActions.BooksActions
39+
action: BooksPageActions.BooksActions | BooksApiActions.BooksApiActions
4040
): State {
4141
switch (action.type) {
42-
case BooksPageActions.BooksActionTypes.Enter:
43-
return adapter.addAll(initialBooks, state);
42+
case BooksApiActions.BooksApiActionTypes.BooksLoaded:
43+
return adapter.addAll(action.books, state);
4444

4545
case BooksPageActions.BooksActionTypes.SelectBook:
4646
return {

0 commit comments

Comments
 (0)