Skip to content
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

09-effects #12

Open
wants to merge 1 commit into
base: 08-reading-state
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
18 changes: 17 additions & 1 deletion src/app/books/books-api.effects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { Injectable } from "@angular/core";
import { createEffect, Actions, ofType } from "@ngrx/effects";
import {} from "rxjs/operators";
import { mergeMap, map } from "rxjs/operators";
import { BooksService } from "../shared/services";
import { BooksPageActions, BooksApiActions } from "./actions";

@Injectable()
export class BooksApiEffects {
constructor(private booksService: BooksService, private actions$: Actions) {}

loadBooks$ = createEffect(() =>
this.actions$.pipe(
ofType(BooksPageActions.enter),
mergeMap(() =>
this.booksService
.all()
.pipe(map(books => BooksApiActions.booksLoaded({ books })))
)
)
);
}
4 changes: 3 additions & 1 deletion src/app/books/books.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { BooksPageComponent } from "./components/books-page/books-page.component
import { BookDetailComponent } from "./components/book-detail/book-detail.component";
import { BooksListComponent } from "./components/books-list/books-list.component";
import { BooksTotalComponent } from "./components/books-total/books-total.component";
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
10 changes: 0 additions & 10 deletions src/app/books/components/books-page/books-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export class BooksPageComponent implements OnInit {

ngOnInit() {
this.store.dispatch(BooksPageActions.enter());

this.getBooks();
}

getBooks() {
this.booksService.all().subscribe(books => {
this.store.dispatch(BooksApiActions.booksLoaded({ books }));
});
}

onSelect(book: BookModel) {
Expand All @@ -63,7 +55,6 @@ export class BooksPageComponent implements OnInit {
this.store.dispatch(BooksPageActions.createBook({ book: bookProps }));

this.booksService.create(bookProps).subscribe(book => {
this.getBooks();
this.removeSelectedBook();

this.store.dispatch(BooksApiActions.bookCreated({ book }));
Expand All @@ -84,7 +75,6 @@ export class BooksPageComponent implements OnInit {
this.store.dispatch(BooksPageActions.deleteBook({ bookId: book.id }));

this.booksService.delete(book.id).subscribe(() => {
this.getBooks();
this.removeSelectedBook();

this.store.dispatch(BooksApiActions.bookDeleted({ bookId: book.id }));
Expand Down