Last active
March 8, 2017 20:14
-
-
Save heymartinadams/013fc730570898caf8778eeb94ec739d 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
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