Passed
Push — task/ci-test-actions ( 48435f...69d712 )
by Tristan
04:29
created

resources/assets/js/store/RatingGuideAnswer/ratingGuideAnswerActions.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 312
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 246
dl 0
loc 312
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
  updateRatingGuideAnswer as updateRatingGuideAnswerApi,
6
  createRatingGuideAnswer as createRatingGuideAnswerApi,
7
  deleteRatingGuideAnswer as deleteRatingGuideAnswerApi,
8
} from "../../api/ratingGuide";
9
import { RatingGuideAnswer } from "../../models/types";
10
import { FailedAction } from "../asyncAction";
11
import { RootState } from "../store";
12
import { tempRatingGuideAnswerIsSaving } from "./ratingGuideAnswerSelectors";
13
14
/** Action for editing Rating Guide Q&A (without saving to server) */
15
export const EDIT_RATING_GUIDE_ANSWER = "EDIT_RATING_GUIDE_ANSWER";
16
export type EditRatingGuideAnswerAction = Action<
17
  typeof EDIT_RATING_GUIDE_ANSWER,
18
  { ratingGuideAnswer: RatingGuideAnswer }
19
>;
20
export const editRatingGuideAnswer = (
21
  ratingGuideAnswer: RatingGuideAnswer,
22
): EditRatingGuideAnswerAction => ({
23
  type: EDIT_RATING_GUIDE_ANSWER,
24
  payload: { ratingGuideAnswer },
25
});
26
27
/** Actions for manipulating Temp Rating Guide Answers */
28
export const CREATE_TEMP_RATING_GUIDE_ANSWER =
29
  "CREATE_TEMP_RATING_GUIDE_ANSWER";
30
export const EDIT_TEMP_RATING_GUIDE_ANSWER = "EDIT_TEMP_RATING_GUIDE_ANSWER";
31
export const DELETE_TEMP_RATING_GUIDE_ANSWER =
32
  "DELETE_TEMP_RATING_GUIDE_ANSWER";
33
34
export type CreateTempRatingGuideAnswerAction = Action<
35
  typeof CREATE_TEMP_RATING_GUIDE_ANSWER,
36
  {
37
    ratingGuideQuestionId: number;
38
    criterionId: number | null;
39
    expectedAnswer: string | null;
40
  }
41
>;
42
43
export type EditTempRatingGuideAnswerAction = Action<
44
  typeof EDIT_TEMP_RATING_GUIDE_ANSWER,
45
  { ratingGuideAnswer: RatingGuideAnswer }
46
>;
47
48
export type DeleteTempRatingGuideAnswerAction = Action<
49
  typeof DELETE_TEMP_RATING_GUIDE_ANSWER,
50
  { id: number }
51
>;
52
53
export const createTempRatingGuideAnswer = (
54
  ratingGuideQuestionId: number,
55
  criterionId: number | null,
56
  expectedAnswer: string | null,
57
): CreateTempRatingGuideAnswerAction => ({
58
  type: CREATE_TEMP_RATING_GUIDE_ANSWER,
59
  payload: {
60
    ratingGuideQuestionId,
61
    criterionId,
62
    expectedAnswer,
63
  },
64
});
65
66
export const editTempRatingGuideAnswer = (
67
  ratingGuideAnswer: RatingGuideAnswer,
68
): EditTempRatingGuideAnswerAction => ({
69
  type: EDIT_TEMP_RATING_GUIDE_ANSWER,
70
  payload: { ratingGuideAnswer },
71
});
72
73
export const deleteTempRatingGuideAnswer = (
74
  id: number,
75
): DeleteTempRatingGuideAnswerAction => ({
76
  type: DELETE_TEMP_RATING_GUIDE_ANSWER,
77
  payload: { id },
78
});
79
80
/** Updating Rating Guide Answers on Server */
81
82
export const UPDATE_RATING_GUIDE_ANSWER_STARTED =
83
  "UPDATE_RATING_GUIDE_ANSWER_STARTED";
84
export const UPDATE_RATING_GUIDE_ANSWER_SUCCEEDED =
85
  "UPDATE_RATING_GUIDE_ANSWER_SUCCEEDED";
86
export const UPDATE_RATING_GUIDE_ANSWER_FAILED =
87
  "UPDATE_RATING_GUIDE_ANSWER_FAILED";
88
89
export type UpdateRatingGuideAnswerStartedAction = Action<
90
  typeof UPDATE_RATING_GUIDE_ANSWER_STARTED,
91
  { ratingGuideAnswer: RatingGuideAnswer }
92
>;
93
export type UpdateRatingGuideAnswerSucceededAction = Action<
94
  typeof UPDATE_RATING_GUIDE_ANSWER_SUCCEEDED,
95
  { ratingGuideAnswer: RatingGuideAnswer }
96
>;
97
export type UpdateRatingGuideAnswerFailedAction = FailedAction<
98
  typeof UPDATE_RATING_GUIDE_ANSWER_FAILED,
99
  RatingGuideAnswer
100
>;
101
102
export const updateRatingGuideAnswerStarted = (
103
  ratingGuideAnswer: RatingGuideAnswer,
104
): UpdateRatingGuideAnswerStartedAction => {
105
  return {
106
    type: UPDATE_RATING_GUIDE_ANSWER_STARTED,
107
    payload: {
108
      ratingGuideAnswer,
109
    },
110
  };
111
};
112
export const updateRatingGuideAnswerSucceeded = (
113
  ratingGuideAnswer: RatingGuideAnswer,
114
): UpdateRatingGuideAnswerSucceededAction => {
115
  return {
116
    type: UPDATE_RATING_GUIDE_ANSWER_SUCCEEDED,
117
    payload: {
118
      ratingGuideAnswer,
119
    },
120
  };
121
};
122
export const updateRatingGuideAnswerFailed = (
123
  ratingGuideAnswer: RatingGuideAnswer,
124
  error: Error,
125
): UpdateRatingGuideAnswerFailedAction => ({
126
  type: UPDATE_RATING_GUIDE_ANSWER_FAILED,
127
  payload: error,
128
  meta: ratingGuideAnswer,
129
  error: true,
130
});
131
export const updateRatingGuideAnswer = (
132
  ratingGuideAnswer: RatingGuideAnswer,
133
): ThunkAction<void, any, any, RatingGuideAnswerAction> => {
134
  return (
135
    dispatch: ThunkDispatch<any, undefined, RatingGuideAnswerAction>,
136
  ): void => {
137
    dispatch(updateRatingGuideAnswerStarted(ratingGuideAnswer));
138
    updateRatingGuideAnswerApi(ratingGuideAnswer)
139
      .then((updatedRatingGuideAnswer): void => {
140
        dispatch(updateRatingGuideAnswerSucceeded(updatedRatingGuideAnswer));
141
      })
142
      .catch((error: Error): void => {
143
        dispatch(updateRatingGuideAnswerFailed(ratingGuideAnswer, error));
144
      });
145
  };
146
};
147
148
/** Deleting RatingGuideAnswers on server */
149
150
export const DELETE_RATING_GUIDE_ANSWER_STARTED =
151
  "DELETE_RATING_GUIDE_ANSWER_STARTED";
152
export const DELETE_RATING_GUIDE_ANSWER_SUCCEEDED =
153
  "DELETE_RATING_GUIDE_ANSWER_SUCCEEDED";
154
export const DELETE_RATING_GUIDE_ANSWER_FAILED =
155
  "DELETE_RATING_GUIDE_ANSWER_FAILED";
156
157
export type DeleteRatingGuideAnswerStartedAction = Action<
158
  typeof DELETE_RATING_GUIDE_ANSWER_STARTED,
159
  { id: number }
160
>;
161
export type DeleteRatingGuideAnswerSucceededAction = Action<
162
  typeof DELETE_RATING_GUIDE_ANSWER_SUCCEEDED,
163
  { id: number }
164
>;
165
export type DeleteRatingGuideAnswerFailedAction = FailedAction<
166
  typeof DELETE_RATING_GUIDE_ANSWER_FAILED,
167
  { id: number }
168
>;
169
170
export const deleteRatingGuideAnswerStarted = (
171
  id: number,
172
): DeleteRatingGuideAnswerStartedAction => {
173
  return {
174
    type: DELETE_RATING_GUIDE_ANSWER_STARTED,
175
    payload: {
176
      id,
177
    },
178
  };
179
};
180
export const deleteRatingGuideAnswerSucceeded = (
181
  id: number,
182
): DeleteRatingGuideAnswerSucceededAction => {
183
  return {
184
    type: DELETE_RATING_GUIDE_ANSWER_SUCCEEDED,
185
    payload: {
186
      id,
187
    },
188
  };
189
};
190
export const deleteRatingGuideAnswerFailed = (
191
  id: number,
192
  error: Error,
193
): DeleteRatingGuideAnswerFailedAction => ({
194
  type: DELETE_RATING_GUIDE_ANSWER_FAILED,
195
  payload: error,
196
  meta: { id },
197
  error: true,
198
});
199
export const deleteRatingGuideAnswer = (
200
  id: number,
201
): ThunkAction<void, any, any, RatingGuideAnswerAction> => {
202
  return (
203
    dispatch: ThunkDispatch<any, undefined, RatingGuideAnswerAction>,
204
  ): void => {
205
    dispatch(deleteRatingGuideAnswerStarted(id));
206
    deleteRatingGuideAnswerApi(id)
207
      .then((): void => {
208
        dispatch(deleteRatingGuideAnswerSucceeded(id));
209
      })
210
      .catch((error: Error): void => {
211
        dispatch(deleteRatingGuideAnswerFailed(id, error));
212
      });
213
  };
214
};
215
216
/** Actions for saving a NEW ratingGuideAnswer to server */
217
export const STORE_NEW_RATING_GUIDE_ANSWER_STARTED =
218
  "STORE_RATING_GUIDE_ANSWER_STARTED";
219
export const STORE_NEW_RATING_GUIDE_ANSWER_SUCCEEDED =
220
  "STORE_RATING_GUIDE_ANSWER_SUCCEEDED";
221
export const STORE_NEW_RATING_GUIDE_ANSWER_FAILED =
222
  "STORE_RATING_GUIDE_ANSWER_FAILED";
223
224
export type StoreNewRatingGuideAnswerStartedAction = Action<
225
  typeof STORE_NEW_RATING_GUIDE_ANSWER_STARTED,
226
  { ratingGuideAnswer: RatingGuideAnswer }
227
>;
228
export type StoreNewRatingGuideAnswerSucceededAction = Action<
229
  typeof STORE_NEW_RATING_GUIDE_ANSWER_SUCCEEDED,
230
  {
231
    ratingGuideAnswer: RatingGuideAnswer;
232
    oldRatingGuideAnswer: RatingGuideAnswer;
233
  }
234
>;
235
export type StoreNewRatingGuideAnswerFailedAction = FailedAction<
236
  typeof STORE_NEW_RATING_GUIDE_ANSWER_FAILED,
237
  RatingGuideAnswer
238
>;
239
240
export const storeNewRatingGuideAnswerStarted = (
241
  ratingGuideAnswer: RatingGuideAnswer,
242
): StoreNewRatingGuideAnswerStartedAction => {
243
  return {
244
    type: STORE_NEW_RATING_GUIDE_ANSWER_STARTED,
245
    payload: {
246
      ratingGuideAnswer,
247
    },
248
  };
249
};
250
export const storeNewRatingGuideAnswerSucceeded = (
251
  ratingGuideAnswer: RatingGuideAnswer,
252
  oldRatingGuideAnswer: RatingGuideAnswer,
253
): StoreNewRatingGuideAnswerSucceededAction => {
254
  return {
255
    type: STORE_NEW_RATING_GUIDE_ANSWER_SUCCEEDED,
256
    payload: {
257
      ratingGuideAnswer,
258
      oldRatingGuideAnswer,
259
    },
260
  };
261
};
262
export const storeNewRatingGuideAnswerFailed = (
263
  oldRatingGuideAnswer: RatingGuideAnswer,
264
  error: Error,
265
): StoreNewRatingGuideAnswerFailedAction => ({
266
  type: STORE_NEW_RATING_GUIDE_ANSWER_FAILED,
267
  payload: error,
268
  meta: oldRatingGuideAnswer,
269
  error: true,
270
});
271
export const storeNewRatingGuideAnswer = (
272
  ratingGuideAnswer: RatingGuideAnswer,
273
): ThunkAction<void, RootState, {}, AnyAction> => {
274
  return (dispatch: ThunkDispatch<{}, {}, AnyAction>, getState): void => {
275
    // If a store request for this resource is already in progress, we cannot submit
276
    //  a second one. We can only edit the temp version.
277
    if (tempRatingGuideAnswerIsSaving(getState(), ratingGuideAnswer.id)) {
278
      dispatch(editTempRatingGuideAnswer(ratingGuideAnswer));
279
      return;
280
    }
281
282
    dispatch(storeNewRatingGuideAnswerStarted(ratingGuideAnswer));
283
    createRatingGuideAnswerApi(ratingGuideAnswer)
284
      .then((updatedRatingGuideAnswer): void => {
285
        dispatch(
286
          storeNewRatingGuideAnswerSucceeded(
287
            updatedRatingGuideAnswer,
288
            ratingGuideAnswer,
289
          ),
290
        );
291
      })
292
      .catch((error: Error): void => {
293
        dispatch(storeNewRatingGuideAnswerFailed(ratingGuideAnswer, error));
294
      });
295
  };
296
};
297
298
export type RatingGuideAnswerAction =
299
  | EditRatingGuideAnswerAction
300
  | UpdateRatingGuideAnswerStartedAction
301
  | UpdateRatingGuideAnswerSucceededAction
302
  | UpdateRatingGuideAnswerFailedAction
303
  | DeleteRatingGuideAnswerStartedAction
304
  | DeleteRatingGuideAnswerSucceededAction
305
  | DeleteRatingGuideAnswerFailedAction
306
  | CreateTempRatingGuideAnswerAction
307
  | EditTempRatingGuideAnswerAction
308
  | DeleteTempRatingGuideAnswerAction
309
  | StoreNewRatingGuideAnswerStartedAction
310
  | StoreNewRatingGuideAnswerSucceededAction
311
  | StoreNewRatingGuideAnswerFailedAction;
312