Created
January 19, 2017 09:01
-
-
Save xavxyz/9edac6cda02d3e08d55bfa5809d86fba to your computer and use it in GitHub Desktop.
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
// the scene takes place on Nova, NewsletterButton.jsx | |
// a little fancy way to do with promise and apollo mutation | |
subscriptionAction() { | |
this.props[this.props.mutationName]({userId: this.props.user._id}).then(result => { | |
this.props.successCallback(result); | |
}).catch(error => { | |
console.log(error); // eslint-disable-line no-console | |
this.props.flash(error.message, "error"); | |
}); | |
} | |
// super fancy way with async/await and destructuration | |
async subscriptionAction() { | |
const { | |
flash, | |
mutationName, | |
successCallback, | |
user, | |
[mutationName]: mutationToTrigger, // dynamic 'mutationToTrigger' variable based on the mutationName (addUserNewsletter or removeUserNewsletter) | |
} = this.props; | |
try { | |
const mutationResult = await mutationToTrigger({userId: user._id}); | |
successCallback(mutationResult); | |
} catch(error) { | |
console.error(error); // eslint-disable-line no-console | |
flash(error.message, "error"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment