이준호

[REFACTOR] redux-thunk 미들웨어 추가로 리팩토링 + api 모듈화 작업

1 +import axios from 'axios';
2 +
3 +const getQuestion = async () =>{
4 + const res = axios.get("getquestions");
5 + return res;
6 +}
7 +
8 +export default getQuestion;
...\ No newline at end of file ...\ No newline at end of file
1 +import axios from 'axios';
2 +
3 +const getResult = async (answer) => {
4 + const res = await axios.post("submit",answer);
5 + return res;
6 +}
7 +
8 +export default getResult;
...\ No newline at end of file ...\ No newline at end of file
1 +import getResultApi from '../../api/getResult';
2 +
1 // action 3 // action
2 const GET_RESULT = 'GET_RESULT'; 4 const GET_RESULT = 'GET_RESULT';
3 const GET_RESULT_SUCCESS = 'GET_RESULT_SUCCESS'; 5 const GET_RESULT_SUCCESS = 'GET_RESULT_SUCCESS';
...@@ -14,6 +16,18 @@ const initialState = { ...@@ -14,6 +16,18 @@ const initialState = {
14 error: null 16 error: null
15 } 17 }
16 18
19 +export function getResultThunk(answer){
20 + return async (dispatch) => {
21 + dispatch(getResult());
22 + try{
23 + const { data } = await getResultApi(answer);
24 + dispatch(getResultSuccess(data));
25 + }catch(err){
26 + dispatch(getResultError());
27 + }
28 + }
29 +}
30 +
17 function result( 31 function result(
18 state = initialState, 32 state = initialState,
19 action 33 action
......
1 +import getQuestionApi from '../../api/getQuestion';
2 +
1 // action 3 // action
2 const GET_SURVEY = 'GET_SURVEY'; 4 const GET_SURVEY = 'GET_SURVEY';
3 const GET_SURVEY_SUCCESS = 'GET_SURVEY_SUCCESS'; 5 const GET_SURVEY_SUCCESS = 'GET_SURVEY_SUCCESS';
...@@ -14,6 +16,18 @@ const initialState = { ...@@ -14,6 +16,18 @@ const initialState = {
14 error: null 16 error: null
15 } 17 }
16 18
19 +export function getQuestionThunk(){
20 + return async (dispatch) => {
21 + dispatch(getSurvey());
22 + try{
23 + const { data } = await getQuestionApi();
24 + dispatch(getSurveySuccess(data));
25 + }catch(err){
26 + dispatch(getSurveyError());
27 + }
28 + }
29 +}
30 +
17 function survey( 31 function survey(
18 state = initialState, 32 state = initialState,
19 action 33 action
......