Skip to content

Instantly share code, notes, and snippets.

@heymartinadams
Last active March 8, 2017 20:14
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 heymartinadams/013fc730570898caf8778eeb94ec739d to your computer and use it in GitHub Desktop.
Save heymartinadams/013fc730570898caf8778eeb94ec739d to your computer and use it in GitHub Desktop.
componentDidUpdate(newProps) {
if (this.props.data.user) {
const userId = this.props.data.user.id
if (!this.stripeTokenSubscription && !this.purchaseSubscription) {
// Logic #5: Create subscription to trigger response once user in User model has been successfully assigned a Stripe customer ID
this.stripeTokenSubscription = newProps.data.subscribeToMore({
document: receiveStripeId,
variables: { userId },
updateQuery: (previousState, { subscriptionData }) => {
console.log('Purchasing...')
this.setState({ status: 'Purchasing...' })
// Logic #6: Now that user has a Stripe customer Id (ie. subscription has been triggered), make a purchase by creating a node in Purchase model.
this.props.createPurchase({ variables: { userId } })
.then(() => {
console.log('Purchase complete')
this.setState({ status: 'Purchase complete...' })
// Cancel current subscription
this.stripeTokenSubscription()
})
.catch((e) => { console.error(e) })
},
onError: (err) => console.error(err)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment