Skip to content

05-effects #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 04-selectors
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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