Total Complexity | 1 |
Complexity/F | 0 |
Lines of Code | 44 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { User } from "../../models/types"; |
||
2 | import { |
||
3 | UserAction, |
||
4 | FETCH_USER_SUCCEEDED, |
||
5 | FETCH_ALL_USERS_SUCCEEDED, |
||
6 | } from "./userActions"; |
||
7 | import { mapToObject, getId } from "../../helpers/queries"; |
||
8 | |||
9 | export interface UserState { |
||
10 | usersById: { |
||
11 | [id: number]: User; |
||
12 | }; |
||
13 | } |
||
14 | |||
15 | export const initUserState = (): UserState => ({ |
||
16 | usersById: {}, |
||
17 | }); |
||
18 | |||
19 | export const userReducer = ( |
||
20 | state = initUserState(), |
||
21 | action: UserAction, |
||
22 | ): UserState => { |
||
23 | switch (action.type) { |
||
24 | case FETCH_USER_SUCCEEDED: |
||
25 | return { |
||
26 | ...state, |
||
27 | usersById: { |
||
28 | ...state.usersById, |
||
29 | [action.payload.id]: action.payload, |
||
30 | }, |
||
31 | }; |
||
32 | case FETCH_ALL_USERS_SUCCEEDED: |
||
33 | return { |
||
34 | ...state, |
||
35 | usersById: { |
||
36 | ...state.usersById, |
||
37 | ...mapToObject(action.payload, getId), |
||
38 | }, |
||
39 | }; |
||
40 | default: |
||
41 | return state; |
||
42 | } |
||
43 | }; |
||
44 |