From 821d218ff687a585c99937948989408541b596e4 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Tue, 26 Nov 2024 09:46:07 +0700
Subject: filter biteship courier sesuikan dengan courier dari odoo

---
 src/lib/checkout/components/Checkout.jsx | 185 ++++++++++++++-----------------
 1 file changed, 84 insertions(+), 101 deletions(-)

(limited to 'src/lib/checkout/components/Checkout.jsx')

diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 92a94834..38e11038 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -35,6 +35,7 @@ import { getVoucher } from '../api/getVoucher';
 import { useAddress } from '../stores/useAdress';
 import SectionExpedition from './SectionExpedition';
 import { useCheckout } from '../stores/stateCheckout';
+import { formatShipmentRange, getToDate } from '../utils/functionCheckouit';
 
 const SELF_PICKUP_ID = 32;
 
@@ -128,15 +129,14 @@ const Checkout = () => {
 
   const [totalWeight, setTotalWeight] = useState(0);
   const [priceCheck, setPriceCheck] = useState(false);
-  const [listExpedisi, setExpedisi] = useState([]);
   const [listserviceExpedisi, setListServiceExpedisi] = useState([]);
   const [selectedExpedisi, setSelectedExpedisi] = useState(0);
   const [selectedCarrierId, setselectedCarrierId] = useState(0);
   const [selectedCarrier, setselectedCarrier] = useState(0);
   const [selectedServiceType, setSelectedServiceType] = useState(null);
   const [selectedExpedisiService, setselectedExpedisiService] = useState(null);
-  const [etd, setEtd] = useState(null);
-  const [etdFix, setEtdFix] = useState(null);
+  // const [etd, setEtd] = useState(null);
+  // const [etdFix, setEtdFix] = useState(null);
   const [bottomPopup, SetBottomPopup] = useState(null);
   const [bottomPopupTnC, SetBottomPopupTnC] = useState(null);
   const [itemTnC, setItemTnC] = useState(null);
@@ -151,7 +151,7 @@ const Checkout = () => {
   const [loadingRajaOngkir, setLoadingRajaOngkir] = useState(false);
   const [grandTotal, setGrandTotal] = useState(0);
 
-  const {checkWeigth, setCheckWeight, hasFlashSale, setHasFlashSale, checkoutValidation, setCheckoutValidation, biayaKirim, setBiayaKirim, products, setProducts} = useCheckout();
+  const {checkWeigth, setCheckWeight, hasFlashSale, setHasFlashSale, checkoutValidation, setCheckoutValidation, biayaKirim, products, setProducts, etd, unit, selectedCourier, selectedService, listExpedisi, setExpedisi} = useCheckout();
 
   const expedisiValidation = useRef(null);
 
@@ -290,6 +290,7 @@ const Checkout = () => {
 
     window.onpopstate = handlePopState;
 
+
     return () => {
       window.onpopstate = null;
     };
@@ -312,70 +313,70 @@ const Checkout = () => {
     setHasFlashSale(hasFlashSale);
   }, [cartCheckout]);
 
-  useEffect(() => {
-    setCheckoutValidation(false);
-    const loadServiceRajaOngkir = async () => {
-      setLoadingRajaOngkir(true);
-      const body = {
-        origin: 2127,
-        destination: selectedAddress.shipping.rajaongkirCityId,
-        weight: totalWeight,
-        courier: selectedCarrier,
-        originType: 'subdistrict',
-        destinationType: 'subdistrict',
-      };
-      setBiayaKirim(0);
-      const dataService = await axios(
-        '/api/rajaongkir-service?body=' + JSON.stringify(body)
-      );
-      setLoadingRajaOngkir(false);
-      setListServiceExpedisi(dataService.data[0].costs);
-      if (dataService.data[0].costs[0]) {
-        setBiayaKirim(dataService.data[0].costs[0]?.cost[0].value);
-        setselectedExpedisiService(
-          dataService.data[0].costs[0]?.description +
-            '-' +
-            dataService.data[0].costs[0]?.service
-        );
-        setEtd(dataService.data[0].costs[0]?.cost[0].etd);
-        toast.success('Harap pilih tipe layanan pengiriman');
-      } else {
-        toast.error('Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.');
-      }
-    };
-    if (selectedCarrier != 0 && selectedCarrier != 1 && totalWeight > 0) {
-      loadServiceRajaOngkir();
-    } else {
-      setListServiceExpedisi();
-      setBiayaKirim(0);
-      setselectedExpedisiService();
-      setEtd();
-    }
-  }, [selectedCarrier, selectedAddress, totalWeight]);
-
-  useEffect(() => {
-    if (selectedServiceType) {
-      let serviceType = selectedServiceType.split(',');
-      setBiayaKirim(serviceType[0]);
-      setselectedExpedisiService(serviceType[1]);
-      setEtd(serviceType[2]);
-    }
-  }, [selectedServiceType]);
-
-  useEffect(() => {
-    if (etd) setEtdFix(calculateEstimatedArrival(etd));
-  }, [etd]);
-
-  useEffect(() => {
-    if (selectedExpedisi) {
-      let serviceType = selectedExpedisi.split(',');
-      if (serviceType[0] === 0) return;
-
-      setselectedCarrier(serviceType[0]);
-      setselectedCarrierId(serviceType[1]);
-      setListServiceExpedisi([]);
-    }
-  }, [selectedExpedisi]);
+  // useEffect(() => {
+  //   setCheckoutValidation(false);
+  //   const loadServiceRajaOngkir = async () => {
+  //     setLoadingRajaOngkir(true);
+  //     const body = {
+  //       origin: 2127,
+  //       destination: selectedAddress.shipping.rajaongkirCityId,
+  //       weight: totalWeight,
+  //       courier: selectedCarrier,
+  //       originType: 'subdistrict',
+  //       destinationType: 'subdistrict',
+  //     };
+  //     setBiayaKirim(0);
+  //     const dataService = await axios(
+  //       '/api/rajaongkir-service?body=' + JSON.stringify(body)
+  //     );
+  //     setLoadingRajaOngkir(false);
+  //     setListServiceExpedisi(dataService.data[0].costs);
+  //     if (dataService.data[0].costs[0]) {
+  //       setBiayaKirim(dataService.data[0].costs[0]?.cost[0].value);
+  //       setselectedExpedisiService(
+  //         dataService.data[0].costs[0]?.description +
+  //           '-' +
+  //           dataService.data[0].costs[0]?.service
+  //       );
+  //       setEtd(dataService.data[0].costs[0]?.cost[0].etd);
+  //       toast.success('Harap pilih tipe layanan pengiriman');
+  //     } else {
+  //       toast.error('Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.');
+  //     }
+  //   };
+  //   if (selectedCarrier != 0 && selectedCarrier != 1 && totalWeight > 0) {
+  //     loadServiceRajaOngkir();
+  //   } else {
+  //     setListServiceExpedisi();
+  //     setBiayaKirim(0);
+  //     setselectedExpedisiService();
+  //     setEtd();
+  //   }
+  // }, [selectedCarrier, selectedAddress, totalWeight]);
+
+  // useEffect(() => {
+  //   if (selectedServiceType) {
+  //     let serviceType = selectedServiceType.split(',');
+  //     setBiayaKirim(serviceType[0]);
+  //     setselectedExpedisiService(serviceType[1]);
+  //     setEtd(serviceType[2]);
+  //   }
+  // }, [selectedServiceType]);
+
+  // useEffect(() => {
+  //   if (etd) setEtdFix(calculateEstimatedArrival(etd));
+  // }, [etd]);
+
+  // useEffect(() => {
+  //   if (selectedExpedisi) {
+  //     let serviceType = selectedExpedisi.split(',');
+  //     if (serviceType[0] === 0) return;
+
+  //     setselectedCarrier(serviceType[0]);
+  //     setselectedCarrierId(serviceType[1]);
+  //     setListServiceExpedisi([]);
+  //   }
+  // }, [selectedExpedisi]);
 
   const poNumber = useRef(null);
   const poFile = useRef(null);
@@ -403,7 +404,7 @@ const Checkout = () => {
       });
       return;
     }
