Total Complexity | 2 |
Complexity/F | 0 |
Lines of Code | 86 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { combineReducers, Reducer } from "redux"; |
||
2 | import { Skill } from "../../models/types"; |
||
3 | import { |
||
4 | SkillAction, |
||
5 | FETCH_SKILLS_STARTED, |
||
6 | FETCH_SKILLS_SUCCEEDED, |
||
7 | FETCH_SKILLS_FAILED, |
||
8 | } from "./skillActions"; |
||
9 | import { mapToObject, getId } from "../../helpers/queries"; |
||
10 | |||
11 | interface EntityState { |
||
12 | skills: { |
||
13 | byId: { |
||
14 | [id: number]: Skill; |
||
15 | }; |
||
16 | }; |
||
17 | } |
||
18 | |||
19 | interface UiState { |
||
20 | updating: boolean; |
||
21 | } |
||
22 | |||
23 | export interface SkillState { |
||
24 | entities: EntityState; |
||
25 | ui: UiState; |
||
26 | } |
||
27 | |||
28 | const initEntityState = (): EntityState => ({ |
||
29 | skills: { |
||
30 | byId: {}, |
||
31 | }, |
||
32 | }); |
||
33 | |||
34 | const initUiState = (): UiState => ({ updating: false }); |
||
35 | |||
36 | export const initState = (): SkillState => ({ |
||
37 | entities: initEntityState(), |
||
38 | ui: initUiState(), |
||
39 | }); |
||
40 | |||
41 | const entityReducer = ( |
||
42 | state = initEntityState(), |
||
43 | action: SkillAction, |
||
44 | ): EntityState => { |
||
45 | switch (action.type) { |
||
46 | case FETCH_SKILLS_SUCCEEDED: |
||
47 | return { |
||
48 | ...state, |
||
49 | skills: { |
||
50 | byId: mapToObject(action.payload, getId), |
||
51 | }, |
||
52 | }; |
||
53 | default: |
||
54 | return state; |
||
55 | } |
||
56 | }; |
||
57 | |||
58 | const uiReducer = (state = initUiState(), action: SkillAction): UiState => { |
||
59 | switch (action.type) { |
||
60 | case FETCH_SKILLS_STARTED: |
||
61 | return { |
||
62 | ...state, |
||
63 | updating: true, |
||
64 | }; |
||
65 | case FETCH_SKILLS_SUCCEEDED: |
||
66 | return { |
||
67 | ...state, |
||
68 | updating: false, |
||
69 | }; |
||
70 | case FETCH_SKILLS_FAILED: |
||
71 | return { |
||
72 | ...state, |
||
73 | updating: false, |
||
74 | }; |
||
75 | default: |
||
76 | return state; |
||
77 | } |
||
78 | }; |
||
79 | |||
80 | export const skillReducer: Reducer<SkillState> = combineReducers({ |
||
81 | entities: entityReducer, |
||
82 | ui: uiReducer, |
||
83 | }); |
||
84 | |||
85 | export default skillReducer; |
||
86 |