-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.ts
31 lines (26 loc) · 1.27 KB
/
action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { AsyncThunkPayloadCreator, PayloadAction } from '@reduxjs/toolkit';
import { getSurvey, submitSurvey } from 'adapters/Survey';
import { DeserializableResponse, deserialize } from 'helpers/deserializer';
import { JSONObject } from 'helpers/json';
import { QuestionRequest, SurveySubmitRequest } from 'types/request/surveySubmitRequest';
import { Survey } from 'types/survey';
import { SurveyState, initialState } from '.';
export const surveyReducers = {
fillAnswers: (state: SurveyState, action: PayloadAction<QuestionRequest>) => {
const newQuestionRequests = state.questionRequests.filter((question: QuestionRequest) => question.id !== action.payload.id);
state.questionRequests = [...newQuestionRequests, action.payload];
return state;
},
resetState: (state: SurveyState) => {
state = initialState;
return state;
},
};
export const getSurveyThunkCreator: AsyncThunkPayloadCreator<Survey, string, JSONObject> = async (surveyId: string) => {
return getSurvey(surveyId).then((response: DeserializableResponse) => deserialize<Survey>(response.data, response.included));
};
export const submitSurveyThunkCreator: AsyncThunkPayloadCreator<void, SurveySubmitRequest, JSONObject> = async (
surveySubmitRequest
) => {
await submitSurvey(surveySubmitRequest);
};