From 12babe36ddda112fd809d5ce4f68727addfdd2da Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Fri, 6 Sep 2019 07:41:10 -0700 Subject: [PATCH 1/2] 10-more-effects --- src/app/books/books-api.effects.ts | 34 +++++++++++++++++-- .../books-page/books-page.component.ts | 21 ++---------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/app/books/books-api.effects.ts b/src/app/books/books-api.effects.ts index 07ce516..ab9cd56 100644 --- a/src/app/books/books-api.effects.ts +++ b/src/app/books/books-api.effects.ts @@ -1,6 +1,6 @@ import { Injectable } from "@angular/core"; import { Effect, Actions, ofType } from "@ngrx/effects"; -import { mergeMap, map } from "rxjs/operators"; +import { mergeMap, map, exhaustMap, concatMap } from "rxjs/operators"; import { BooksService } from "../shared/services/book.service"; import { BooksPageActions, BooksApiActions } from "./actions"; @@ -9,12 +9,42 @@ export class BooksApiEffects { @Effect() loadBooks$ = this.actions$.pipe( ofType(BooksPageActions.enter), - mergeMap(() => + exhaustMap(() => this.booksService .all() .pipe(map(books => BooksApiActions.booksLoaded({ books }))) ) ); + @Effect() + createBook$ = this.actions$.pipe( + ofType(BooksPageActions.createBook), + mergeMap(action => + this.booksService + .create(action.book) + .pipe(map(book => BooksApiActions.bookCreated({ book }))) + ) + ); + + @Effect() + updateBook$ = this.actions$.pipe( + ofType(BooksPageActions.updateBook), + concatMap(action => + this.booksService + .update(action.bookId, action.changes) + .pipe(map(book => BooksApiActions.bookUpdated({ book }))) + ) + ); + + @Effect() + deleteBook$ = this.actions$.pipe( + ofType(BooksPageActions.deleteBook), + mergeMap(action => + this.booksService + .delete(action.bookId) + .pipe(map(() => BooksApiActions.bookDeleted({ bookId: action.bookId }))) + ) + ); + constructor(private booksService: BooksService, private actions$: Actions) {} } diff --git a/src/app/books/components/books-page/books-page.component.ts b/src/app/books/components/books-page/books-page.component.ts index c96f0b7..140bc23 100755 --- a/src/app/books/components/books-page/books-page.component.ts +++ b/src/app/books/components/books-page/books-page.component.ts @@ -8,8 +8,7 @@ import { selectBooksEarningsTotals } from "src/app/shared/state"; import { BookModel, BookRequiredProps } from "src/app/shared/models/book.model"; -import { BooksService } from "src/app/shared/services/book.service"; -import { BooksPageActions, BooksApiActions } from "../../actions"; +import { BooksPageActions } from "../../actions"; @Component({ selector: "app-books", @@ -21,7 +20,7 @@ export class BooksPageComponent implements OnInit { currentBook$: Observable; total$: Observable; - constructor(private booksService: BooksService, private store: Store) { + constructor(private store: Store) { this.books$ = store.select(selectAllBooks); this.currentBook$ = store.select(selectActiveBook); this.total$ = store.select(selectBooksEarningsTotals); @@ -53,31 +52,15 @@ export class BooksPageComponent implements OnInit { saveBook(bookProps: BookRequiredProps) { this.store.dispatch(BooksPageActions.createBook({ book: bookProps })); - - this.booksService.create(bookProps).subscribe(book => { - this.removeSelectedBook(); - - this.store.dispatch(BooksApiActions.bookCreated({ book })); - }); } updateBook(book: BookModel) { this.store.dispatch( BooksPageActions.updateBook({ bookId: book.id, changes: book }) ); - - this.booksService.update(book.id, book).subscribe(book => { - this.store.dispatch(BooksApiActions.bookUpdated({ book })); - }); } onDelete(book: BookModel) { this.store.dispatch(BooksPageActions.deleteBook({ bookId: book.id })); - - this.booksService.delete(book.id).subscribe(() => { - this.removeSelectedBook(); - - this.store.dispatch(BooksApiActions.bookDeleted({ bookId: book.id })); - }); } } From a5c02f8fb6414e44967f8a334202f4cc85468613 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 18 Sep 2019 09:56:06 -0500 Subject: [PATCH 2/2] Update effect for creating book to use concatMap --- src/app/books/books-api.effects.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/books/books-api.effects.ts b/src/app/books/books-api.effects.ts index ab9cd56..157d1b8 100644 --- a/src/app/books/books-api.effects.ts +++ b/src/app/books/books-api.effects.ts @@ -19,7 +19,7 @@ export class BooksApiEffects { @Effect() createBook$ = this.actions$.pipe( ofType(BooksPageActions.createBook), - mergeMap(action => + concatMap(action => this.booksService .create(action.book) .pipe(map(book => BooksApiActions.bookCreated({ book })))