search.js 1.32 KB
import {sendSearch} from '../lib/api'
import {readTest} from '../lib/readTest';

const CHANGE = 'search/CHANGE';
const SUCCESS = 'search/SUCCESS';
const FAILURE = 'search/FAILURE';
const START = 'search/START';

export const change = (text) => ({ 
    type: CHANGE,
    text,
})

export const submit = (text) => async (dispatch) => { 
    dispatch( {type:START});
    try{
        /*const response = await sendSearch(text);*/
        const response = await readTest(); // 테스트용입니당~
        //dispatch( { type:SUCCESS, result:response }
        setTimeout(()=>dispatch( { type:SUCCESS, result:response }),500); // 셋타임아웃도 테스트용
    }
    catch(err){
        dispatch({ type:FAILURE, result:response })
    }
}


const initialState = {
    query: '',
    result: {
        return_data: {
            searchResults: []
        }   
    },
    isLoading: false,
};
   
export default ToggleLoading = (state = initialState, action) => {
    switch (action.type) {
        case CHANGE:
            return {...state, query: action.text};
        case SUCCESS:
            return {...state, isLoading:false,result: action.result};
        case FAILURE:
            return {...state, isLoading:false};
        case START:
            return {...state, query:'',isLoading:true}
        default:
            return state;
    }
}