Skip to content

Instantly share code, notes, and snippets.

@alxvallejo
Created June 13, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alxvallejo/e75eaa5314d598eff366ddf942b75ab2 to your computer and use it in GitHub Desktop.
Save alxvallejo/e75eaa5314d598eff366ddf942b75ab2 to your computer and use it in GitHub Desktop.
async action creator
class ProfileAction {
constructor{}
//action creators
syncProfile() {
this.fetchProfile()
.then(old_profile => this.fetchProfileDetailsFrom3rdParty())
.then(new_profile => this.syncProfileDetails(old_profile, new_profile))
.then(new_profile => this.dispatch(this.syncProfileSuccess(new_profile)))
.catch(e => this.syncProfileFail(e))
}
fetchProfile() {
fetch()..
}
fetchProfileDetailsFrom3rdParty() {
fetch()..
}
syncProfileDetails(old_profile, new_profile) {
newProfile = {
...old_profile,
// new_profile props
}
dispatch(this.updateProfile())
.then(json => this.updateProfileSuccess(json))
.catch(e => this.updateProfileFail(e))
}
//actions
getProfile() {
return {
type: GET_PROFILE
}
}
syncProfileSuccess() {
return {
type: GET_PROFILE_SUCCESS
}
}
syncProfileFail() {
return {
type: GET_PROFILE_FAIL
}
}
updateProfile() {
return {
type: UPDATE_PROFILE
}
}
updateProfileSuccess() {
return {
type: UPDATE_PROFILE_SUCCESS,
data
}
}
updateProfileFail(e) {
return {
type: UPDATE_PROFILE_FAIL,
error
}
}
}
export default profile = new ProfileAction()
// in my component...
import profile as profileActions from './actions/profile'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment