Advertisement
vallec

OrderSummary

May 15th, 2024
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use client";
  2.  
  3. import React, { useEffect, useState } from "react";
  4. import st from "./styles.module.scss";
  5. import { H6, P } from "../TextStyles";
  6. import Button from "@/components/Button";
  7. import { photo_url } from "@/utils/apiUrl";
  8. import Image from "@/components/Image";
  9. import Bullet from "../Bullet";
  10. import { Form, Formik } from "formik";
  11. import CheckBox from "@/components/Fields/CheckBox";
  12.  
  13. import checkoutValidation from "@/_validations/checkoutReview";
  14. import { formatNumber } from "@/utils/numbers";
  15. import ProductSummary from "./ProductSummary";
  16.  
  17. const OrderSummary = ({
  18.     cartData,
  19.     checkoutStep,
  20.     setCheckoutStep,
  21.     submit,
  22.     setCheckoutValues,
  23.     setCheckoutErrors,
  24.     checkoutErrors,
  25.     shippingErrors,
  26.     invoiceErrors,
  27.     shippingDirty,
  28.     invoiceDirty,
  29. }) => {
  30.     const [checkoutDirty, setCheckoutDirty] = useState({});
  31.     const [checkoutValid, setCheckoutValid] = useState({});
  32.     const setCheckout = (values, errors, dirty, valid) => {
  33.         setCheckoutValues(values);
  34.         setCheckoutErrors(errors);
  35.         setCheckoutDirty(dirty);
  36.         setCheckoutValid(valid);
  37.     };
  38.  
  39.     return (
  40.         <div className={st.wrapper}>
  41.             {cartData && (
  42.                 <>
  43.                     {/* {JSON.stringify(checkoutStep, null, 4)} */}
  44.  
  45.                     {/* {JSON.stringify(cartData, null, 4)} */}
  46.                     <div className={st.titleGreenElement}>
  47.                         <H6>Order summary</H6>
  48.                         {/* <div className={st.greenElement}>
  49.               <P className={st.greenElementTitle}>
  50.                 Your order will save <span>576 kg CO2</span>
  51.               </P>
  52.               <svg
  53.                 width="33"
  54.                 height="32"
  55.                 viewBox="0 0 33 32"
  56.                 fill="none"
  57.                 xmlns="http://www.w3.org/2000/svg"
  58.               >
  59.                 <path
  60.                   d="M37.4 15.71C37.398 18.0241 36.7099 20.2855 35.4226 22.2085C34.1354 24.1315 32.3068 25.6295 30.1682 26.5132C28.0295 27.3969 25.6768 27.6266 23.4076 27.1732C21.1384 26.7198 19.0546 25.6037 17.4198 23.9661C15.7849 22.3284 14.6724 20.2427 14.2229 17.9727C13.7734 15.7028 14.0071 13.3505 14.8944 11.2134C15.7818 9.07625 17.283 7.25022 19.2082 5.96625C21.1333 4.68228 23.396 3.99803 25.71 4C28.8122 4.0053 31.7856 5.24136 33.9773 7.43683C36.1691 9.63231 37.4 12.6078 37.4 15.71Z"
  61.                   fill="#F3E8DA"
  62.                 />
  63.                 <path
  64.                   d="M29.3391 7.39062C28.637 7.61157 27.89 7.64942 27.1692 7.50061C26.6792 7.40061 25.6691 7.19064 25.3091 7.97064C25.0517 8.49941 24.6696 8.9577 24.1958 9.30609C23.722 9.65448 23.1706 9.88255 22.5891 9.97064C22.0691 10.0506 21.8992 10.6006 21.8492 10.7806L20.9291 13.7806C20.7234 14.1623 20.6753 14.6093 20.7949 15.026C20.9145 15.4427 21.1924 15.7961 21.5691 16.0106C22.1381 16.642 22.8437 17.1351 23.6323 17.4523C24.4208 17.7694 25.2714 17.9023 26.1191 17.8406C26.5141 17.8004 26.9132 17.8274 27.2992 17.9207C27.4455 17.9665 27.5792 18.0453 27.6902 18.1511C27.8012 18.2568 27.8863 18.3867 27.9391 18.5306C27.9689 18.5878 27.9845 18.6512 27.9845 18.7156C27.9845 18.7801 27.9689 18.8435 27.9391 18.9006C27.8278 19.0974 27.7693 19.3196 27.7693 19.5457C27.7693 19.7717 27.8278 19.9939 27.9391 20.1906C27.9913 20.3107 28.0347 20.4343 28.0691 20.5606C28.0994 20.9601 28.0125 21.3598 27.8191 21.7106C27.5537 22.2064 27.4555 22.7746 27.5392 23.3306C27.7692 24.3306 28.8192 24.6806 29.5392 24.8306"
  65.                   stroke="#3E562B"
  66.                   stroke-miterlimit="10"
  67.                 />
  68.                 <path
  69.                   d="M1.01953 16.309C1.41953 15.399 1.84956 14.959 2.29956 14.999H2.45953C2.61172 15.0255 2.76737 15.0255 2.91956 14.999C3.48956 14.939 3.75955 14.4691 3.97955 14.0891C4.07776 13.8661 4.22878 13.6705 4.41956 13.519C4.68426 13.4219 4.97486 13.4219 5.23956 13.519C5.58149 13.6287 5.94315 13.6628 6.29956 13.619C6.5591 13.5591 6.80841 13.4614 7.03955 13.329L7.12952 13.269C7.98189 12.8099 8.94222 12.5888 9.90955 12.629C10.2615 12.6841 10.6209 12.5978 10.9095 12.389C11.0038 12.3031 11.0784 12.1979 11.1285 12.0806C11.1785 11.9632 11.2027 11.8366 11.1995 11.709C11.1809 11.5147 11.1236 11.3261 11.0308 11.1544C10.9381 10.9826 10.8118 10.8312 10.6595 10.709L10.2795 10.449C10.1378 10.3645 10.0039 10.2674 9.87952 10.1591C9.99906 9.80194 10.0467 9.42463 10.0195 9.04901C9.96293 8.7266 10.0194 8.3946 10.1795 8.10907C10.3675 7.92703 10.6033 7.80213 10.8596 7.74902L11.1995 7.62903C11.5108 7.54411 11.7782 7.34386 11.9472 7.06897C12.1162 6.79408 12.1743 6.46519 12.1096 6.14905C12.055 6.00932 11.9809 5.878 11.8895 5.75903C11.8601 5.74389 11.8355 5.72097 11.8183 5.69275C11.8011 5.66453 11.792 5.63211 11.792 5.59906C11.792 5.56601 11.8011 5.53359 11.8183 5.50537C11.8355 5.47715 11.8601 5.45417 11.8895 5.43903C11.9997 5.29864 12.0752 5.13417 12.1096 4.95905C12.1996 4.38905 11.6695 4.04907 11.3795 3.85907C10.7172 3.42935 10.2202 2.78775 9.96954 2.03906"
  70.                   stroke="#3E562B"
  71.                   stroke-linecap="round"
  72.                   stroke-linejoin="round"
  73.                 />
  74.                 <path
  75.                   d="M10.75 19.9609L14.26 25.9009"
  76.                   stroke="#3E562B"
  77.                   stroke-linecap="round"
  78.                   stroke-linejoin="round"
  79.                 />
  80.                 <path
  81.                   d="M8.58008 16.3086C8.58008 16.3086 15.7201 17.4886 18.6401 21.4686"
  82.                   stroke="#3E562B"
  83.                   stroke-linecap="round"
  84.                   stroke-linejoin="round"
  85.                 />
  86.                 <path
  87.                   d="M16.61 31.72C25.2312 31.72 32.22 24.7312 32.22 16.11C32.22 7.48882 25.2312 0.5 16.61 0.5C7.98888 0.5 1 7.48882 1 16.11C1 24.7312 7.98888 31.72 16.61 31.72Z"
  88.                   stroke="#3E562B"
  89.                   stroke-linecap="round"
  90.                   stroke-linejoin="round"
  91.                 />
  92.                 <path
  93.                   d="M22.5199 20.4688C22.5199 20.4688 16.7398 21.4687 14.8298 24.7487C13.0098 27.8787 16.1599 31.3287 16.1599 31.3287"
  94.                   stroke="#3E562B"
  95.                   stroke-linecap="round"
  96.                   stroke-linejoin="round"
  97.                 />
  98.                 <path
  99.                   d="M8.57905 16.3086C8.57905 16.3086 5.94905 23.7986 8.57905 28.2286C10.179 30.8986 13.249 31.2886 15.269 31.2286"
  100.                   stroke="#3E562B"
  101.                   stroke-linecap="round"
  102.                   stroke-linejoin="round"
  103.                 />
  104.                 <path
  105.                   d="M22.5189 20.4688C22.5189 20.4688 24.5189 25.9887 22.5189 29.2687C20.6789 32.3887 16.1289 31.3287 16.1289 31.3287"
  106.                   stroke="#3E562B"
  107.                   stroke-linecap="round"
  108.                   stroke-linejoin="round"
  109.                 />
  110.               </svg>
  111.             </div> */}
  112.                     </div>
  113.                     <div className={st.priceDetailsWrapper}>
  114.                         <div className={st.priceAllDetails}>
  115.                             <div className={st.priceDetails}>
  116.                                 <div className={st.priceDetail}>
  117.                                     <P>Subtotal</P>
  118.                                     <P>{formatNumber(cartData.sub_total || 0)}</P>
  119.                                 </div>
  120.                                 {cartData.discount > 0 && (
  121.                                     <div className={st.priceDetail}>
  122.                                         <P>Discount</P>
  123.                                         <P>-{formatNumber(cartData.discount || 0)}</P>
  124.                                     </div>
  125.                                 )}
  126.                                 {cartData.delivery > 0 && (
  127.                                     <div className={st.priceDetail}>
  128.                                         <P>Shipping</P>
  129.                                         <P>{formatNumber(cartData.delivery || 0)}</P>
  130.                                     </div>
  131.                                 )}
  132.                             </div>
  133.                             <svg
  134.                                 width='100%'
  135.                                 height='1'
  136.                                 viewBox='0 0 357 1'
  137.                                 fill='none'
  138.                                 xmlns='http://www.w3.org/2000/svg'
  139.                             >
  140.                                 <line y1='0.5' x2='357' y2='0.5' stroke='#4F1444' />
  141.                             </svg>
  142.                             <div className={st.priceTotal}>
  143.                                 <H6>Total</H6>
  144.                                 <H6>{formatNumber(cartData.total)}</H6>
  145.                             </div>
  146.                             {checkoutStep === 3 && (
  147.                                 <Formik
  148.                                     enableReinitialize
  149.                                     initialValues={{
  150.                                         agree_tac: false,
  151.                                     }}
  152.                                     validationSchema={checkoutValidation}
  153.                                     // onSubmit={onSubmit}
  154.                                     innerRef={(formikActions) =>
  155.                                         formikActions &&
  156.                                         setCheckout(
  157.                                             formikActions.values,
  158.                                             formikActions.errors,
  159.                                             formikActions.dirty,
  160.                                             formikActions.isValid
  161.                                         )
  162.                                     }
  163.                                 >
  164.                                     {({ errors, touched, values }) => (
  165.                                         <Form className={st.form}>
  166.                                             <CheckBox
  167.                                                 label='I agree '
  168.                                                 name='agree_tac'
  169.                                                 link='/privacy-policy'
  170.                                                 linkText='Privacy policy'
  171.                                                 afterText='rules'
  172.                                                 link2='/terms-and-conditions'
  173.                                                 link2Text='Terms & Conditions'
  174.                                             />
  175.                                         </Form>
  176.                                     )}
  177.                                 </Formik>
  178.                             )}
  179.                         </div>
  180.                         {/* {JSON.stringify([[!checkoutDirty || !checkoutValid], shippingDirty, invoiceDirty])} */}
  181.                         <div className={st.buttonAndInfo}>
  182.                             {checkoutStep === 1 && (
  183.                                 <>
  184.                                     <Button bg size='lg' onClick={() => setCheckoutStep(2)}>
  185.                                         checkout
  186.                                     </Button>
  187.                                     <P size={"sm"}>You won’t be charged yet</P>
  188.                                 </>
  189.                             )}
  190.                             {/* {JSON.stringify([
  191.                 Object.keys(checkoutErrors).length,
  192.                 Object.keys(shippingErrors).length,
  193.                 Object.keys(invoiceErrors).length,
  194.                 shippingDirty,
  195.                 invoiceDirty,
  196.               ])} */}
  197.                             {checkoutStep === 2 && (
  198.                                 <Button
  199.                                     bg
  200.                                     size='lg'
  201.                                     // onClick={() => submit()}
  202.                                     onClick={() => setCheckoutStep(3)}
  203.                                     disabled={
  204.                                         Object.keys(checkoutErrors).length !== 0 ||
  205.                                         Object.keys(shippingErrors).length !== 0 ||
  206.                                         Object.keys(invoiceErrors).length !== 0 ||
  207.                                         !shippingDirty ||
  208.                                         !invoiceDirty
  209.                                     }
  210.                                 >
  211.                                     Preview and confirm
  212.                                 </Button>
  213.                             )}
  214.                             {checkoutStep === 3 && (
  215.                                 <Button
  216.                                     bg
  217.                                     size='lg'
  218.                                     onClick={() => submit()}
  219.                                     disabled={
  220.                                         Object.keys(checkoutErrors).length !== 0 ||
  221.                                         Object.keys(shippingErrors).length !== 0 ||
  222.                                         Object.keys(invoiceErrors).length !== 0 ||
  223.                                         !checkoutDirty ||
  224.                                         !checkoutValid ||
  225.                                         !shippingDirty ||
  226.                                         !invoiceDirty
  227.                                     }
  228.                                 >
  229.                                     Confirm and pay
  230.                                 </Button>
  231.                             )}
  232.                             {checkoutStep >= 2 && (
  233.                                 <>
  234.                                     <svg
  235.                                         width='357'
  236.                                         height='1'
  237.                                         viewBox='0 0 357 1'
  238.                                         fill='none'
  239.                                         xmlns='http://www.w3.org/2000/svg'
  240.                                         style={{ margin: "32px 0" }}
  241.                                     >
  242.                                         <line y1='0.5' x2='357' y2='0.5' stroke='#EFEFEF' />
  243.                                     </svg>
  244.                                     {/* {JSON.stringify(cartData, null, 4)} */}
  245.                                     {/* {cartData.product.map((pr, i) => (
  246.                                         <ProductSummary
  247.                                             key={i}
  248.                                             pr={pr}
  249.                                             i={i}
  250.                                             length={cartData.product.length - 1}
  251.                                         />
  252.                                     ))} */}
  253.                                 </>
  254.                             )}
  255.                         </div>
  256.                     </div>
  257.                 </>
  258.             )}
  259.         </div>
  260.     );
  261. };
  262.  
  263. export default OrderSummary;
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement