diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2025-01-28 09:46:11 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2025-01-28 09:46:11 +0700 |
| commit | c26a0d026886e6f70ea3487b9d83a54d20b9c1e4 (patch) | |
| tree | 2c633d36d5700a6fd61a18f0e292ddce04039849 /src/lib/checkout/components | |
| parent | 00afe24409bf3cd517597e7c59cf1c12535c54c0 (diff) | |
biteship
Diffstat (limited to 'src/lib/checkout/components')
| -rw-r--r-- | src/lib/checkout/components/Checkout.jsx | 5 | ||||
| -rw-r--r-- | src/lib/checkout/components/SectionExpedition.jsx | 59 |
2 files changed, 53 insertions, 11 deletions
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 5f630799..3ad833ec 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -168,6 +168,7 @@ const Checkout = () => { selectedService, listExpedisi, setExpedisi, + productSla } = useCheckout(); const expedisiValidation = useRef(null); @@ -1171,7 +1172,7 @@ const Checkout = () => { <div className='text-gray_r-11'> Biaya Kirim{' '} <p className='text-xs mt-1'> - {formatShipmentRange(etd, unit)} + {formatShipmentRange(etd, unit, productSla)} </p> </div> <div> @@ -1479,7 +1480,7 @@ const Checkout = () => { <div className='text-gray_r-11'> Biaya Kirim <p className='text-xs mt-1'> - {formatShipmentRange(etd, unit)} + {formatShipmentRange(etd, unit, productSla)} </p> </div> <div> 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> |
