Skip to content

Commit 50e0071

Browse files
08-reducer-creators complete
1 parent 82ba050 commit 50e0071

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed

src/app/books/actions/books-api.actions.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,3 @@ export const bookDeleted = createAction(
2020
"[Books API] Book Deleted",
2121
props<{ book: Book }>()
2222
);
23-
24-
export type BooksApiActions = ReturnType<
25-
| typeof booksLoaded
26-
| typeof bookCreated
27-
| typeof bookUpdated
28-
| typeof bookDeleted
29-
>;

src/app/books/actions/books-page.actions.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,3 @@ export const deleteBook = createAction(
2727
props<{ book: Book }>()
2828
);
2929

30-
export type BooksActions = ReturnType<
31-
| typeof enter
32-
| typeof selectBook
33-
| typeof clearSelectedBook
34-
| typeof createBook
35-
| typeof updateBook
36-
| typeof deleteBook
37-
>;

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createEntityAdapter, EntityAdapter, EntityState } from "@ngrx/entity";
22
import { Book } from "src/app/shared/models/book.model";
33
import { BooksPageActions, BooksApiActions } from "src/app/books/actions";
4-
import { createSelector } from "@ngrx/store";
4+
import { createSelector, on, createReducer, Action } from "@ngrx/store";
55

66
export const initialBooks: Book[] = [
77
{
@@ -34,47 +34,42 @@ export const initialState = adapter.getInitialState({
3434
activeBookId: null
3535
});
3636

37+
const booksReducer = createReducer(
38+
initialState,
39+
on(BooksApiActions.booksLoaded,
40+
(state, { books }) => adapter.addAll(books, state)
41+
),
42+
on(BooksPageActions.selectBook,
43+
(state, { bookId }) => ({ ...state, activeBookId: bookId })
44+
),
45+
on(BooksPageActions.clearSelectedBook,
46+
state => ({ ...state,activeBookId: null })
47+
),
48+
on(BooksApiActions.bookCreated,
49+
(state, { book }) => adapter.addOne(book, {
50+
...state,
51+
activeBookId: book.id
52+
})
53+
),
54+
on(BooksApiActions.bookUpdated,
55+
(state, { book }) => adapter.updateOne(
56+
{ id: book.id, changes: book },
57+
{ ...state, activeBookId: book.id }
58+
)
59+
),
60+
on(BooksApiActions.bookDeleted,
61+
(state, { book }) => adapter.removeOne(book.id, {
62+
...state,
63+
activeBookId: null
64+
})
65+
)
66+
);
67+
3768
export function reducer(
3869
state = initialState,
39-
action: BooksPageActions.BooksActions | BooksApiActions.BooksApiActions
70+
action: Action
4071
): State {
41-
switch (action.type) {
42-
case BooksApiActions.booksLoaded.type:
43-
return adapter.addAll(action.books, state);
44-
45-
case BooksPageActions.selectBook.type:
46-
return {
47-
...state,
48-
activeBookId: action.bookId
49-
};
50-
51-
case BooksPageActions.clearSelectedBook.type:
52-
return {
53-
...state,
54-
activeBookId: null
55-
};
56-
57-
case BooksApiActions.bookCreated.type:
58-
return adapter.addOne(action.book, {
59-
...state,
60-
activeBookId: action.book.id
61-
});
62-
63-
case BooksApiActions.bookUpdated.type:
64-
return adapter.updateOne(
65-
{ id: action.book.id, changes: action.book },
66-
{ ...state, activeBookId: action.book.id }
67-
);
68-
69-
case BooksApiActions.bookDeleted.type:
70-
return adapter.removeOne(action.book.id, {
71-
...state,
72-
activeBookId: null
73-
});
74-
75-
default:
76-
return state;
77-
}
72+
return booksReducer(state, action);
7873
}
7974

8075
export const { selectAll, selectEntities } = adapter.getSelectors();

0 commit comments

Comments
 (0)