-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckout.js
93 lines (78 loc) · 2.8 KB
/
Checkout.js
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
84
85
86
87
88
89
90
91
92
93
import React, { Component } from 'react';
import ArticleCard from '../../components/page_components/ArticleCard';
import Klarna from '../../components/Klarna';
import { pushCart } from '../../actions/cart';
import { FormattedMessage, injectIntl } from 'react-intl'
import { Grid, GridCell, GridInner } from '@rmwc/grid';
import { withRouter, Redirect } from 'react-router-dom';
import {Elements, StripeProvider} from 'react-stripe-elements';
import CheckoutForm from '../../components/shop/CheckoutForm';
import CheckoutItems from '../../components/shop/CheckoutItems';
import Header from '../../components/page_components/NiceHeader';
import { stripePublicKey, frontEndPath, authUrl } from '../../constants';
import { connect } from 'react-redux';
class Shop extends Component{
constructor(props) {
super(props);
this.intl = this.props.intl;
this.state = {cart: this.props.items}
};
componentDidMount(){
if(Object.keys(this.state.cart).length > 0){ //Do not push if empty (this causes products to double)
this.props.pushCart()
}
}
componentDidUpdate(oldProps) {
const newProps = this.props
if(oldProps.items !== newProps.items) {
if(Object.keys(oldProps.items).length === 0){ //Only update if empty from beggining
this.props.pushCart()
this.setState({cart: newProps.items});
}
}
}
static pageTitle(){
return <FormattedMessage id='Checkout.title' />
}
static pageNavTitle(){
return <FormattedMessage id='Checkout.navTitle' />
}
render() {
if (this.props.error !== null) return (<Redirect to='/shop' />);
return(
<React.Fragment>
<Grid className="base-outer-grid base-outer-grid--first">
<GridInner>
<GridCell desktop='12' tablet='8' phone='4' >
<Header>
<FormattedMessage id='Shop.cart'/>
</Header>
</GridCell>
<CheckoutItems items={this.state.cart}/>
<GridCell desktop='12' tablet='8' phone='4' >
<Header>
<FormattedMessage id='Shop.payment' />
</Header>
</GridCell>
{/* Uses stripes react components to handle payments
* https://stripe.com/docs/stripe-js/react
*/
}
<StripeProvider apiKey={stripePublicKey}>
<GridCell desktop='12' tablet='8' phone='4' className='stripe example'>
<Elements>
<CheckoutForm />
</Elements>
</GridCell>
</StripeProvider>
</GridInner>
</Grid>
</React.Fragment>
);
}
}
const mapStateToProps = state => ({
items : state.cart.cart,
error : state.cart.error
})
export default connect(mapStateToProps, {pushCart})(withRouter(injectIntl(Shop, { withRef: true })));