Created
June 13, 2017 12:33
-
-
Save alxvallejo/e75eaa5314d598eff366ddf942b75ab2 to your computer and use it in GitHub Desktop.
async action creator
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
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