1
|
|
|
import React, { Fragment, useState, useContext } from 'react'; |
2
|
|
|
import ContentWithAsideLayout from '../../layouts/ContentWithAsideLayout'; |
3
|
|
|
import { ProductContext } from '../../context/Context'; |
4
|
|
|
import { calculateSale, getTotalPrice } from '../../helpers/utils'; |
5
|
|
|
import CheckoutAside from './checkout/CheckoutAside'; |
6
|
|
|
import CheckoutShippingAddress from './checkout/CheckoutShippingAddress'; |
7
|
|
|
import CheckoutPaymentMethod from './checkout/CheckoutPaymentMethod'; |
8
|
|
|
|
9
|
|
|
const Checkout = () => { |
10
|
|
|
const { products, shoppingCart, appliedPromo } = useContext(ProductContext); |
11
|
|
|
const [shippingAddress, setShippingAddress] = useState('address-1'); |
12
|
|
|
const [paymentMethod, setPaymentMethod] = useState('credit-card'); |
13
|
|
|
|
14
|
|
|
const calculatedShippingCost = parseFloat( |
15
|
|
|
shoppingCart |
16
|
|
|
.map(shoppingCartItem => products.find(product => product.id === shoppingCartItem.id)) |
17
|
|
|
.filter(product => product.hasOwnProperty('shippingCost') && product.shippingCost !== 0) |
18
|
|
|
.reduce((accumulated, product) => (accumulated > product.shippingCost ? accumulated : product.shippingCost), 0) |
19
|
|
|
); |
20
|
|
|
const subTotal = parseFloat(getTotalPrice(shoppingCart, products).toFixed(2)); |
21
|
|
|
const payableTotal = parseFloat( |
22
|
|
|
(parseFloat(calculateSale(subTotal, !!appliedPromo ? appliedPromo.discount : 0)) + calculatedShippingCost).toFixed( |
23
|
|
|
2 |
24
|
|
|
) |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
return ( |
28
|
|
|
<ContentWithAsideLayout |
29
|
|
|
aside={ |
30
|
|
|
<CheckoutAside |
31
|
|
|
calculatedShippingCost={calculatedShippingCost} |
32
|
|
|
subTotal={subTotal} |
33
|
|
|
payableTotal={payableTotal} |
34
|
|
|
/> |
35
|
|
|
} |
36
|
|
|
isStickyAside={false} |
37
|
|
|
> |
38
|
|
|
<Fragment> |
39
|
|
|
<CheckoutShippingAddress shippingAddress={shippingAddress} setShippingAddress={setShippingAddress} /> |
40
|
|
|
<CheckoutPaymentMethod |
41
|
|
|
payableTotal={payableTotal} |
42
|
|
|
paymentMethod={paymentMethod} |
43
|
|
|
setPaymentMethod={setPaymentMethod} |
44
|
|
|
/> |
45
|
|
|
</Fragment> |
46
|
|
|
</ContentWithAsideLayout> |
47
|
|
|
); |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
export default Checkout; |
51
|
|
|
|