Skip to content

Commit

Permalink
05-effects loading complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Apr 27, 2019
1 parent 902f2dc commit f137acf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/app/books/actions/books-api.actions.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
import { Book } from "src/app/shared/models/book.model";
import { Action } from "@ngrx/store";

export enum BooksApiActionTypes {
BooksLoaded = '[Books API] Books Loaded Success',
}

export class BooksLoaded implements Action {
readonly type = BooksApiActionTypes.BooksLoaded;

constructor(public books: Book[]) {}
}

export type BooksApiActions =
| BooksLoaded;
27 changes: 27 additions & 0 deletions src/app/books/books-api.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from "@angular/core";
import { Effect, Actions, ofType } from "@ngrx/effects";
import { BooksPageActions, BooksApiActions } from "./actions";
import { BooksService } from "../shared/services/book.service";
import { mergeMap, map, catchError } from "rxjs/operators";
import { EMPTY } from "rxjs";

@Injectable()
export class BooksApiEffects {
@Effect()
loadBooks$ = this.actions$.pipe(
ofType(BooksPageActions.BooksActionTypes.Enter),
mergeMap(() =>
this.booksService.all().pipe(
map(books => new BooksApiActions.BooksLoaded(books)),
catchError(() => EMPTY)
)
)
);

constructor(
private booksService: BooksService,
private actions$: Actions<
BooksPageActions.BooksActions | BooksApiActions.BooksApiActions
>
) {}
}
6 changes: 5 additions & 1 deletion src/app/books/books.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import { BookDetailComponent } from "./components/book-detail/book-detail.compon
import { BooksListComponent } from "./components/books-list/books-list.component";
import { BooksTotalComponent } from "./components/books-total/books-total.component";

import { EffectsModule } from "@ngrx/effects";
import { BooksApiEffects } from "./books-api.effects";

@NgModule({
imports: [
CommonModule,
ReactiveFormsModule,
MaterialModule,
RouterModule.forChild([{ path: "books", component: BooksPageComponent }])
RouterModule.forChild([{ path: "books", component: BooksPageComponent }]),
EffectsModule.forFeature([BooksApiEffects])
],
declarations: [
BooksPageComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Book } from "src/app/shared/models/book.model";
import { Observable } from "rxjs";
import { Store, select } from "@ngrx/store";
import * as fromRoot from "src/app/shared/state";
import { map, tap } from "rxjs/operators";
import { BooksPageActions } from "../../actions";

@Component({
Expand Down
8 changes: 4 additions & 4 deletions src/app/shared/state/books.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createEntityAdapter, EntityAdapter, EntityState } from "@ngrx/entity";
import { Book } from "src/app/shared/models/book.model";
import { BooksPageActions } from "src/app/books/actions";
import { BooksPageActions, BooksApiActions } from "src/app/books/actions";
import { createSelector } from "@ngrx/store";

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

export function reducer(
state = initialState,
action: BooksPageActions.BooksActions
action: BooksPageActions.BooksActions | BooksApiActions.BooksApiActions
): State {
switch (action.type) {
case BooksPageActions.BooksActionTypes.Enter:
return adapter.addAll(initialBooks, state);
case BooksApiActions.BooksApiActionTypes.BooksLoaded:
return adapter.addAll(action.books, state);

case BooksPageActions.BooksActionTypes.SelectBook:
return {
Expand Down

0 comments on commit f137acf

Please sign in to comment.