From f88f457fd1b91298ea8a7f9f396e49660a81e276 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Mon, 17 Mar 2025 09:06:47 +0700
Subject: handle bug service type kurir

---
 src/lib/checkout/components/SectionExpedition.jsx | 47 ++++++++++++++++-------
 1 file changed, 33 insertions(+), 14 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 b261cae0..22d8df32 100644
--- a/src/lib/checkout/components/SectionExpedition.jsx
+++ b/src/lib/checkout/components/SectionExpedition.jsx
@@ -31,7 +31,10 @@ function reverseMappingCourier(couriersOdoo, couriers, includeInstant = false) {
     const { courier_name, courier_code, courier_service_code } = item;
     const key = courier_name.toLowerCase();
 
-    if (!includeInstant && ['hours'].includes(item.shipment_duration_unit.toLowerCase())) {
+    if (
+      !includeInstant &&
+      ['hours'].includes(item.shipment_duration_unit.toLowerCase())
+    ) {
       return acc;
     }
 
@@ -80,7 +83,6 @@ function reverseMappingCourier(couriersOdoo, couriers, includeInstant = false) {
   });
 }
 
-
 function mappingCourier(couriersOdoo, couriers, notIncludeInstant = false) {
   const validCourierMap = couriersOdoo.reduce((acc, courier) => {
     acc[courier.label.toLowerCase()] = courier.carrierId;
@@ -137,7 +139,6 @@ function mappingCourier(couriersOdoo, couriers, notIncludeInstant = false) {
   }, {});
 }
 
-
 // interface CourierService {
 //   courier_name: string;
 //   courier_code: string;
@@ -172,6 +173,7 @@ export default function SectionExpedition({ products }) {
   const [couriers, setCouriers] = useState(null);
   const [slaProducts, setSlaProducts] = useState(null);
   const [addHolidays, setAddHolidays] = useState(0);
+  const [savedServiceOptions, setSavedServiceOptions] = useState([]);
 
   const {
     checkWeigth,
@@ -186,7 +188,7 @@ export default function SectionExpedition({ products }) {
     listExpedisi,
     productSla,
     setProductSla,
-    setSelectedCourierId
+    setSelectedCourierId,
   } = useCheckout();
 
   let destination = {};
@@ -207,8 +209,15 @@ 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}`)
+      let productsMapped = products.map((item) => ({
+        id: item.id,
+        quantity: item.quantity,
+      }));
+
+      let data = {
+        products: JSON.stringify(productsMapped),
+      }
+      const res = await odooApi('POST', `/api/v1/product/variants/sla`, data);
       setSlaProducts(res);
     } catch (error) {
       console.error('Failed to fetch expedition rates:', error);
@@ -221,13 +230,12 @@ export default function SectionExpedition({ products }) {
 
   useEffect(() => {
     if (slaProducts) {
-      let productSla = slaProducts?.slaTotal
-      if(slaProducts.slaUnit === 'jam') {
-        productSla = 1
+      let productSla = slaProducts?.slaTotal;
+      if (slaProducts.slaUnit === 'jam') {
+        productSla = 1;
       }
       setProductSla(productSla);
     }
-    console.log('ini slaProducts', slaProducts, productSla);
   }, [slaProducts]);
 
   const fetchExpedition = async () => {
@@ -264,7 +272,11 @@ export default function SectionExpedition({ products }) {
   useEffect(() => {
     if (data) {
       const instant = slaProducts?.includeInstant || false;
-      const couriers = reverseMappingCourier(listExpedisi, data?.data?.pricing, instant);
+      const couriers = reverseMappingCourier(
+        listExpedisi,
+        data?.data?.pricing,
+        instant
+      );
       setCouriers(couriers);
     }
   }, [data]);
@@ -273,13 +285,12 @@ export default function SectionExpedition({ products }) {
     setIsOpen(false);
     setOnFocuseSelectedCourier(false);
     const courier = code;
-    console.log('ini courier', courier, couriers);
     setSelectedService(null);
     setBiayaKirim(0);
     if (courier !== 0 && courier !== 32) {
       if (courier.courier) {
         setSelectedCourier(courier.courier.courier_code);
-        setSelectedCourierId(courier.carrierId)
+        setSelectedCourierId(courier.carrierId);
         setServiceOptions(Object.values(courier.courier.service_type));
       } else {
         if (
@@ -296,6 +307,7 @@ export default function SectionExpedition({ products }) {
             'Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.'
           );
         }
+        setServiceOptions([]);
       }
     } else {
       setSelectedCourier(courier === 32 ? 'SELF PICKUP' : null);
@@ -321,6 +333,12 @@ export default function SectionExpedition({ products }) {
     setIsOpen(false);
   };
 
+  useEffect(() => {
+  if (serviceOptions.length > 0) {
+    setSavedServiceOptions(serviceOptions);
+  }
+}, [serviceOptions]);
+
   return (
     <form onSubmit={handleSubmit(onSubmit)}>
       <div className='px-4 py-2'>
@@ -420,7 +438,8 @@ export default function SectionExpedition({ products }) {
         )}
       </div>
 
-      {serviceOptions.length > 0 &&
+      {(serviceOptions.length > 0 ||
+        selectedService )&&
         selectedCourier &&
         selectedCourier !== 32 &&
         selectedCourier !== 0 && (
-- 
cgit v1.2.3

