-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbooks-api.effects.ts
More file actions
50 lines (45 loc) · 1.38 KB
/
books-api.effects.ts
File metadata and controls
50 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Injectable } from "@angular/core";
import { Effect, Actions, ofType } from "@ngrx/effects";
import { mergeMap, map, exhaustMap, concatMap } from "rxjs/operators";
import { BooksService } from "../shared/services/book.service";
import { BooksPageActions, BooksApiActions } from "./actions";
@Injectable()
export class BooksApiEffects {
@Effect()
loadBooks$ = this.actions$.pipe(
ofType(BooksPageActions.enter),
exhaustMap(() =>
this.booksService
.all()
.pipe(map(books => BooksApiActions.booksLoaded({ books })))
)
);
@Effect()
createBook$ = this.actions$.pipe(
ofType(BooksPageActions.createBook),
concatMap(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) {}
}