Skip to content

Commit 93722f5

Browse files
committed
11-entity
1 parent 12babe3 commit 93722f5

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

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

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
import { createReducer, on, Action, createSelector } from "@ngrx/store";
2+
import { createEntityAdapter, EntityState } from "@ngrx/entity";
23
import {
34
BookModel,
45
calculateBooksGrossEarnings
56
} from "src/app/shared/models/book.model";
67
import { BooksPageActions, BooksApiActions } from "src/app/books/actions";
78

8-
const createBook = (books: BookModel[], book: BookModel) => [...books, book];
9-
const updateBook = (books: BookModel[], changes: BookModel) =>
10-
books.map(book => {
11-
return book.id === changes.id ? Object.assign({}, book, changes) : book;
12-
});
13-
const deleteBook = (books: BookModel[], bookId: string) =>
14-
books.filter(book => bookId !== book.id);
15-
16-
export interface State {
17-
collection: BookModel[];
9+
export interface State extends EntityState<BookModel> {
1810
activeBookId: string | null;
1911
}
2012

21-
export const initialState: State = {
22-
collection: [],
13+
export const adapter = createEntityAdapter<BookModel>();
14+
15+
export const initialState: State = adapter.getInitialState({
2316
activeBookId: null
24-
};
17+
});
2518

2619
export const booksReducer = createReducer(
2720
initialState,
@@ -38,41 +31,40 @@ export const booksReducer = createReducer(
3831
};
3932
}),
4033
on(BooksApiActions.booksLoaded, (state, action) => {
41-
return {
42-
...state,
43-
collection: action.books
44-
};
34+
return adapter.addAll(action.books, state);
4535
}),
4636
on(BooksApiActions.bookCreated, (state, action) => {
47-
return {
48-
collection: createBook(state.collection, action.book),
37+
return adapter.addOne(action.book, {
38+
...state,
4939
activeBookId: null
50-
};
40+
});
5141
}),
5242
on(BooksApiActions.bookUpdated, (state, action) => {
53-
return {
54-
collection: updateBook(state.collection, action.book),
55-
activeBookId: null
56-
};
43+
return adapter.updateOne(
44+
{ id: action.book.id, changes: action.book },
45+
{
46+
...state,
47+
activeBookId: null
48+
}
49+
);
5750
}),
5851
on(BooksApiActions.bookDeleted, (state, action) => {
59-
return {
60-
...state,
61-
collection: deleteBook(state.collection, action.bookId)
62-
};
52+
return adapter.removeOne(action.bookId, state);
6353
})
6454
);
6555

6656
export function reducer(state: State | undefined, action: Action) {
6757
return booksReducer(state, action);
6858
}
6959

70-
export const selectAll = (state: State) => state.collection;
60+
export const { selectAll, selectEntities } = adapter.getSelectors();
7161
export const selectActiveBookId = (state: State) => state.activeBookId;
7262
export const selectActiveBook = createSelector(
73-
selectAll,
63+
selectEntities,
7464
selectActiveBookId,
75-
(books, activeBookId) => books.find(book => book.id === activeBookId) || null
65+
(booksEntities, activeBookId) => {
66+
return activeBookId ? booksEntities[activeBookId]! : null;
67+
}
7668
);
7769
export const selectEarningsTotals = createSelector(
7870
selectAll,

0 commit comments

Comments
 (0)