11import { createReducer , on , Action , createSelector } from "@ngrx/store" ;
2+ import { createEntityAdapter , EntityState } from "@ngrx/entity" ;
23import {
34 BookModel ,
45 calculateBooksGrossEarnings
56} from "src/app/shared/models/book.model" ;
67import { 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
2619export 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
6656export 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 ( ) ;
7161export const selectActiveBookId = ( state : State ) => state . activeBookId ;
7262export 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) ;
7769export const selectEarningsTotals = createSelector (
7870 selectAll ,
0 commit comments