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/SectionExpedition.jsx | 118 ++++++++++++++--------
 1 file changed, 75 insertions(+), 43 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 27224e5b..be40a577 100644
--- a/src/lib/checkout/components/SectionExpedition.jsx
+++ b/src/lib/checkout/components/SectionExpedition.jsx
@@ -1,4 +1,4 @@
-import { Spinner } from '@chakra-ui/react';
+import { Skeleton, Spinner } from '@chakra-ui/react';
 import axios from 'axios';
 import { AnimatePresence, motion } from 'framer-motion';
 import React, { useState } from 'react';
@@ -14,15 +14,25 @@ function mappingItems(products) {
     name: item.parent.name,
     description: `${item.code} - ${item.name}`,
     value: item.price.priceDiscount,
-    weight: item.weight * 1000 * item.quantity,
+    weight: item.weight * 1000,
     quantity: item.quantity,
     canInstant: item.availableQuantity > item.quantity ? true : false,
   }));
 }
 
-function mappingCourier(couriers, notIncludeInstant = false) {
+function mappingCourier(couriersOdoo, couriers, notIncludeInstant = false) {
+  const validCourierMap = couriersOdoo.reduce((acc, courier) => {
+    acc[courier.label.toLowerCase()] = courier.carrierId;
+    return acc;
+  }, {});
+
   return couriers?.reduce((result, item) => {
-    const { courier_code, courier_service_code } = item;
+    const { courier_name, courier_code, courier_service_code } = item;
+    if (!validCourierMap[courier_name.toLowerCase()]) {
+      return result; // Jika tidak ada, lewati item ini
+    }
+
+
     if (
       notIncludeInstant &&
       ['hours'].includes(item.shipment_duration_unit.toLowerCase())
@@ -30,16 +40,20 @@ function mappingCourier(couriers, notIncludeInstant = false) {
       return result;
     }
 
+    const carrierId = validCourierMap[courier_name];
+
     // Jika courier_code belum ada di result, buat objek baru untuknya
     if (!result[courier_code]) {
       result[courier_code] = {
         courier_name: item.courier_name,
         courier_code: courier_code,
+        courier_id_odoo : carrierId,
         service_type: {
           [courier_service_code]: {
             service_name: item.courier_service_name,
             duration: item.duration,
-            shipment_duration: item.duration,
+            shipment_range: item.shipment_duration_range,
+            shipment_unit: item.shipment_duration_unit,
             price: item.price,
             service_type: item.service_type,
             description: item.description,
@@ -50,6 +64,8 @@ function mappingCourier(couriers, notIncludeInstant = false) {
       result[courier_code].service_type[courier_service_code] = {
         service_name: item.courier_service_name,
         duration: item.duration,
+        shipment_range: item.shipment_duration_range,
+        shipment_unit: item.shipment_duration_unit,
         shipment_duration: item.duration,
         price: item.price,
         service_type: item.service_type,
@@ -92,18 +108,27 @@ function hasCanInstantFalse(items) {
 export default function SectionExpedition({ products }) {
   const { addressMaps, coordinate, postalCode } = useAddress();
   const { control, handleSubmit } = useForm();
-  const [selectedCourier, setSelectedCourier] = useState('');
   const [serviceOptions, setServiceOptions] = useState([]);
-  const [selectedService, setSelectedService] = useState(null);
   const [isOpen, setIsOpen] = useState(false);
+  const [onFocusSelectedCourier, setOnFocuseSelectedCourier] = useState(false);
 
-  const { checkWeigth, checkoutValidation } = useCheckout();
+  const {
+    checkWeigth,
+    checkoutValidation,
+    setBiayaKirim,
+    setUnit,
+    setEtd,
+    selectedCourier,
+    setSelectedCourier,
+    selectedService,
+    setSelectedService,
+    listExpedisi,
+  } = useCheckout();
 
   let destination = {};
   let items = mappingItems(products);
 
   let notIncludeInstant = hasCanInstantFalse(items);
-  console.log('instanCourier', items);
   if (addressMaps) {
     destination = {
       origin_latitude: -6.3031123,
@@ -112,7 +137,7 @@ export default function SectionExpedition({ products }) {
     };
   } else if (postalCode) {
     destination = {
-      origin_postal_code: 12440,
+      origin_postal_code: 14440,
       destination_postal_code: postalCode,
     };
   }
@@ -141,23 +166,26 @@ export default function SectionExpedition({ products }) {
       enabled:
         Boolean(Object.keys(destination).length) &&
         items?.length > 0 &&
-        !checkWeigth,
+        !checkWeigth &&
+        onFocusSelectedCourier,
       staleTime: Infinity,
       cacheTime: Infinity,
     }
   );
 
-  console.log('data', data);
+  const couriers =
+    mappingCourier(listExpedisi, data?.data?.pricing, notIncludeInstant) ||
+    null;
 
-  const couriers = mappingCourier(data?.data?.pricing, true) || null;
+  console.log('ini scourier', data?.data?.pricing)
 
   const onCourierChange = (e) => {
     setIsOpen(false);
     const courier = e.target.value;
     setSelectedService(null);
     setSelectedCourier(courier);
-    console.log('courier', courier);
-    // Menentukan layanan berdasarkan pengiriman yang dipilih
+    // setSelectedCourierId(Object.values(couriers[courier]?.courier_id_odoo);
+    setBiayaKirim(0);
     if (courier && courier !== '0' && courier !== '32' && couriers[courier]) {
       setServiceOptions(Object.values(couriers[courier]?.service_type));
     } else {
@@ -165,12 +193,15 @@ export default function SectionExpedition({ products }) {
     }
   };
 
-  const onSubmit = (data) => {
+  const onSubmit = (data) => {1
     console.log(data);
   };
 
   const handleSelect = (service) => {
     setSelectedService(service);
+    setBiayaKirim(service?.price);
+    setEtd(service?.shipment_range);
+    setUnit(service?.shipment_unit);
     setIsOpen(false);
   };
 
@@ -184,37 +215,38 @@ export default function SectionExpedition({ products }) {
               <select
                 className={`form-input `}
                 onChange={onCourierChange}
+                onMouseDown={() => setOnFocuseSelectedCourier(true)}
                 required
               >
-                <option value='0'>Pilih Pengiriman</option>
-                <option value='32'>SELF PICKUP</option>
-                {couriers &&
-                  Object.values(couriers)?.map((expedisi) => (
-                    <option
-                      value={expedisi.courier_code}
-                      key={expedisi.courier_name}
-                    >
-                      {' '}
-                      {expedisi.courier_name}
+                {!isLoading ? (
+                  <>
+                    <option value='0'>Pilih Pengiriman</option>
+                    <option value='32'>SELF PICKUP</option>
+                    {couriers &&
+                      Object.values(couriers)?.map((expedisi) => (
+                        <option
+                          value={expedisi.courier_code}
+                          key={expedisi.courier_name}
+                        >
+                          {' '}
+                          {expedisi.courier_name}
+                        </option>
+                      ))}
+                  </>
+                ) : (
+                  <>
+                    <option disabled className='skeleton-option'>
+                      <Skeleton height={40} containerClassName='w-full' />
                     </option>
-                  ))}
-              </select>
-
-              <AnimatePresence>
-                {isLoading && (
-                  <motion.div
-                    initial={{ opacity: 0, width: 0 }}
-                    animate={{ opacity: 1, width: '28px' }}
-                    exit={{ opacity: 0, width: 0 }}
-                    transition={{
-                      duration: 0.25,
-                    }}
-                    className='overflow-hidden'
-                  >
-                    <Spinner thickness='3px' speed='0.5s' color='red.500' />
-                  </motion.div>
+                    <option disabled className='skeleton-option'>
+                      <Skeleton height={40} containerClassName='w-full' />
+                    </option>
+                    <option disabled className='skeleton-option'>
+                      <Skeleton height={40} containerClassName='w-full' />
+                    </option>
+                  </>
                 )}
-              </AnimatePresence>
+              </select>
             </div>
             {checkoutValidation && (
               <span className='text-sm text-red-500'>
-- 
cgit v1.2.3

