Advertisement
gembleng

Checkout.js

Oct 30th, 2022
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from 'react';
  2. import { Redirect } from 'react-router';
  3. import { connect } from 'react-redux';
  4.  
  5. import Button from '../../UI/Button/Button';
  6. import Address from '../../UI/Address/Address';
  7. import AddressForm from './AddressForm/AddressForm';
  8. import Spinner from '../../UI/Spinner/Spinner';
  9. import RadioButton from '../../UI/RadioButton/RadioButton';
  10. import PageTitle from '../../UI/PageTitle/PageTitle';
  11. import SectionTitle from '../../UI/SectionTitle/SectionTitle';
  12. import ErrorDisplay from '../../Util/ErrorDisplay/ErrorDisplay';
  13.  
  14. import * as actions from '../../../store/actions/actions';
  15.  
  16. import commonStyle from '../../../static/style/common.module.css';
  17.  
  18. const placeOrderHandler = (
  19.     event,
  20.     address,
  21.     modeSelected,
  22.     data,
  23.     placeOrder,
  24.     placeOrderFail
  25. ) => {
  26.     event.preventDefault();
  27.  
  28.     if (address && modeSelected) {
  29.         placeOrder(data);
  30.     } else if (!address && !modeSelected) {
  31.         placeOrderFail('Please make sure that all fields are filled');
  32.     } else if (!address) {
  33.         placeOrderFail('Please fill in the address field');
  34.     } else {
  35.         placeOrderFail('Please select the mode of payment field');
  36.     }
  37. };
  38.  
  39. function Checkout(props) {
  40.     const order = props.cart.map((item) => ({
  41.         name: item.name,
  42.         quantity: item.quantity,
  43.         itemPrice: item.price,
  44.         price: item.quantity * item.price,
  45.     }));
  46.     const data = {
  47.         order: order,
  48.         address: props.address,
  49.         uid: props.user && props.user.uid,
  50.         price: {
  51.             price: props.price,
  52.             gst: props.gst,
  53.             total: props.price + props.gst,
  54.         },
  55.     };
  56.  
  57.     const { placeOrderInitialize, getAddress, user } = props;
  58.  
  59.     const [addressFormShown, setAddressFormShown] = useState(false);
  60.     const [modeSelected, setModeSelected] = useState(false);
  61.  
  62.     useEffect(() => {
  63.         getAddress(user);
  64.     }, [user, getAddress]);
  65.     useEffect(() => {
  66.         return () => placeOrderInitialize();
  67.     }, [placeOrderInitialize]);
  68.  
  69.     if (props.orderPlaced) {
  70.         props.clearCart();
  71.     }
  72.  
  73.     return (
  74.         <div className={`container mt-5 pt-2 ${commonStyle.PageBody}`}>
  75.             {props.isOrderLoading ? (
  76.                 <Spinner />
  77.             ) : (
  78.                 <>
  79.                     {props.orderPlaced ? (
  80.                         <div>
  81.                             <PageTitle>Order Placed</PageTitle>
  82.                             <h1 className='display-6 mt-4'>
  83.                                 Your yummy pizza will arrive at your doorstep
  84.                                 soon! :)
  85.                             </h1>
  86.                         </div>
  87.                     ) : (
  88.                         <>
  89.                             {props.cart.length === 0 ? (
  90.                                 <Redirect to='./menu' />
  91.                             ) : null}
  92.                             {!props.user ? <Redirect to='./login' /> : null}
  93.                             <PageTitle>Checkout</PageTitle>
  94.  
  95.                             <div className='my-4'>
  96.                                 <SectionTitle>Location</SectionTitle>
  97.                                 {props.isAddressLoading ? (
  98.                                     <Spinner />
  99.                                 ) : (
  100.                                     <>
  101.                                         <Address {...props.address} />
  102.  
  103.                                         {addressFormShown ? (
  104.                                             <AddressForm
  105.                                                 {...props.address}
  106.                                                 hideAddressForm={() =>
  107.                                                     setAddressFormShown(false)
  108.                                                 }
  109.                                             />
  110.                                         ) : props.addressError ===
  111.                                           'No Address Found' ? (
  112.                                             <>
  113.                                                 <ErrorDisplay>
  114.                                                     {props.addressError}
  115.                                                 </ErrorDisplay>
  116.                                                 <span className='my-3 d-inline-block'>
  117.                                                     <Button
  118.                                                         onClick={() =>
  119.                                                             setAddressFormShown(
  120.                                                                 true
  121.                                                             )
  122.                                                         }>
  123.                                                         Add Address
  124.                                                     </Button>
  125.                                                 </span>
  126.                                             </>
  127.                                         ) : (
  128.                                             <span className='my-3 d-inline-block'>
  129.                                                 <Button
  130.                                                     onClick={() =>
  131.                                                         setAddressFormShown(
  132.                                                             true
  133.                                                         )
  134.                                                     }>
  135.                                                     Update Address
  136.                                                 </Button>
  137.                                             </span>
  138.                                         )}
  139.                                     </>
  140.                                 )}
  141.                             </div>
  142.                             <div className='my-4'>
  143.                                 <SectionTitle>Mode of Payment</SectionTitle>
  144.                                 <form>
  145.                                     <div className='col-12 mt-4'>
  146.                                         <RadioButton
  147.                                             name='ModeOfPayment'
  148.                                             code='CashOnDelivery'
  149.                                             isRequired
  150.                                             clickFunc={() =>
  151.                                                 setModeSelected(true)
  152.                                             }>
  153.                                             Cash on Delivery
  154.                                         </RadioButton>
  155.                                         <RadioButton
  156.                                             name='ModeOfPayment'
  157.                                             code='Wallet'
  158.                                             isDisabled
  159.                                             isRequired
  160.                                             clickFunc={() =>
  161.                                                 setModeSelected(true)
  162.                                             }>
  163.                                             Wallet
  164.                                         </RadioButton>
  165.                                         <RadioButton
  166.                                             name='ModeOfPayment'
  167.                                             code='CreditOrDebitCard'
  168.                                             isDisabled
  169.                                             isRequired
  170.                                             clickFunc={() =>
  171.                                                 setModeSelected(true)
  172.                                             }>
  173.                                             Credit / Debit Card
  174.                                         </RadioButton>
  175.                                         <RadioButton
  176.                                             name='ModeOfPayment'
  177.                                             code='NetBanking'
  178.                                             isDisabled
  179.                                             isRequired
  180.                                             clickFunc={() =>
  181.                                                 setModeSelected(true)
  182.                                             }>
  183.                                             Net Banking
  184.                                         </RadioButton>
  185.                                     </div>
  186.                                     <div className='col-12'>
  187.                                         {props.orderError ? (
  188.                                             <ErrorDisplay>
  189.                                                 {props.orderError}
  190.                                             </ErrorDisplay>
  191.                                         ) : null}
  192.                                     </div>
  193.                                     <div className='col-12 my-3'>
  194.                                         <Button
  195.                                             type='button'
  196.                                             onClick={(event) =>
  197.                                                 placeOrderHandler(
  198.                                                     event,
  199.                                                     props.address,
  200.                                                     modeSelected,
  201.                                                     data,
  202.                                                     props.placeOrder,
  203.                                                     props.placeOrderFail
  204.                                                 )
  205.                                             }>
  206.                                             Place Order
  207.                                         </Button>
  208.                                     </div>
  209.                                 </form>
  210.                             </div>
  211.                         </>
  212.                     )}
  213.                 </>
  214.             )}
  215.         </div>
  216.     );
  217. }
  218.  
  219. const mapStateToProps = (state) => ({
  220.     cart: state.cart.cart,
  221.     price: state.cart.totalPrice,
  222.     gst: state.cart.gst,
  223.     user: state.auth.user,
  224.     address: state.address.address,
  225.     addressError: state.address.error,
  226.     isAddressLoading: state.address.isLoading,
  227.     orderPlaced: state.order.orderPlaced,
  228.     isOrderLoading: state.order.isLoading,
  229.     orderError: state.order.error,
  230. });
  231.  
  232. const mapDispatchToProps = (dispatch) => ({
  233.     placeOrderInitialize: () => dispatch(actions.placeOrderInitialize()),
  234.     placeOrder: (data) => dispatch(actions.placeOrder(data)),
  235.     placeOrderFail: (error) => dispatch(actions.placeOrderFail(error)),
  236.     getAddress: (user) => dispatch(actions.getAddress(user)),
  237.     clearCart: () => dispatch(actions.clearCart()),
  238. });
  239.  
  240. export default connect(mapStateToProps, mapDispatchToProps)(Checkout);
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement