summaryrefslogtreecommitdiff
path: root/src/lib/checkout
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2025-01-28 09:46:11 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2025-01-28 09:46:11 +0700
commitc26a0d026886e6f70ea3487b9d83a54d20b9c1e4 (patch)
tree2c633d36d5700a6fd61a18f0e292ddce04039849 /src/lib/checkout
parent00afe24409bf3cd517597e7c59cf1c12535c54c0 (diff)
biteship
Diffstat (limited to 'src/lib/checkout')
-rw-r--r--src/lib/checkout/api/checkoutApi.js11
-rw-r--r--src/lib/checkout/components/Checkout.jsx5
-rw-r--r--src/lib/checkout/components/SectionExpedition.jsx59
-rw-r--r--src/lib/checkout/stores/stateCheckout.js4
-rw-r--r--src/lib/checkout/utils/functionCheckouit.js31
5 files changed, 89 insertions, 21 deletions
diff --git a/src/lib/checkout/api/checkoutApi.js b/src/lib/checkout/api/checkoutApi.js
index fd982fff..9d326b10 100644
--- a/src/lib/checkout/api/checkoutApi.js
+++ b/src/lib/checkout/api/checkoutApi.js
@@ -18,3 +18,14 @@ export const getProductsCheckout = async (query) => {
const result = await odooApi('GET', url);
return result;
};
+
+export const getProductsSla = async ({data}) => {
+ const dataSLA = await odooApi(
+ 'GET',
+ `/api/v1/product/variants/sla`,
+ data
+ )
+
+ console.log('ini sla - data', dataSLA);
+ return dataSLA
+}
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>
diff --git a/src/lib/checkout/stores/stateCheckout.js b/src/lib/checkout/stores/stateCheckout.js
index 4fefbbed..52210d7f 100644
--- a/src/lib/checkout/stores/stateCheckout.js
+++ b/src/lib/checkout/stores/stateCheckout.js
@@ -12,6 +12,7 @@ export const useCheckout = create((set) => ({
selectedCourierId : null,
selectedService : null,
listExpedisi : [],
+ productSla : null,
setCheckWeight : (checkWeigth) => set({ checkWeigth }),
setHasFlashSale : (hasFlashSale) => set({ hasFlashSale }),
setCheckoutValidation : (checkoutValidation) => set({ checkoutValidation }),
@@ -22,7 +23,8 @@ export const useCheckout = create((set) => ({
setSelectedCourier : (selectedCourier) => set({ selectedCourier }),
setSelectedService : (selectedService) => set({ selectedService }),
setSelectedCourierId : (selectedCourierId) => set({ selectedCourierId }),
- setExpedisi : (listExpedisi) => set({ listExpedisi })
+ setExpedisi : (listExpedisi) => set({ listExpedisi }),
+ setProductSla : (productSla) => set({ productSla })
})) \ No newline at end of file
diff --git a/src/lib/checkout/utils/functionCheckouit.js b/src/lib/checkout/utils/functionCheckouit.js
index b299f289..a6e6c337 100644
--- a/src/lib/checkout/utils/functionCheckouit.js
+++ b/src/lib/checkout/utils/functionCheckouit.js
@@ -1,17 +1,27 @@
+import { m } from 'framer-motion';
+import { min } from 'moment/moment';
+
export function formatShipmentRange(
shipmentDurationRange,
- shipmentDurationUnit
+ shipmentDurationUnit,
+ productSLA
) {
if (!shipmentDurationRange || !shipmentDurationUnit) {
return '';
}
let minRange, maxRange;
+ console.log('ini masuk', shipmentDurationRange);
+
// Cek apakah durasi berupa range atau angka tunggal
if (shipmentDurationRange.includes('-')) {
[minRange, maxRange] = shipmentDurationRange.split(' - ').map(Number);
+ // if (minRange === maxRange) {
+ // maxRange = minRange + 3;
+ // }
} else {
- minRange = maxRange = Number(shipmentDurationRange); // Jika angka tunggal
+ minRange = Number(shipmentDurationRange); // Jika angka tunggal
+ maxRange = Number(shipmentDurationRange) + 3;
}
const start = new Date(); // Tanggal saat ini
@@ -21,19 +31,22 @@ export function formatShipmentRange(
// Hitung estimasi berdasarkan unit waktu
if (shipmentDurationUnit === 'days' || shipmentDurationUnit === 'day') {
minDate = new Date(start);
- minDate.setDate(start.getDate() + (minRange + 3));
+ minDate.setDate(start.getDate() + (minRange + productSLA));
maxDate = new Date(start);
- maxDate.setDate(start.getDate() + (maxRange + 3));
+ maxDate.setDate(start.getDate() + (maxRange + productSLA));
} else if (shipmentDurationUnit === 'hours') {
- minDate = new Date(start.getTime() + (minRange + 3) * 60 * 60 * 1000);
- maxDate = new Date(start.getTime() + (maxRange + 3) * 60 * 60 * 1000);
+ minDate = new Date(start);
+ minDate.setDate(start.getDate() + (1 + productSLA));
+
+ maxDate = new Date(start);
+ maxDate.setDate(start.getDate() + (1 + productSLA + 1));
+ // minDate = new Date(start.getTime() + (minRange + 3) * 60 * 60 * 1000);
+ // maxDate = new Date(start.getTime() + (maxRange + 3) * 60 * 60 * 1000);
} else {
throw new Error("Unsupported unit. Please use 'days' or 'hours'.");
}
- console.log('min max', minDate, maxDate);
-
const minDateStr = formatDate(minDate);
const maxDateStr = formatDate(maxDate);
if (minDateStr === maxDateStr) {
@@ -60,7 +73,7 @@ export function getToDate(shipmentDurationRange, shipmentDurationUnit) {
let maxDate;
// Hitung estimasi berdasarkan unit waktu
- if (shipmentDurationUnit === 'days' || shipmentDurationUnit === 'day') {
+ if (shipmentDurationUnit === 'days' || shipmentDurationUnit === 'day') {
maxDate = new Date(start);
maxDate.setDate(start.getDate() + (maxRange + 3));
} else if (shipmentDurationUnit === 'hours') {