Passed
Push — feature/connect-application-st... ( a6f23b...9d3dc6 )
by Chris
04:02
created

resources/assets/js/store/RatingGuideQuestion/ratingGuideQuestionActions.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 311
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 245
dl 0
loc 311
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { ThunkAction, ThunkDispatch } from "redux-thunk";
2
import { AnyAction } from "redux";
3
import { Action } from "../createAction";
4
import {
5
  updateRatingGuideQuestion as updateRatingGuideQuestionApi,
6
  createRatingGuideQuestion as createRatingGuideQuestionApi,
7
  deleteRatingGuideQuestion as deleteRatingGuideQuestionApi,
8
} from "../../api/ratingGuide";
9
import { RatingGuideQuestion } from "../../models/types";
10
import { FailedAction } from "../asyncAction";
11
import { RootState } from "../store";
12
import { tempRatingGuideQuestionIsSaving } from "./ratingGuideQuestionSelectors";
13
14
/** Action for editing Rating Guide Q&A (without saving to server) */
15
export const EDIT_RATING_GUIDE_QUESTION = "EDIT_RATING_GUIDE_QUESTION";
16
export type EditRatingGuideQuestionAction = Action<
17
  typeof EDIT_RATING_GUIDE_QUESTION,
18
  { ratingGuideQuestion: RatingGuideQuestion }
19
>;
20
export const editRatingGuideQuestion = (
21
  ratingGuideQuestion: RatingGuideQuestion,
22
): EditRatingGuideQuestionAction => ({
23
  type: EDIT_RATING_GUIDE_QUESTION,
24
  payload: { ratingGuideQuestion },
25
});
26
27
/** Actions for manipulating Temp Rating Guide Questions */
28
export const CREATE_TEMP_RATING_GUIDE_QUESTION =
29
  "CREATE_TEMP_RATING_GUIDE_QUESTION";
30
export const EDIT_TEMP_RATING_GUIDE_QUESTION =
31
  "EDIT_TEMP_RATING_GUIDE_QUESTION";
32
export const DELETE_TEMP_RATING_GUIDE_QUESTION =
33
  "DELETE_TEMP_RATING_GUIDE_QUESTION";
34
35
export type CreateTempRatingGuideQuestionAction = Action<
36
  typeof CREATE_TEMP_RATING_GUIDE_QUESTION,
37
  {
38
    jobPosterId: number;
39
    assessmentTypeId: number;
40
    question: string | null;
41
  }
42
>;
43
44
export type EditTempRatingGuideQuestionAction = Action<
45
  typeof EDIT_TEMP_RATING_GUIDE_QUESTION,
46
  { ratingGuideQuestion: RatingGuideQuestion }
47
>;
48
49
export type DeleteTempRatingGuideQuestionAction = Action<
50
  typeof DELETE_TEMP_RATING_GUIDE_QUESTION,
51
  { id: number }
52
>;
53
54
export const createTempRatingGuideQuestion = (
55
  jobPosterId: number,
56
  assessmentTypeId: number,
57
  question: string | null,
58
): CreateTempRatingGuideQuestionAction => ({
59
  type: CREATE_TEMP_RATING_GUIDE_QUESTION,
60
  payload: { jobPosterId, assessmentTypeId, question },
61
});
62
63
export const editTempRatingGuideQuestion = (
64
  ratingGuideQuestion: RatingGuideQuestion,
65
): EditTempRatingGuideQuestionAction => ({
66
  type: EDIT_TEMP_RATING_GUIDE_QUESTION,
67
  payload: { ratingGuideQuestion },
68
});
69
70
export const deleteTempRatingGuideQuestion = (
71
  id: number,
72
): DeleteTempRatingGuideQuestionAction => ({
73
  type: DELETE_TEMP_RATING_GUIDE_QUESTION,
74
  payload: { id },
75
});
76
77
/** Updating Rating Guide Questions on Server */
78
79
export const UPDATE_RATING_GUIDE_QUESTION_STARTED =
80
  "UPDATE_RATING_GUIDE_QUESTION_STARTED";
81
export const UPDATE_RATING_GUIDE_QUESTION_SUCCEEDED =
82
  "UPDATE_RATING_GUIDE_QUESTION_SUCCEEDED";
83
export const UPDATE_RATING_GUIDE_QUESTION_FAILED =
84
  "UPDATE_RATING_GUIDE_QUESTION_FAILED";
85
86
export type UpdateRatingGuideQuestionStartedAction = Action<
87
  typeof UPDATE_RATING_GUIDE_QUESTION_STARTED,
88
  { ratingGuideQuestion: RatingGuideQuestion }
89
>;
90
export type UpdateRatingGuideQuestionSucceededAction = Action<
91
  typeof UPDATE_RATING_GUIDE_QUESTION_SUCCEEDED,
92
  { ratingGuideQuestion: RatingGuideQuestion }
