Skip to content

Instantly share code, notes, and snippets.

@xavxyz
Created January 19, 2017 09:01
Show Gist options
  • Save xavxyz/9edac6cda02d3e08d55bfa5809d86fba to your computer and use it in GitHub Desktop.
Save xavxyz/9edac6cda02d3e08d55bfa5809d86fba to your computer and use it in GitHub Desktop.
// 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