blob: b9e42eadb208ff2c1cabe597fc1116e0e79ee699 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import { Component } from 'react';
import StripeCheckout from 'react-stripe-checkout';
import { Mutation, Query } 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 Component {
onToken = async (res, createOrder) => {
NProgress.start();
const order = await createOrder({
variables: {
token: res.id,
},
});
console.log(order);
// Route them to that order page
const { id } = order.data.createOrder;
Router.push({
pathname: `/order`,
query: { id },
});
};
render() {
return (
<User>
{({ data: { me }, error }) => {
if (!me || !me.cart.length) return null;
if (error) return <Error error={error} />;
return (
<Mutation
mutation={CREATE_ORDER_MUTATION}
refetchQueries={[{ query: CURRENT_USER_QUERY }]}
>
{createOrder => (
<StripeCheckout
amount={calcTotalPrice(me.cart)}
name="Sick Fits Haul"
description={`Order of ${totalItems(me.cart)} Items From Sick Fits`}
image={me.cart[0].item && me.cart[0].item.image}
token={res => this.onToken(res, createOrder)}
stripeKey="pk_lclTtThFp8CnO3QtEZSd8HA9mFUps"
currency="USD"
email={me.email}
>
{this.props.children}
</StripeCheckout>
)}
</Mutation>
);
}}
</User>
);
}
}
TakeMyMoney.propTypes = {
children: PropTypes.any,
};
export default TakeMyMoney;
|