Created
August 16, 2018 19:59
-
-
Save antialias/82c61b5766343cb3bdd72e9fb9acbd2a to your computer and use it in GitHub Desktop.
Simple Thunk API Request
This file contains hidden or 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
import { | |
API_BUTTON_CLICK, | |
API_BUTTON_CLICK_SUCCESS, | |
API_BUTTON_CLICK_ERROR, | |
} from './actions/consts'; | |
import { getDataFromAPI } from './api'; | |
const getDataStarted = () => ({ type: API_BUTTON_CLICK }); | |
const getDataSuccess = data => ({ type: API_BUTTON_CLICK_SUCCESS, payload: data }) | |
const getDataError = message => ({ type: API_BUTTON_CLICK_ERROR, payload: message }); | |
const getDataFromAPI = () => { | |
return dispatch => { | |
dispatch(getDataStarted()); | |
getDataFromAPI() | |
.then(data => { | |
dispatch(getDataSuccess(data)); | |
}).fail(err => { | |
dispatch(getDataError(err.message)); | |
}) | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment