From 8a5825d52271d712ec7c8348447d433067e13ef2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 13 Sep 2018 14:18:56 -0400 Subject: DONE --- .../FINISHED/frontend/components/TakeMyMoney.js | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 stepped-solutions/FINISHED/frontend/components/TakeMyMoney.js (limited to 'stepped-solutions/FINISHED/frontend/components/TakeMyMoney.js') diff --git a/stepped-solutions/FINISHED/frontend/components/TakeMyMoney.js b/stepped-solutions/FINISHED/frontend/components/TakeMyMoney.js new file mode 100644 index 0000000..ca87832 --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/components/TakeMyMoney.js @@ -0,0 +1,79 @@ +import React from 'react'; +import StripeCheckout from 'react-stripe-checkout'; +import { Mutation } from 'react-apollo'; +import Router from 'next/router'; +import NProgress from 'nprogress'; +import PropTypes from 'prop-types'; +import gql from 'graphql-tag'; +import calcTotalPrice from '../lib/calcTotalPrice'; +import Error from './ErrorMessage'; +import User, { CURRENT_USER_QUERY } from './User'; + +const CREATE_ORDER_MUTATION = gql` + mutation createOrder($token: String!) { + createOrder(token: $token) { + id + charge + total + items { + id + title + } + } + } +`; + +function totalItems(cart) { + return cart.reduce((tally, cartItem) => tally + cartItem.quantity, 0); +} + +class TakeMyMoney extends React.Component { + onToken = async (res, createOrder) => { + NProgress.start(); + // manually call the mutation once we have the stripe token + const order = await createOrder({ + variables: { + token: res.id, + }, + }).catch(err => { + alert(err.message); + }); + Router.push({ + pathname: '/order', + query: { id: order.data.createOrder.id }, + }); + }; + render() { + return ( + + {({ data: { me }, loading }) => { + if (loading) return null; + return ( + + {createOrder => ( + this.onToken(res, createOrder)} + > + {this.props.children} + + )} + + ); + }} + + ); + } +} + +export default TakeMyMoney; +export { CREATE_ORDER_MUTATION }; -- cgit v1.3