-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (54 loc) · 1.83 KB
/
index.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { QuestionRequest } from 'types/request/surveySubmitRequest';
import { Survey } from 'types/survey';
import { getSurveyThunkCreator, submitSurveyThunkCreator, surveyReducers } from './action';
export interface SurveyState {
survey?: Survey;
isLoading: boolean;
isError: boolean;
questionRequests: QuestionRequest[];
isSubmitSuccess: boolean;
}
export const initialState: SurveyState = {
isLoading: false,
isError: false,
questionRequests: [],
isSubmitSuccess: false,
};
export const getSurveyAsyncThunk = createAsyncThunk('survey/getSurvey', getSurveyThunkCreator);
export const submitSurveyAsyncThunk = createAsyncThunk('survey/submitSurvey', submitSurveyThunkCreator);
export const surveySlice = createSlice({
name: 'survey',
initialState,
reducers: surveyReducers,
extraReducers: (builder) => {
builder.addCase(getSurveyAsyncThunk.pending, (state) => {
state.isLoading = true;
state.isError = false;
});
builder.addCase(getSurveyAsyncThunk.fulfilled, (state, action) => {
state.isLoading = false;
state.isError = false;
state.survey = action.payload;
});
builder.addCase(getSurveyAsyncThunk.rejected, (state) => {
state.isLoading = false;
state.isError = true;
});
builder.addCase(submitSurveyAsyncThunk.pending, (state) => {
state.isLoading = true;
state.isError = false;
});
builder.addCase(submitSurveyAsyncThunk.fulfilled, (state) => {
state.isLoading = false;
state.isSubmitSuccess = true;
state.questionRequests = [];
});
builder.addCase(submitSurveyAsyncThunk.rejected, (state) => {
state.isLoading = false;
state.isSubmitSuccess = false;
state.isError = true;
});
},
});
export const surveyAction = surveySlice.actions;