Created
July 24, 2019 07:51
-
-
Save takahirohonda/d41a81d67c6b11fe7ab42a8734eca16c to your computer and use it in GitHub Desktop.
calling-async-function-in-action-with-redux-thunk-5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const setPersonalData = (personalData) => { | |
return { | |
type: UPDATE_PesonalData, | |
personalData | |
} | |
} | |
export const getPersonalData = (payload) => (dispatch) => { | |
// Update the store on the status of API call (optional) | |
dispatch(apiCallInProgress()) | |
// Make AJAX call | |
return postRequest(payload) | |
.then((response) => { | |
// Once AJAX call is successful, update the store | |
dispatch(setPersonalData(response)) | |
// Then indicate apiCall was successful (optional) | |
dispatch(apiCallSuccess()) | |
}) | |
.catch(err => dispatch(apiCallNotSuccess())) | |
} | |
export const getPersonalData = (payload) => (dispatch, getState) => { | |
// Update the store on the status of API call (optional) | |
dispatch(apiCallInProgress()) | |
// Make AJAX call | |
return postRequest(payload) | |
.then((response) => { | |
// we can transform response data with current state retrived with getState | |
// with fictitious transformData function takes response and current state. | |
const newState = transformData(response, getState) | |
// Once AJAX call is successful, update the store | |
dispatch(setPersonalData(newState)) | |
// Then indicate apiCall was successful (optional) | |
dispatch(apiCallSuccess()) | |
}) | |
.catch(err => dispatch(apiCallNotSuccess())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment