Passed
Push — dependabot/npm_and_yarn/dev/sa... ( 003e03...a9167e )
by Yonathan
06:21 queued 12s
created

resources/assets/js/store/HrAdvisor/hrAdvisorReducer.ts   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 181
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 137
c 0
b 0
f 0
dl 0
loc 181
rs 10
mnd 4
bc 4
fnc 0
bpm 0
cpm 0
noi 0
1
/* eslint-disable @typescript-eslint/camelcase */
2
import { combineReducers } from "redux";
3
import { HrAdvisor } from "../../models/types";
4
import {
5
  HrAdvisorAction,
6
  GET_HR_ADVISOR_SUCCEEDED,
7
  CLAIM_JOB_SUCCEEDED,
8
  CLAIM_JOB_FAILED,
9
  CLAIM_JOB_STARTED,
10
  UNCLAIM_JOB_STARTED,
11
  GET_HR_ADVISOR_STARTED,
12
  GET_HR_ADVISOR_FAILED,
13
  UNCLAIM_JOB_SUCCEEDED,
14
  UNCLAIM_JOB_FAILED,
15
} from "./hrAdvisorActions";
16
17
import uniq from "lodash/uniq";
18
19
export interface EntityState {
20
  hrAdvisors: {
21
    byId: {
22
      [id: number]: HrAdvisor;
23
    };
24
  };
25
}
26
export interface UiState {
27
  hrAdvisorUpdating: {
28
    [id: number]: boolean;
29
  };
30
  jobClaimUpdating: {
31
    [hrAdvisorId: number]: {
32
      [jobId: number]: boolean;
33
    };
34
  };
35
}
36
37
export interface HrAdvisorState {
38
  entities: EntityState;
39
  ui: UiState;
40
}
41
42
export const initEntities = (): EntityState => ({
43
  hrAdvisors: { byId: {} },
44
});
45
46
export const initUi = (): UiState => ({
47
  hrAdvisorUpdating: {},
48
  jobClaimUpdating: {},
49
});
50
51
export const initState = (): HrAdvisorState => ({
52
  entities: initEntities(),
53
  ui: initUi(),
54
});
55
56
const addClaimedJob = (advisor: HrAdvisor, jobId: number): HrAdvisor => ({
57
  ...advisor,
58
  claimed_job_ids: uniq([jobId, ...advisor.claimed_job_ids]).sort(),
59
});
60
61
const removeClaimedJob = (advisor: HrAdvisor, jobId: number): HrAdvisor => ({
62
  ...advisor,
63
  claimed_job_ids: advisor.claimed_job_ids.filter(id => id !== jobId),
64
});
65
66
export const entitiesReducer = (
67
  state = initEntities(),
68
  action: HrAdvisorAction,
69
): EntityState => {
70
  switch (action.type) {
71
    case GET_HR_ADVISOR_SUCCEEDED:
72
      return {
73
        ...state,
74
        hrAdvisors: {
75
          byId: {
76
            ...state.hrAdvisors.byId,
77
            [action.payload.id]: action.payload,
78
          },
79
        },
80
      };
81
    case CLAIM_JOB_SUCCEEDED:
82
      return {
83
        ...state,
84
        hrAdvisors: {
85
          byId: {
86
            ...state.hrAdvisors.byId,
87
            // Only modify the state if the advisor already exists
88
            ...(state.hrAdvisors.byId[action.meta.hrAdvisorId]
89
              ? {
90
                  [action.meta.hrAdvisorId]: addClaimedJob(
91
                    state.hrAdvisors.byId[action.meta.hrAdvisorId],
92
                    action.meta.jobId,
93
                  ),
94
                }
95
              : {}),
96
          },
97
        },
98
      };
99
    case UNCLAIM_JOB_SUCCEEDED:
100
      return {
101
        ...state,
102
        hrAdvisors: {
103
          byId: {
104
            ...state.hrAdvisors.byId,
105
            // Only modify the state if the advisor already exists
106
            ...(state.hrAdvisors.byId[action.meta.hrAdvisorId]
107
              ? {
108
                  [action.meta.hrAdvisorId]: removeClaimedJob(
109
                    state.hrAdvisors.byId[action.meta.hrAdvisorId],
110
                    action.meta.jobId,
111
                  ),
112
                }
113
              : {}),
114
          },
115
        },
116
      };
117
    default:
118
      return state;
119
  }
120
};
121
122
export const uiReducer = (
123
  state = initUi(),
124
  action: HrAdvisorAction,
125
): UiState => {
126
  switch (action.type) {
127
    case GET_HR_ADVISOR_STARTED:
128
      return {
129
        ...state,
130
        hrAdvisorUpdating: {
131
          ...state.hrAdvisorUpdating,
132
          [action.meta.id]: true,
133
        },
134
      };
135
    case GET_HR_ADVISOR_FAILED:
136
    case GET_HR_ADVISOR_SUCCEEDED:
137
      return {
138
        ...state,
139
        hrAdvisorUpdating: {
140
          ...state.hrAdvisorUpdating,
141
          [action.meta.id]: false,
142
        },
143
      };
144
    case CLAIM_JOB_STARTED:
145
    case UNCLAIM_JOB_STARTED:
146
      return {
147
        ...state,
148
        jobClaimUpdating: {
149
          ...state.jobClaimUpdating,
150
          [action.meta.hrAdvisorId]: {
151
            ...state.jobClaimUpdating[action.meta.hrAdvisorId],
152
            [action.meta.jobId]: true,
153
          },
154
        },
155
      };
156
    case CLAIM_JOB_SUCCEEDED:
157
    case UNCLAIM_JOB_SUCCEEDED:
158
    case CLAIM_JOB_FAILED:
159
    case UNCLAIM_JOB_FAILED:
160
      return {
161
        ...state,
162
        jobClaimUpdating: {
163
          ...state.jobClaimUpdating,
164
          [action.meta.hrAdvisorId]: {
165
            ...state.jobClaimUpdating[action.meta.hrAdvisorId],
166
            [action.meta.jobId]: false,
167
          },
168
        },
169
      };
170
    default:
171
      return state;
172
  }
173
};
174
175
export const hrAdvisorReducer = combineReducers({
176
  entities: entitiesReducer,
177
  ui: uiReducer,
178
});
179
180
export default hrAdvisorReducer;
181