Skip to content

Commit 1adeb0a

Browse files
committed
09-effects
1 parent 6715c7c commit 1adeb0a

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

Diff for: src/app/books/books-api.effects.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

Diff for: src/app/books/books.module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import { NgModule } from "@angular/core";
22
import { CommonModule } from "@angular/common";
33
import { RouterModule } from "@angular/router";
44
import { ReactiveFormsModule } from "@angular/forms";
5+
import { EffectsModule } from "@ngrx/effects";
56
import { MaterialModule } from "src/app/material.module";
67
import { BooksPageComponent } from "./components/books-page/books-page.component";
78
import { BookDetailComponent } from "./components/book-detail/book-detail.component";
89
import { BooksListComponent } from "./components/books-list/books-list.component";
910
import { 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,

Diff for: src/app/books/components/books-page/books-page.component.ts

-10
Original file line numberDiff line numberDiff 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 }));

0 commit comments

Comments
 (0)