-    if (selectedExpedisi === 0) {
+    if (selectedCourier === 0 || !selectedCourier) {
       setCheckoutValidation(true);
       if (expedisiValidation.current) {
         const position = expedisiValidation.current.getBoundingClientRect();
@@ -414,7 +415,11 @@ const Checkout = () => {
       }
       return;
     }
-    if (selectedCarrier != 1 && biayaKirim == 0) {
+    if(!selectedService){
+      toast.error('Harap pilih tipe layanan pengiriman');
+      return;
+    }
+    if (selectedCourier != 1 && biayaKirim == 0) {
       toast.error('Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.');
       return;
     }
@@ -432,15 +437,14 @@ const Checkout = () => {
       user_id: auth.id,
       order_line: JSON.stringify(productOrder),
       delivery_amount: biayaKirim,
-      carrier_id: selectedCarrierId,
-      estimated_arrival_days: splitDuration(etd),
-      delivery_service_type: selectedExpedisiService,
+      carrier_id: selectedCourier,
+      estimated_arrival_days: getToDate(etd, unit),
+      delivery_service_type: selectedService?.service_type,
       flash_sale: hasFlashSale, // dibuat negasi untuk ngetest kebalikan nilai false
       voucher: activeVoucher,
       voucher_shipping: activeVoucherShipping,
       type: 'sale_order',
     };
-
     if (query) {
       data.source = 'buy';
     }
@@ -487,24 +491,6 @@ const Checkout = () => {
         )}`;
       }
     }
-
-    /* const midtrans = async () => {
-      for (const product of products) deleteItemCart({ productId: product.id });
-      if (grandTotal > 0) {
-        const payment = await axios.post(
-          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
-        );
-        setIsLoading(false);
-        window.location.href = payment.data.redirectUrl;
-      } else {
-        window.location.href = `${
-          process.env.NEXT_PUBLIC_SELF_HOST
-        }/shop/checkout/success?order_id=${isCheckouted.name.replace(
-          /\//g,
-          '-'
-        )}`;
-      }
-    };*/
   };
 
   const handlingActivateCode = async () => {
@@ -569,9 +555,6 @@ const Checkout = () => {
     cartCheckout?.discountVoucher +
     (cartCheckout?.discountVoucherShipping || 0);
 
-
-  console.log('etd', etd, calculateEstimatedArrival(etd), splitDuration(etd));
-
   return (
     <>
       <BottomPopup
@@ -1068,7 +1051,7 @@ const Checkout = () => {
         )}
         <Divider />
         <SectionValidation address={selectedAddress.invoicing} />
-        <SectionExpedisi
+        {/* <SectionExpedisi
           address={selectedAddress.shipping}
           listExpedisi={listExpedisi}
           setSelectedExpedisi={setSelectedExpedisi}
@@ -1081,7 +1064,7 @@ const Checkout = () => {
         <SectionListService
           listserviceExpedisi={listserviceExpedisi}
           setSelectedServiceType={setSelectedServiceType}
-        />
+        /> */}
 
         <div className='p-4 flex flex-col gap-y-4'>
           {!!products &&
@@ -1168,7 +1151,7 @@ const Checkout = () => {
               </div>
               <div className='flex gap-x-2 justify-between'>
                 <div className='text-gray_r-11'>
-                  Biaya Kirim <p className='text-xs mt-1'>{etdFix}</p>
+                  Biaya Kirim <p className='text-xs mt-1'>{formatShipmentRange(etd, unit)}</p>
                 </div>
                 <div>
                   {currencyFormat(
@@ -1367,7 +1350,7 @@ const Checkout = () => {
               {products && <SectionExpedition products={products} />}
               <Divider />
               <SectionValidation address={selectedAddress.invoicing} />
-              <SectionExpedisi
+              {/* <SectionExpedisi
                 address={selectedAddress.shipping}
                 listExpedisi={listExpedisi}
                 setSelectedExpedisi={setSelectedExpedisi}
@@ -1380,7 +1363,7 @@ const Checkout = () => {
               <SectionListService
                 listserviceExpedisi={listserviceExpedisi}
                 setSelectedServiceType={setSelectedServiceType}
-              />
+              /> */}
 
               <div className='p-4'>
                 <div className='font-medium mb-6'>Detail Pesanan</div>
@@ -1474,7 +1457,7 @@ const Checkout = () => {
                     <div className='flex gap-x-2 justify-between'>
                       <div className='text-gray_r-11'>
                         Biaya Kirim
-                        <p className='text-xs mt-1'>{etdFix}</p>
+                        <p className='text-xs mt-1'>{formatShipmentRange(etd, unit)}</p>
                       </div>
                       <div>
                         {currencyFormat(
-- 
cgit v1.2.3