93
>;
94
export type UpdateRatingGuideQuestionFailedAction = FailedAction<
95
  typeof UPDATE_RATING_GUIDE_QUESTION_FAILED,
96
  RatingGuideQuestion
97
>;
98
99
export const updateRatingGuideQuestionStarted = (
100
  ratingGuideQuestion: RatingGuideQuestion,
101
): UpdateRatingGuideQuestionStartedAction => {
102
  return {
103
    type: UPDATE_RATING_GUIDE_QUESTION_STARTED,
104
    payload: {
105
      ratingGuideQuestion,
106
    },
107
  };
108
};
109
export const updateRatingGuideQuestionSucceeded = (
110
  ratingGuideQuestion: RatingGuideQuestion,
111
): UpdateRatingGuideQuestionSucceededAction => {
112
  return {
113
    type: UPDATE_RATING_GUIDE_QUESTION_SUCCEEDED,
114
    payload: {
115
      ratingGuideQuestion,
116
    },
117
  };
118
};
119
export const updateRatingGuideQuestionFailed = (
120
  ratingGuideQuestion: RatingGuideQuestion,
121
  error: Error,
122
): UpdateRatingGuideQuestionFailedAction => ({
123
  type: UPDATE_RATING_GUIDE_QUESTION_FAILED,
124
  payload: error,
125
  meta: ratingGuideQuestion,
126
  error: true,
127
});
128
export const updateRatingGuideQuestion = (
129
  ratingGuideQuestion: RatingGuideQuestion,
130
): ThunkAction<void, any, any, RatingGuideQuestionAction> => {
131
  return (
132
    dispatch: ThunkDispatch<any, undefined, RatingGuideQuestionAction>,
133
  ): void => {
134
    dispatch(updateRatingGuideQuestionStarted(ratingGuideQuestion));
135
    updateRatingGuideQuestionApi(ratingGuideQuestion)
136
      .then((updatedRatingGuideQuestion): void => {
137
        dispatch(
138
          updateRatingGuideQuestionSucceeded(updatedRatingGuideQuestion),
139
        );
140
      })
141
      .catch((error: Error): void => {
142
        dispatch(updateRatingGuideQuestionFailed(ratingGuideQuestion, error));
143
      });
144
  };
145
};
146
147
/** Deleting RatingGuideQuestions on server */
148
149
export const DELETE_RATING_GUIDE_QUESTION_STARTED =
150
  "DELETE_RATING_GUIDE_QUESTION_STARTED";
151
export const DELETE_RATING_GUIDE_QUESTION_SUCCEEDED =
152
  "DELETE_RATING_GUIDE_QUESTION_SUCCEEDED";
153
export const DELETE_RATING_GUIDE_QUESTION_FAILED =
154
  "DELETE_RATING_GUIDE_QUESTION_FAILED";
155
156
export type DeleteRatingGuideQuestionStartedAction = Action<
157
  typeof DELETE_RATING_GUIDE_QUESTION_STARTED,
158
  { id: number }
159
>;
160
export type DeleteRatingGuideQuestionSucceededAction = Action<
161
  typeof DELETE_RATING_GUIDE_QUESTION_SUCCEEDED,
162
  { id: number }
163
>;
164
export type DeleteRatingGuideQuestionFailedAction = FailedAction<
165
  typeof DELETE_RATING_GUIDE_QUESTION_FAILED,
166
  { id: number }
167
>;
168
169
export const deleteRatingGuideQuestionStarted = (
170
  id: number,
171
): DeleteRatingGuideQuestionStartedAction => {
172
  return {
173
    type: DELETE_RATING_GUIDE_QUESTION_STARTED,
174
    payload: {
175
      id,
176
    },
177
  };
178
};
179
export const deleteRatingGuideQuestionSucceeded = (
180
  id: number,
181
): DeleteRatingGuideQuestionSucceededAction => {
182
  return {
183
    type: DELETE_RATING_GUIDE_QUESTION_SUCCEEDED,
184
    payload: {
185
      id,
186
    },
187
  };
188
};
189
export const deleteRatingGuideQuestionFailed = (
190
  id: number,
191
  error: Error,
192
): DeleteRatingGuideQuestionFailedAction => ({
193
  type: DELETE_RATING_GUIDE_QUESTION_FAILED,
194
  payload: error,
195
  meta: { id },
196
  error: true,
197
});
198
export const deleteRatingGuideQuestion = (
199
  id: number,
200
): ThunkAction<void, any, any, RatingGuideQuestionAction> => {
201
  return (
202
    dispatch: ThunkDispatch<any, undefined, RatingGuideQuestionAction>,
203
  ): void => {
204
    dispatch(deleteRatingGuideQuestionStarted(id));
205
    deleteRatingGuideQuestionApi(id)
206
      .then((): void => {
207
        dispatch(deleteRatingGuideQuestionSucceeded(id));
208
      })
209
      .catch((error: Error): void => {
210
        dispatch(deleteRatingGuideQuestionFailed(id, error));
211
      });
212
  };
213
};
214
215
/** Actions for saving a NEW ratingGuideQuestion to server */
216
export const STORE_NEW_RATING_GUIDE_QUESTION_STARTED =
217
  "STORE_RATING_GUIDE_QUESTION_STARTED";
