Advertisement
vallec

ShippingAndBilling

May 15th, 2024
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use client";
  2.  
  3. import React, { useContext, useEffect, useState } from "react";
  4.  
  5. import st from "./styles.module.scss";
  6. import { Col, Container, Row } from "react-bootstrap";
  7. import { H3, H6, P } from "@/components/TextStyles";
  8. import Image from "@/components/Image";
  9. import { Formik, Form } from "formik";
  10. import Input from "@/components/Fields/Input";
  11. import Select from "@/components/Fields/Select";
  12. import CheckBox from "@/components/Fields/CheckBox";
  13. import Button from "@/components/Button";
  14. import { getCountries } from "@/utils/ordersApi";
  15. import { AuthContext } from "@/providers/AuthContext";
  16. import TextArea from "@/components/Fields/TextArea";
  17.  
  18. import checkoutValidation from "@/_validations/checkout";
  19. import invoiceValidation from "@/_validations/invoice";
  20. import { getProfile } from "@/utils/authenticationApi";
  21.  
  22. const ShippingAndBilling = ({
  23.     setShippingValues,
  24.     setInvoiceValues,
  25.     setShippingErrors,
  26.     setInvoiceErrors,
  27.     setShippingDirty,
  28.     setInvoiceDirty,
  29.     checkoutErrors,
  30.     shippingErrors,
  31.     invoiceErrors,
  32.     shippingDirty,
  33.     invoiceDirty,
  34.     setCheckoutStep,
  35.     setNotesValues,
  36. }) => {
  37.     const { context, updateContext } = useContext(AuthContext);
  38.     const setAuthVisible = () => {
  39.         updateContext({ authModal: "login" });
  40.     };
  41.  
  42.     const [userInfo, setUserInfo] = useState(null);
  43.  
  44.     useEffect(() => {
  45.         const fetchData = async () => {
  46.             if (context.user) {
  47.                 const res = await getProfile(context.user.access_token);
  48.                 setUserInfo(res.data);
  49.             }
  50.         };
  51.         fetchData();
  52.     }, [context.user]);
  53.  
  54.     const [countriesList, setCountriesList] = useState([]);
  55.     const payloadFromStorage =
  56.         typeof window !== "undefined" && localStorage.getItem("checkoutPayload")
  57.             ? JSON.parse(localStorage.getItem("checkoutPayload"))
  58.             : null;
  59.     const initalStateShipping = payloadFromStorage
  60.         ? {
  61.               first_name: payloadFromStorage.first_name,
  62.               last_name: payloadFromStorage.last_name,
  63.               email: payloadFromStorage.email,
  64.               country_id: payloadFromStorage.country_id,
  65.               city: payloadFromStorage.city,
  66.               state: payloadFromStorage.state,
  67.               zip: payloadFromStorage.zip,
  68.               address: payloadFromStorage.address,
  69.               phone: payloadFromStorage.phone,
  70.               phone_code: "123",
  71.               city_id: payloadFromStorage.city_id,
  72.           }
  73.         : {
  74.               first_name:
  75.                   userInfo && userInfo.shipping_details ? userInfo.shipping_details.first_name : "",
  76.               last_name:
  77.                   userInfo && userInfo.shipping_details ? userInfo.shipping_details.last_name : "",
  78.               email: userInfo && userInfo.shipping_details ? userInfo.shipping_details.email : "",
  79.               country_id:
  80.                   userInfo && userInfo.shipping_details ? userInfo.shipping_details.country_id : "",
  81.               city: userInfo && userInfo.shipping_details ? userInfo.shipping_details.city : "",
  82.               state: userInfo && userInfo.shipping_details ? userInfo.shipping_details.state : "",
  83.               zip: userInfo && userInfo.shipping_details ? userInfo.shipping_details.zip : "",
  84.               address:
  85.                   userInfo && userInfo.shipping_details ? userInfo.shipping_details.address : "",
  86.               phone:
  87.                   userInfo && userInfo.shipping_details ? userInfo.shipping_details.phone : "+359",
  88.               phone_code: "123",
  89.               city_id: "",
  90.           };
  91.  
  92.     const initialStateInvoice = payloadFromStorage
  93.         ? {
  94.               company_name: payloadFromStorage?.company_name || "",
  95.               company_vat_number: payloadFromStorage?.company_vat_number || "",
  96.               company_uin_number: payloadFromStorage?.company_uin_number || "",
  97.               company_country_id: payloadFromStorage?.company_country_id || "",
  98.               company_city: payloadFromStorage?.company_city || "",
  99.               company_address: payloadFromStorage?.company_address || "",
  100.               company_manager: payloadFromStorage?.company_manager || "",
  101.               has_invoice: false,
  102.               company_has_vat: payloadFromStorage.company_has_vat
  103.                   ? payloadFromStorage.company_has_vat === 1
  104.                       ? true
  105.                       : false
  106.                   : false,
  107.           }
  108.         : {
  109.               company_name:
  110.                   (userInfo &&
  111.                       userInfo.invoice_details &&
  112.                       userInfo.invoice_details?.company_name) ||
  113.                   "",
  114.               company_vat_number:
  115.                   (userInfo &&
  116.                       userInfo.invoice_details &&
  117.                       userInfo.invoice_details?.company_vat_number) ||
  118.                   "",
  119.               company_uin_number:
  120.                   (userInfo &&
  121.                       userInfo.invoice_details &&
  122.                       userInfo.invoice_details?.company_uin_number) ||
  123.                   "",
  124.               company_country_id:
  125.                   (userInfo &&
  126.                       userInfo.invoice_details &&
  127.                       userInfo.invoice_details?.company_country_id) ||
  128.                   "",
  129.               company_city:
  130.                   (userInfo &&
  131.                       userInfo.invoice_details &&
  132.                       userInfo.invoice_details?.company_city) ||
  133.                   "",
  134.               company_address:
  135.                   (userInfo &&
  136.                       userInfo.invoice_details &&
  137.                       userInfo.invoice_details?.company_address) ||
  138.                   "",
  139.               company_manager:
  140.                   (userInfo &&
  141.                       userInfo.invoice_details &&
  142.                       userInfo.invoice_details?.company_manager) ||
  143.                   "",
  144.               has_invoice: "",
  145.               company_has_vat:
  146.                   userInfo && userInfo?.invoice_details
  147.                       ? userInfo.invoice_details.company_has_vat === 1
  148.                           ? true
  149.                           : false
  150.                       : "",
  151.           };
  152.  
  153.     const initialStateNotes = payloadFromStorage
  154.         ? {
  155.               note: payloadFromStorage?.note || "",
  156.           }
  157.         : {
  158.               note: "",
  159.           };
  160.  
  161.     const setShipping = (values, errors, dirty) => {
  162.         setShippingValues(values);
  163.         setShippingErrors(errors);
  164.         setShippingDirty(dirty);
  165.     };
  166.  
  167.     const setInvoice = (values, errors, dirty) => {
  168.         setInvoiceValues(values);
  169.         setInvoiceErrors(errors);
  170.         setInvoiceDirty(dirty);
  171.     };
  172.  
  173.     const setNotes = (values) => {
  174.         setNotesValues(values);
  175.     };
  176.  
  177.     useEffect(() => {
  178.         const fetchData = async () => {
  179.             const res = await getCountries();
  180.             res.data.map((country) => {
  181.                 setCountriesList((oldArray) => [
  182.                     ...oldArray,
  183.                     { value: country.id, label: country.name },
  184.                 ]);
  185.             });
  186.         };
  187.         fetchData();
  188.     }, []);
  189.  
  190.     // const updateCountry = (name) => {
  191.     //   setShippingValues((val) => [...val, country: name])
  192.     // }
  193.  
  194.     return (
  195.         <Container>
  196.             <div className={st.wrapper}>
  197.                 <div className={st.item}>
  198.                     <div className={st.titleSection}>
  199.                         <H3>Shipping details</H3>
  200.                         {!context.user && (
  201.                             <div className={st.loginInfo}>
  202.                                 <Button underline onClick={() => setAuthVisible()}>
  203.                                     Log in
  204.                                 </Button>
  205.                                 <P>for faster checkout</P>
  206.                             </div>
  207.                         )}
  208.                         {/* {JSON.stringify(shippingValues, null, 4)} */}
  209.                     </div>
  210.                     <div className={st.formWrapper}>
  211.                         <div className={st.formTitle}>
  212.                             <H6>We’ll ship to this address</H6>
  213.                             <div className={st.deliveryInfo}>
  214.                                 <P>Delivery</P>
  215.                                 <svg
  216.                                     width='2'
  217.                                     height='26'
  218.                                     viewBox='0 0 2 26'
  219.                                     fill='none'
  220.                                     xmlns='http://www.w3.org/2000/svg'
  221.                                 >
  222.                                     <path
  223.                                         d='M1 1L0.999999 25'
  224.                                         stroke='#DEDFE0'
  225.                                         stroke-linecap='round'
  226.                                     />
  227.                                 </svg>
  228.  
  229.                                 <Image
  230.                                     src={"/images/checkout/speedy.png"}
  231.                                     className={st.deliveryImage}
  232.                                 />
  233.                                 <Image
  234.                                     src={"/images/checkout/dhl.png"}
  235.                                     className={st.deliveryImage}
  236.                                 />
  237.                             </div>
  238.                         </div>
  239.                         <svg
  240.                             width='100%'
  241.                             height='1'
  242.                             viewBox='0 0 747 1'
  243.                             fill='none'
  244.                             xmlns='http://www.w3.org/2000/svg'
  245.                         >
  246.                             <line y1='0.5' x2='747' y2='0.5' stroke='#EFEFEF' />
  247.                         </svg>
  248.  
  249.                         <Formik
  250.                             enableReinitialize
  251.                             initialValues={initalStateShipping}
  252.                             validationSchema={checkoutValidation}
  253.                             validateOnMount
  254.                             // onSubmit={onSubmit}
  255.                             innerRef={(formikActions) =>
  256.                                 formikActions &&
  257.                                 setShipping(
  258.                                     formikActions.values,
  259.                                     formikActions.errors,
  260.                                     formikActions.isValid
  261.                                 )
  262.                             }
  263.                         >
  264.                             {({ values }) => (
  265.                                 <Form className={st.form}>
  266.                                     {/* {JSON.stringify(values, null, 4)}
  267.                   {JSON.stringify(errors, null, 4)} */}
  268.                                     <Row className={st.row}>
  269.                                         <Col lg={12}>
  270.                                             <Row>
  271.                                                 <Col lg={6}>
  272.                                                     <Input
  273.                                                         label='First name'
  274.                                                         name='first_name'
  275.                                                         placeholder='Your first name'
  276.                                                         mb='24px'
  277.                                                     />
  278.                                                 </Col>
  279.                                                 <Col lg={6}>
  280.                                                     <Input
  281.                                                         label='Last name'
  282.                                                         name='last_name'
  283.                                                         placeholder='Your last name'
  284.                                                         mb='24px'
  285.                                                     />
  286.                                                 </Col>
  287.                                             </Row>
  288.                                             <Input
  289.                                                 label='Your email'
  290.                                                 name='email'
  291.                                                 placeholder='example@email.com'
  292.                                                 mb='24px'
  293.                                             />
  294.                                             <Row>
  295.                                                 <Col lg={7}>
  296.                                                     <Select
  297.                                                         label='Your country'
  298.                                                         name='country_id'
  299.                                                         mb='24px'
  300.                                                         options={countriesList}
  301.                                                     />
  302.                                                 </Col>
  303.                                                 {values?.country_id === 26 ? (
  304.                                                     <>
  305.                                                         <Col lg={5}>
  306.                                                             <Select
  307.                                                                 label='Your city'
  308.                                                                 name='city_id'
  309.                                                                 mb='24px'
  310.                                                                 isSearchable={true}
  311.                                                                 apiPath={"cities"}
  312.                                                             />
  313.                                                         </Col>
  314.                                                     </>
  315.                                                 ) : (
  316.                                                     <>
  317.                                                         <Col lg={5}>
  318.                                                             <Input
  319.                                                                 label='Your city'
  320.                                                                 name='city'
  321.                                                                 placeholder='City'
  322.                                                                 mb='24px'
  323.                                                             />
  324.                                                         </Col>
  325.                                                     </>
  326.                                                 )}
  327.                                             </Row>
  328.  
  329.                                             <Row>
  330.                                                 <Col lg={7}>
  331.                                                     {/* <Select
  332.                             label="State / Province"
  333.                             name="state"
  334.                             placeholder="State / Province"
  335.                             mb="24px"
  336.                             options={optionsText}
  337.                           /> */}
  338.                                                     <Input
  339.                                                         label='State / Province'
  340.                                                         name='state'
  341.                                                         placeholder='State / Province'
  342.                                                         mb='24px'
  343.                                                     />
  344.                                                 </Col>
  345.                                                 <Col lg={5}>
  346.                                                     <Input
  347.                                                         label='Postcode / zip'
  348.                                                         name='zip'
  349.                                                         placeholder='4023'
  350.                                                         mb='24px'
  351.                                                     />
  352.                                                 </Col>
  353.                                             </Row>
  354.  
  355.                                             <Input
  356.                                                 label='Address /street,flr,ap/'
  357.                                                 name='address'
  358.                                                 placeholder='Address'
  359.                                                 mb='24px'
  360.                                             />
  361.  
  362.                                             <Input
  363.                                                 label='Your phone number'
  364.                                                 name='phone'
  365.                                                 placeholder='0899 921 111'
  366.                                                 mb='24px'
  367.                                                 phone
  368.                                             />
  369.                                         </Col>
  370.                                     </Row>
  371.                                 </Form>
  372.                             )}
  373.                         </Formik>
  374.                     </div>
  375.                 </div>
  376.                 {/* {JSON.stringify(payloadFromStorage, null, 4)}
  377.       {JSON.stringify(initialStateInvoice, null, 4)}
  378.       {JSON.stringify(initialStateNotes, null, 4)} */}
  379.                 <div className={st.item}>
  380.                     <div className={st.titleSection}>
  381.                         <H3>Invoice</H3>
  382.                     </div>
  383.                     <div className={st.formWrapper}>
  384.                         {/* {JSON.stringify(invoiceValues, null, 4)} */}
  385.                         <Formik
  386.                             enableReinitialize
  387.                             initialValues={initialStateInvoice}
  388.                             validationSchema={invoiceValidation}
  389.                             validateOnMount
  390.                             // onSubmit={onSubmit}
  391.                             innerRef={(formikActions) =>
  392.                                 formikActions &&
  393.                                 setInvoice(
  394.                                     formikActions.values,
  395.                                     formikActions.errors,
  396.                                     formikActions.isValid
  397.                                 )
  398.                             }
  399.                         >
  400.                             {({ values }) => (
  401.                                 <Form className={st.form}>
  402.                                     {/* {JSON.stringify(values, null, 4)}
  403.                   {JSON.stringify(errors, null, 4)} */}
  404.                                     <CheckBox
  405.                                         label='I want an invoice'
  406.                                         name='has_invoice'
  407.                                         textType='h6'
  408.                                     />
  409.                                     {/* {JSON.stringify(errors, null, 4)} */}
  410.                                     {values.has_invoice && (
  411.                                         <>
  412.                                             <svg
  413.                                                 width='100%'
  414.                                                 height='1'
  415.                                                 viewBox='0 0 747 1'
  416.                                                 fill='none'
  417.                                                 xmlns='http://www.w3.org/2000/svg'
  418.                                                 style={{ marginBottom: "24px" }}
  419.                                             >
  420.                                                 <line y1='0.5' x2='747' y2='0.5' stroke='#EFEFEF' />
  421.                                             </svg>
  422.  
  423.                                             <Row className={st.row}>
  424.                                                 <Col lg={12}>
  425.                                                     <Input
  426.                                                         label='Your company'
  427.                                                         name='company_name'
  428.                                                         placeholder='Your company name'
  429.                                                         mb='24px'
  430.                                                     />
  431.                                                     <CheckBox
  432.                                                         label='VAT registration'
  433.                                                         name='company_has_vat'
  434.                                                     />
  435.                                                     {values.company_has_vat && (
  436.                                                         <Input
  437.                                                             label='VAT number'
  438.                                                             name='company_vat_number'
  439.                                                             placeholder='VAT registration number'
  440.                                                             mb='24px'
  441.                                                         />
  442.                                                     )}
  443.  
  444.                                                     <Input
  445.                                                         label='Company registration number'
  446.                                                         name='company_uin_number'
  447.                                                         placeholder='Company registration number'
  448.                                                         mb='24px'
  449.                                                     />
  450.  
  451.                                                     <Row>
  452.                                                         <Col lg={7}>
  453.                                                             <Select
  454.                                                                 label='Your country'
  455.                                                                 name='company_country_id'
  456.                                                                 mb='24px'
  457.                                                                 options={countriesList}
  458.                                                             />
  459.                                                         </Col>
  460.                                                         <Col lg={5}>
  461.                                                             <Input
  462.                                                                 label='Your city'
  463.                                                                 name='company_city'
  464.                                                                 placeholder='City'
  465.                                                                 mb='24px'
  466.                                                             />
  467.                                                         </Col>
  468.                                                     </Row>
  469.  
  470.                                                     <Input
  471.                                                         label='Address'
  472.                                                         name='company_address'
  473.                                                         placeholder='Address'
  474.                                                         mb='24px'
  475.                                                     />
  476.  
  477.                                                     <Input
  478.                                                         label='Financially responsible person'
  479.                                                         name='company_manager'
  480.                                                         placeholder='Financially responsible person'
  481.                                                         mb='24px'
  482.                                                     />
  483.                                                 </Col>
  484.                                             </Row>
  485.                                         </>
  486.                                     )}
  487.                                 </Form>
  488.                             )}
  489.                         </Formik>
  490.                     </div>
  491.                 </div>
  492.  
  493.                 <div className={st.item}>
  494.                     <div className={st.titleSection}>
  495.                         <H3>Order notes</H3>
  496.                     </div>
  497.                     <div className={st.formWrapper}>
  498.                         {/* {JSON.stringify(invoiceValues, null, 4)} */}
  499.                         <Formik
  500.                             enableReinitialize
  501.                             validateOnMount
  502.                             initialValues={initialStateNotes}
  503.                             innerRef={(formikActions) =>
  504.                                 formikActions && setNotes(formikActions.values)
  505.                             }
  506.                         >
  507.                             {() => (
  508.                                 <Form className={st.form}>
  509.                                     {/* {JSON.stringify(errors, null, 4)} */}
  510.                                     <Row className={st.row}>
  511.                                         <Col lg={12}>
  512.                                             {/* <Input
  513.                         label="Your company"
  514.                         name="company_name"
  515.                         placeholder="Your company name"
  516.                         mb="24px"
  517.                       /> */}
  518.                                             <TextArea
  519.                                                 name='note'
  520.                                                 placeholder={"Add your notes here..."}
  521.                                             />
  522.                                         </Col>
  523.                                     </Row>
  524.                                 </Form>
  525.                             )}
  526.                         </Formik>
  527.                     </div>
  528.                 </div>
  529.  
  530.                 <div className={st.item}>
  531.                     <Button
  532.                         bg
  533.                         size='lg'
  534.                         // onClick={() => submit()}
  535.                         onClick={() => setCheckoutStep(3)}
  536.                         className={st.submitBtn}
  537.                         disabled={
  538.                             Object.keys(checkoutErrors).length !== 0 ||
  539.                             Object.keys(shippingErrors).length !== 0 ||
  540.                             Object.keys(invoiceErrors).length !== 0 ||
  541.                             !shippingDirty ||
  542.                             !invoiceDirty
  543.                         }
  544.                     >
  545.                         Preview and confirm
  546.                     </Button>
  547.                 </div>
  548.             </div>
  549.         </Container>
  550.     );
  551. };
  552.  
  553. export default ShippingAndBilling;
  554.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement