From 5559770e0b0e94b68de1e31bf2be5c978362821d Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Tue, 31 Dec 2024 09:01:10 +0700 Subject: logo and mapping courier --- src/lib/checkout/components/Checkout.jsx | 1 + src/lib/checkout/components/SectionExpedition.jsx | 214 ++++++++++++++++------ 2 files changed, 158 insertions(+), 57 deletions(-) (limited to 'src/lib') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 0471dc6c..99c7b40a 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -279,6 +279,7 @@ const Checkout = () => { value: expedisi.id, label: expedisi.name, carrierId: expedisi.deliveryCarrierId, + logo : expedisi.image, })); setExpedisi(dataExpedisi); }; diff --git a/src/lib/checkout/components/SectionExpedition.jsx b/src/lib/checkout/components/SectionExpedition.jsx index 2098d5b5..8b59d210 100644 --- a/src/lib/checkout/components/SectionExpedition.jsx +++ b/src/lib/checkout/components/SectionExpedition.jsx @@ -1,7 +1,7 @@ import { Skeleton, Spinner } from '@chakra-ui/react'; import axios from 'axios'; import { AnimatePresence, motion } from 'framer-motion'; -import React, { useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useQuery } from 'react-query'; import { useAddress } from '../stores/useAdress'; @@ -9,6 +9,8 @@ import { useAddress } from '../stores/useAdress'; import currencyFormat from '@/core/utils/currencyFormat'; import { useCheckout } from '../stores/stateCheckout'; import { formatShipmentRange } from '../utils/functionCheckouit'; +import Image from 'next/image'; +import toast from 'react-hot-toast'; function mappingItems(products) { return products?.map((item) => ({ @@ -21,6 +23,57 @@ function mappingItems(products) { })); } +function reverseMappingCourier(couriersOdoo, couriers) { + // 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 (!acc[key]) { + acc[key] = { + courier_name: item.courier_name, + courier_code: courier_code, + service_type: {}, + }; + } + + acc[key].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, + price: item.price, + service_type: item.service_type, + description: item.description, + }; + + return acc; + }, {}); + + // Iterasi berdasarkan couriersOdoo + return couriersOdoo.map((courierOdoo) => { + const courierNameKey = courierOdoo.label.toLowerCase(); + const carrierId = courierOdoo.carrierId; + + const mappedCourier = courierMap[courierNameKey] || false; + + if (!mappedCourier) { + return { + ...courierOdoo, + courier: false, + }; + } + + return { + ...courierOdoo, + courier: { + ...mappedCourier, + courier_id_odoo: carrierId, + }, + }; + }); +} + function mappingCourier(couriersOdoo, couriers, notIncludeInstant = false) { const validCourierMap = couriersOdoo.reduce((acc, courier) => { acc[courier.label.toLowerCase()] = courier.carrierId; @@ -110,7 +163,9 @@ export default function SectionExpedition({ products }) { const { control, handleSubmit } = useForm(); const [serviceOptions, setServiceOptions] = useState([]); const [isOpen, setIsOpen] = useState(false); + const [selectedE, setIsOpenCourier] = useState(false); const [onFocusSelectedCourier, setOnFocuseSelectedCourier] = useState(false); + const [couriers, setCouriers] = useState(null); const { checkWeigth, @@ -173,22 +228,31 @@ export default function SectionExpedition({ products }) { } ); - const couriers = - mappingCourier(listExpedisi, data?.data?.pricing, notIncludeInstant) || - null; + console.log('ini response', data); - console.log('ini scourier', data?.data?.pricing); + useEffect(() => { + if (data) { + const couriers = reverseMappingCourier(listExpedisi, data?.data?.pricing); + setCouriers(couriers); + console.log('ini scourier', couriers); + } + }, [data]); - const onCourierChange = (e) => { + const onCourierChange = (code) => { setIsOpen(false); - const courier = e.target.value; + setOnFocuseSelectedCourier(false); + const courier = code; setSelectedService(null); - setSelectedCourier(courier); - // 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)); + if (courier !== 0 && courier !== 32) { + if (courier.courier) { + setSelectedCourier(courier.courier.courier_code); + setServiceOptions(Object.values(courier.courier.service_type)); + } else { + toast.error('Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.'); + } } else { + setSelectedCourier(courier === 32 ? 'SELF PICKUP' : null); setServiceOptions([]); } }; @@ -198,6 +262,11 @@ export default function SectionExpedition({ products }) { console.log(data); }; + const handleOnFocuse = (value) => { + setOnFocuseSelectedCourier(!value); + setIsOpen(false); + }; + const handleSelect = (service) => { setSelectedService(service); setBiayaKirim(service?.price); @@ -206,48 +275,79 @@ export default function SectionExpedition({ products }) { setIsOpen(false); }; + console.log('ini selectedCourier', selectedCourier); + return (
Pilih Ekspedisi:
-
-
- +
+
+
+
+
+ {/* Custom Select Input Field */} +
handleOnFocuse(onFocusSelectedCourier)} + > + {selectedCourier ? ( +
+ {selectedCourier} +
+ ) : ( + Pilih Expedisi + )} +
+ + {/* Dropdown Options */} + {onFocusSelectedCourier && ( +
+ {!isLoading ? ( + <> +
onCourierChange(32)} + className='flex justify-between p-2 items-center hover:bg-gray-100 cursor-pointer' + > +
+

SELF PICKUP

+
+
+ {couriers?.map((courier) => ( +
onCourierChange(courier)} + className='flex justify-between p-2 items-center hover:bg-gray-100 cursor-pointer' + > +
+

+ {courier?.label} +

+
+ + {courier?.courier?.courier_name} + +
+ ))} + + ) : ( + <> + + + + )} +
+ )} +
+
+
{checkoutValidation && ( @@ -276,9 +376,9 @@ export default function SectionExpedition({ products }) { )}
- {selectedCourier && - selectedCourier !== '32' && - selectedCourier !== '0' && ( + {serviceOptions.length > 0 && selectedCourier && + selectedCourier !== 32 && + selectedCourier !== 0 && (
Tipe Layanan Ekspedisi:
@@ -294,8 +394,9 @@ export default function SectionExpedition({ products }) { {selectedService.service_name} {currencyFormat( - Math.round(parseInt(selectedService?.price * 1.1) / 1000) * - 1000 + Math.round( + parseInt(selectedService?.price * 1.1) / 1000 + ) * 1000 )}
@@ -305,8 +406,6 @@ export default function SectionExpedition({ products }) { )}
- - {/* Dropdown Options */} {isOpen && (
{serviceOptions.map((service) => ( @@ -328,8 +427,9 @@ export default function SectionExpedition({ products }) {
{currencyFormat( - Math.round(parseInt(service?.price * 1.1) / 1000) * - 1000 + Math.round( + parseInt(service?.price * 1.1) / 1000 + ) * 1000 )}
-- cgit v1.2.3