1
1
import { createReducer , on , Action , createSelector } from "@ngrx/store" ;
2
+ import { createEntityAdapter , EntityState } from "@ngrx/entity" ;
2
3
import {
3
4
BookModel ,
4
5
calculateBooksGrossEarnings
5
6
} from "src/app/shared/models/book.model" ;
6
7
import { BooksPageActions , BooksApiActions } from "src/app/books/actions" ;
7
8
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 > {
18
10
activeBookId : string | null ;
19
11
}
20
12
21
- export const initialState : State = {
22
- collection : [ ] ,
13
+ export const adapter = createEntityAdapter < BookModel > ( ) ;
14
+
15
+ export const initialState : State = adapter . getInitialState ( {
23
16
activeBookId : null
24
- } ;
17
+ } ) ;
25
18
26
19
export const booksReducer = createReducer (
27
20
initialState ,
@@ -38,41 +31,40 @@ export const booksReducer = createReducer(
38
31
} ;
39
32
} ) ,
40
33
on ( BooksApiActions . booksLoaded , ( state , action ) => {
41
- return {
42
- ...state ,
43
- collection : action . books
44
- } ;
34
+ return adapter . addAll ( action . books , state ) ;
45
35
} ) ,
46
36
on ( BooksApiActions . bookCreated , ( state , action ) => {
47
- return {
48
- collection : createBook ( state . collection , action . book ) ,
37
+ return adapter . addOne ( action . book , {
38
+ ... state ,
49
39
activeBookId : null
50
- } ;
40
+ } ) ;
51
41
} ) ,
52
42
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
+ ) ;
57
50
} ) ,
58
51
on ( BooksApiActions . bookDeleted , ( state , action ) => {
59
- return {
60
- ...state ,
61
- collection : deleteBook ( state . collection , action . bookId )
62
- } ;
52
+ return adapter . removeOne ( action . bookId , state ) ;
63
53
} )
64
54
) ;
65
55
66
56
export function reducer ( state : State | undefined , action : Action ) {
67
57
return booksReducer ( state , action ) ;
68
58
}
69
59
70
- export const selectAll = ( state : State ) => state . collection ;
60
+ export const { selectAll, selectEntities } = adapter . getSelectors ( ) ;
71
61
export const selectActiveBookId = ( state : State ) => state . activeBookId ;
72
62
export const selectActiveBook = createSelector (
73
- selectAll ,
63
+ selectEntities ,
74
64
selectActiveBookId ,
75
- ( books , activeBookId ) => books . find ( book => book . id === activeBookId ) || null
65
+ ( booksEntities , activeBookId ) => {
66
+ return activeBookId ? booksEntities [ activeBookId ] ! : null ;
67
+ }
76
68
) ;
77
69
export const selectEarningsTotals = createSelector (
78
70
selectAll ,
0 commit comments