Advertisement
vallec

Untitled

May 15th, 2024
599
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 { Container, Spinner } from "react-bootstrap";
  7. import OrderSummary from "@/components/OrderSummary";
  8. import { ShopContext } from "@/providers/ShopContext";
  9. import { getCart } from "@/utils/cartApi";
  10. import ShippingAndBilling from "./ShippingAndBilling";
  11. import { AuthContext } from "@/providers/AuthContext";
  12. import { useRouter } from "next/navigation";
  13. import { NotificationContext } from "@/providers/NotificationContext";
  14.  
  15. const Checkout = () => {
  16.   const { context, updateContext } = useContext(ShopContext);
  17.   const { context: authContext } = useContext(AuthContext);
  18.  
  19.   const router = useRouter();
  20.  
  21.   const [cartData, setCartData] = useState(null);
  22.  
  23.   const [loading, setLoading] = useState(false);
  24.  
  25.   useEffect(() => {
  26.     const fetchData = async () => {
  27.       if (context.cart && context.cart.data) {
  28.         setLoading(true);
  29.         const data = await getCart(context.cart.data.cart_id);
  30.         setCartData(data);
  31.         setLoading(false);
  32.       }
  33.     };
  34.     fetchData();
  35.   }, [context.cart]);
  36.  
  37.   const setCheckoutStep = (step) => {
  38.     if (step > 0 && step < 3) {
  39.       updateContext({ checkoutStep: step });
  40.     }
  41.     if (step === 3) {
  42.       // setLoading(true);
  43.       const payload = {
  44.         ...shippingValues,
  45.         ...invoiceValues,
  46.         ...notesValues,
  47.         company_has_vat: invoiceValues.company_has_vat ? 1 : 0,
  48.         has_invoice: invoiceValues.has_invoice ? 1 : 0,
  49.         customer_id: authContext.user ? authContext.user.id : null,
  50.         cart_id: context.cart.data.cart_id,
  51.       };
  52.       // const response = await previewOrder(payload);
  53.  
  54.       router.push("/preview");
  55.       localStorage.setItem("checkoutPayload", JSON.stringify(payload));
  56.       updateContext({ checkoutPayload: payload });
  57.  
  58.       // setLoading(false);
  59.     }
  60.   };
  61.  
  62.   const [shippingValues, setShippingValues] = useState({});
  63.   const [shippingErrors, setShippingErrors] = useState({});
  64.   const [shippingDirty, setShippingDirty] = useState({});
  65.   const [invoiceValues, setInvoiceValues] = useState({});
  66.   const [invoiceErrors, setInvoiceErrors] = useState({});
  67.   const [invoiceDirty, setInvoiceDirty] = useState({});
  68.   const [checkoutValues, setCheckoutValues] = useState({});
  69.   const [checkoutErrors, setCheckoutErrors] = useState({});
  70.   const [notesValues, setNotesValues] = useState({});
  71.  
  72.   return (
  73.     <section className={st.wrapper}>
  74.       <Container className={st.container}>
  75.         <div className={st.row}>
  76.           {/* {JSON.stringify(shippingValues, null, 4)} */}
  77.           <div className={st.cartInfo}>
  78.             <ShippingAndBilling
  79.               cartData={cartData?.data}
  80.               setShippingValues={setShippingValues}
  81.               setInvoiceValues={setInvoiceValues}
  82.               setShippingErrors={setShippingErrors}
  83.               setInvoiceErrors={setInvoiceErrors}
  84.               setShippingDirty={setShippingDirty}
  85.               setInvoiceDirty={setInvoiceDirty}
  86.               checkoutErrors={checkoutErrors}
  87.               shippingErrors={shippingErrors}
  88.               invoiceErrors={invoiceErrors}
  89.               shippingDirty={shippingDirty}
  90.               invoiceDirty={invoiceDirty}
  91.               setCheckoutStep={setCheckoutStep}
  92.               setNotesValues={setNotesValues}
  93.               shippingValues={shippingValues}
  94.             />
  95.           </div>
  96.           <div>
  97.             {/* {JSON.stringify(cartData, null, 4)} */}
  98.             {/* {JSON.stringify(shippingForm, null, 4)} */}
  99.             <OrderSummary
  100.               cartData={cartData?.data}
  101.               setCheckoutStep={setCheckoutStep}
  102.               checkoutStep={2}
  103.               setCheckoutValues={setCheckoutValues}
  104.               setCheckoutErrors={setCheckoutErrors}
  105.               checkoutErrors={checkoutErrors}
  106.               shippingErrors={shippingErrors}
  107.               invoiceErrors={invoiceErrors}
  108.               shippingDirty={shippingDirty}
  109.               invoiceDirty={invoiceDirty}
  110.             />
  111.             {/* {JSON.stringify(checkoutErrors)}
  112.             {JSON.stringify(shippingErrors)}
  113.             {JSON.stringify(invoiceErrors)} */}
  114.           </div>
  115.         </div>
  116.       </Container>
  117.     </section>
  118.   );
  119. };
  120.  
  121. export default Checkout;
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement