Skip to content

Commit 14718c3

Browse files
committed
05-reduce-api-actions
1 parent aef3c5f commit 14718c3

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/app/shared/state/books.reducer.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createReducer, on, Action } from "@ngrx/store";
22
import { BookModel } from "src/app/shared/models/book.model";
3-
import { BooksPageActions } from "src/app/books/actions";
3+
import { BooksPageActions, BooksApiActions } from "src/app/books/actions";
44

55
const createBook = (books: BookModel[], book: BookModel) => [...books, book];
66
const updateBook = (books: BookModel[], changes: BookModel) =>
@@ -33,6 +33,30 @@ export const booksReducer = createReducer(
3333
...state,
3434
activeBookId: action.bookId
3535
};
36+
}),
37+
on(BooksApiActions.booksLoaded, (state, action) => {
38+
return {
39+
...state,
40+
collection: action.books
41+
};
42+
}),
43+
on(BooksApiActions.bookCreated, (state, action) => {
44+
return {
45+
collection: createBook(state.collection, action.book),
46+
activeBookId: null
47+
};
48+
}),
49+
on(BooksApiActions.bookUpdated, (state, action) => {
50+
return {
51+
collection: updateBook(state.collection, action.book),
52+
activeBookId: null
53+
};
54+
}),
55+
on(BooksApiActions.bookDeleted, (state, action) => {
56+
return {
57+
...state,
58+
collection: deleteBook(state.collection, action.bookId)
59+
};
3660
})
3761
);
3862

0 commit comments

Comments
 (0)