218
export const STORE_NEW_RATING_GUIDE_QUESTION_SUCCEEDED =
219
  "STORE_RATING_GUIDE_QUESTION_SUCCEEDED";
220
export const STORE_NEW_RATING_GUIDE_QUESTION_FAILED =
221
  "STORE_RATING_GUIDE_QUESTION_FAILED";
222
223
export type StoreNewRatingGuideQuestionStartedAction = Action<
224
  typeof STORE_NEW_RATING_GUIDE_QUESTION_STARTED,
225
  { ratingGuideQuestion: RatingGuideQuestion }
226
>;
227
export type StoreNewRatingGuideQuestionSucceededAction = Action<
228
  typeof STORE_NEW_RATING_GUIDE_QUESTION_SUCCEEDED,
229
  {
230
    ratingGuideQuestion: RatingGuideQuestion;
231
    oldRatingGuideQuestion: RatingGuideQuestion;
232
  }
233
>;
234
export type StoreNewRatingGuideQuestionFailedAction = FailedAction<
235
  typeof STORE_NEW_RATING_GUIDE_QUESTION_FAILED,
236
  RatingGuideQuestion
237
>;
238
239
export const storeNewRatingGuideQuestionStarted = (
240
  ratingGuideQuestion: RatingGuideQuestion,
241
): StoreNewRatingGuideQuestionStartedAction => {
242
  return {
243
    type: STORE_NEW_RATING_GUIDE_QUESTION_STARTED,
244
    payload: {
245
      ratingGuideQuestion,
246
    },
247
  };
248
};
249
export const storeNewRatingGuideQuestionSucceeded = (
250
  ratingGuideQuestion: RatingGuideQuestion,
251
  oldRatingGuideQuestion: RatingGuideQuestion,
252
): StoreNewRatingGuideQuestionSucceededAction => {
253
  return {
254
    type: STORE_NEW_RATING_GUIDE_QUESTION_SUCCEEDED,
255
    payload: {
256
      ratingGuideQuestion,
257
      oldRatingGuideQuestion,
258
    },
259
  };
260
};
261
export const storeNewRatingGuideQuestionFailed = (
262
  oldRatingGuideQuestion: RatingGuideQuestion,
263
  error: Error,
264
): StoreNewRatingGuideQuestionFailedAction => ({
265
  type: STORE_NEW_RATING_GUIDE_QUESTION_FAILED,
266
  payload: error,
267
  meta: oldRatingGuideQuestion,
268
  error: true,
269
});
270
export const storeNewRatingGuideQuestion = (
271
  ratingGuideQuestion: RatingGuideQuestion,
272
): ThunkAction<void, RootState, {}, AnyAction> => {
273
  return (dispatch, getState): void => {
274
    // If a store request for this resource is already in progress, we cannot submit
275
    //  a second one. We can only edit the temp version.
276
    if (tempRatingGuideQuestionIsSaving(getState(), ratingGuideQuestion.id)) {
277
      dispatch(editTempRatingGuideQuestion(ratingGuideQuestion));
278
      return;
279
    }
280
281
    dispatch(storeNewRatingGuideQuestionStarted(ratingGuideQuestion));
282
    createRatingGuideQuestionApi(ratingGuideQuestion)
283
      .then((updatedRatingGuideQuestion): void => {
284
        dispatch(
285
          storeNewRatingGuideQuestionSucceeded(
286
            updatedRatingGuideQuestion,
287
            ratingGuideQuestion,
288
          ),
289
        );
290
      })
291
      .catch((error: Error): void => {
292
        dispatch(storeNewRatingGuideQuestionFailed(ratingGuideQuestion, error));
293
      });
294
  };
295
};
296
297
export type RatingGuideQuestionAction =
298
  | EditRatingGuideQuestionAction
299
  | UpdateRatingGuideQuestionStartedAction
300
  | UpdateRatingGuideQuestionSucceededAction
301
  | UpdateRatingGuideQuestionFailedAction
302
  | DeleteRatingGuideQuestionStartedAction
303
  | DeleteRatingGuideQuestionSucceededAction
304
  | DeleteRatingGuideQuestionFailedAction
305
  | CreateTempRatingGuideQuestionAction
306
  | EditTempRatingGuideQuestionAction
307
  | DeleteTempRatingGuideQuestionAction
308
  | StoreNewRatingGuideQuestionStartedAction
309
  | StoreNewRatingGuideQuestionSucceededAction
310
  | StoreNewRatingGuideQuestionFailedAction;
311