From c26a0d026886e6f70ea3487b9d83a54d20b9c1e4 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Tue, 28 Jan 2025 09:46:11 +0700
Subject: biteship

---
 src/lib/checkout/components/SectionExpedition.jsx | 59 +++++++++++++++++++----
 1 file changed, 50 insertions(+), 9 deletions(-)

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

diff --git a/src/lib/checkout/components/SectionExpedition.jsx b/src/lib/checkout/components/SectionExpedition.jsx
index 5b4f6bfc..d1844204 100644
--- a/src/lib/checkout/components/SectionExpedition.jsx
+++ b/src/lib/checkout/components/SectionExpedition.jsx
@@ -12,6 +12,9 @@ import { formatShipmentRange } from '../utils/functionCheckouit';
 import Image from 'next/image';
 import toast from 'react-hot-toast';
 
+import odooApi from '@/core/api/odooApi';
+import { getProductsSla } from '../api/checkoutApi';
+
 function mappingItems(products) {
   return products?.map((item) => ({
     name: item.parent.name,
@@ -19,16 +22,19 @@ function mappingItems(products) {
     value: item.price.priceDiscount,
     weight: item.weight * 1000,
     quantity: item.quantity,
-    canInstant: item.availableQuantity > item.quantity ? true : false,
   }));
 }
 
-function reverseMappingCourier(couriersOdoo, couriers) {
+function reverseMappingCourier(couriersOdoo, couriers, includeInstant = false) {
   // Buat peta courier berdasarkan nama courier dari couriers
   const courierMap = couriers.reduce((acc, item) => {
     const { courier_name, courier_code, courier_service_code } = item;
     const key = courier_name.toLowerCase();
 
+    if (!includeInstant && ['hours'].includes(item.shipment_duration_unit.toLowerCase())) {
+      return acc;
+    }
+
     if (!acc[key]) {
       acc[key] = {
         courier_name: item.courier_name,
@@ -166,6 +172,7 @@ export default function SectionExpedition({ products }) {
   const [selectedE, setIsOpenCourier] = useState(false);
   const [onFocusSelectedCourier, setOnFocuseSelectedCourier] = useState(false);
   const [couriers, setCouriers] = useState(null);
+  const [slaProducts, setSlaProducts] = useState(null);
 
   const {
     checkWeigth,
@@ -178,12 +185,13 @@ export default function SectionExpedition({ products }) {
     selectedService,
     setSelectedService,
     listExpedisi,
+    productSla,
+    setProductSla
   } = useCheckout();
 
   let destination = {};
   let items = mappingItems(products);
 
-  let notIncludeInstant = hasCanInstantFalse(items);
   if (addressMaps) {
     destination = {
       origin_latitude: -6.3031123,
@@ -197,6 +205,32 @@ export default function SectionExpedition({ products }) {
     };
   }
 
+  const fetchSlaProducts = async () => {
+    try {
+      const ids = products.map((p) => p.id).join(',')
+      const res = await odooApi('GET', `/api/v1/product/variants/sla?ids=${ids}`)
+
+      setSlaProducts(res);
+    } catch (error) {
+      console.error('Failed to fetch expedition rates:', error);
+    }
+  };
+
+  useEffect(() => {
+    fetchSlaProducts();
+  }, []);
+
+  useEffect(() => {
+    if (slaProducts) {
+      let productSla = slaProducts?.slaDuration
+      if(slaProducts.slaUnit === 'jam') {
+        productSla = 1
+      }
+      setProductSla(productSla);
+    }
+    console.log('ini slaProducts', slaProducts, productSla);
+  }, [slaProducts]);
+
   const fetchExpedition = async () => {
     let body = {
       ...destination,
@@ -230,7 +264,8 @@ export default function SectionExpedition({ products }) {
 
   useEffect(() => {
     if (data) {
-      const couriers = reverseMappingCourier(listExpedisi, data?.data?.pricing);
+      const instant = slaProducts?.includeInstant || false;
+      const couriers = reverseMappingCourier(listExpedisi, data?.data?.pricing, instant);
       setCouriers(couriers);
     }
   }, [data]);
@@ -246,10 +281,15 @@ export default function SectionExpedition({ products }) {
         setSelectedCourier(courier.courier.courier_code);
         setServiceOptions(Object.values(courier.courier.service_type));
       } else {
-        if ((courier.label === 'GRAB' || courier.label === 'GOJEK') && !addressMaps) {
+        if (
+          (courier.label === 'GRAB' || courier.label === 'GOJEK') &&
+          !addressMaps
+        ) {
           toast.error(
-            'Maaf, layanan kurir ' + courier.label + ' tidak tersedia. Karena Anda Belum Melakukan Pengaturan PinPoint Alamat Pegiriman.'
-          )
+            'Maaf, layanan kurir ' +
+              courier.label +
+              ' tidak tersedia. Karena Anda Belum Melakukan Pengaturan PinPoint Alamat Pegiriman.'
+          );
         } else {
           toast.error(
             'Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.'
@@ -280,7 +320,7 @@ export default function SectionExpedition({ products }) {
     setIsOpen(false);
   };
 
-  console.log('ini selectedCourier', selectedCourier);
+  console.log('ini selectedCourier', couriers);
 
   return (
     <form onSubmit={handleSubmit(onSubmit)}>
@@ -427,7 +467,8 @@ export default function SectionExpedition({ products }) {
                             <p className='text-gray-600 text-sm'>
                               {formatShipmentRange(
                                 service.shipment_range,
-                                service.shipment_unit
+                                service.shipment_unit,
+                                productSla
                               )}
                             </p>
                           </div>
-- 
cgit v1.2.3

