Passed
Push — master ( 5126cf...651355 )
by Guangyu
23:00 queued 11s
created

web/src/components/e-commerce/Checkout.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 51
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 40
mnd 2
bc 2
fnc 0
dl 0
loc 51
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
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