From cab54d311cd5e89bee34a1bbbfa46bca6b077882 Mon Sep 17 00:00:00 2001
From: it-fixcomart
Date: Fri, 1 Nov 2024 10:51:20 +0700
Subject: update error to large requesr
---
.../pengajuan-tempo/component/PengajuanTempo.jsx | 62 +++++++++++++++++++---
1 file changed, 55 insertions(+), 7 deletions(-)
(limited to 'src/lib')
diff --git a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
index 5c88e9bf..772807a2 100644
--- a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
+++ b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
@@ -25,6 +25,7 @@ import { toast } from 'react-hot-toast';
const PengajuanTempo = () => {
const [currentStep, setCurrentStep] = React.useState(0);
const NUMBER_OF_STEPS = 6;
+ const [isLoading, setIsLoading] = useState(false);
const { form, errors, validate, updateForm } = usePengajuanTempoStore();
const { control, watch, setValue } = useForm();
const auth = useAuth();
@@ -169,10 +170,41 @@ const PengajuanTempo = () => {
}
setCurrentStep((prev) => (prev === NUMBER_OF_STEPS - 1 ? prev : prev + 1));
};
+
+ const splitDataAndSend = async (data, partSize = 50000) => {
+ const totalDataSize = JSON.stringify(data).length;
+
+ if (totalDataSize <= partSize) {
+ return await createPengajuanTempoApi(data);
+ }
+ const dataParts = [];
+ let currentIndex = 0;
+
+ while (currentIndex < totalDataSize) {
+ const part = JSON.stringify(data).slice(
+ currentIndex,
+ currentIndex + partSize
+ );
+ dataParts.push(part);
+ currentIndex += partSize;
+ }
+
+ const responses = [];
+ for (let i = 0; i < dataParts.length; i++) {
+ const partData = {
+ data: dataParts[i],
+ part: i + 1,
+ totalParts: dataParts.length,
+ };
+ responses.push(await createPengajuanTempoApi(partData));
+ }
+
+ return responses;
+ };
+
const handleDaftarTempo = async () => {
for (const error of stepDivsError) {
if (error.length > 0) {
- console.log('error', error);
return;
}
}
@@ -200,11 +232,24 @@ const PengajuanTempo = () => {
formSupplier: JSON.stringify(productOrder),
user_id: auth.id,
};
- const address = await createPengajuanTempoApi(data2);
- if (address) {
- toast.success('Pengajuan tempo berhasil dilakukan');
- // removeFromLocalStorage();
- router.push('/pengajuan-tempo/finish?tempo_id=SO-2023-06480');
+ const toastId = toast.loading('Mengirimkan formulir pengajuan tempo...');
+ setIsLoading(true);
+ try {
+ const address = await splitDataAndSend(data2);
+ toast.dismiss(toastId);
+ setIsLoading(false);
+
+ if (address) {
+ toast.success('Pengajuan tempo berhasil dilakukan');
+ // removeFromLocalStorage();
+ router.push('/pengajuan-tempo/finish?tempo_id=SO-2023-06480');
+ }
+ } catch (error) {
+ toast.dismiss(toastId);
+ setIsLoading(false);
+
+ toast.error('Terjadi kesalahan dalam pengiriman formulir');
+ console.error(error);
}
};
@@ -243,7 +288,10 @@ const PengajuanTempo = () => {
-
+ {isLoading && (
+
+ )}
+