diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-11-01 10:51:20 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-11-01 10:51:20 +0700 |
| commit | cab54d311cd5e89bee34a1bbbfa46bca6b077882 (patch) | |
| tree | 35dded19e134c1c33816099fca1e44e250cf2d04 /src/lib | |
| parent | ff388c53fcd23d108ae81b055caf59ec9e731617 (diff) | |
<iman> update error to large requesr
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/pengajuan-tempo/component/PengajuanTempo.jsx | 62 |
1 files changed, 55 insertions, 7 deletions
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 = () => { </p> </div> <div className='h-[2px] w-full mb-20 bg-gray_r-3' /> - <div className='container mt-10 flex flex-col '> + {isLoading && ( + <div className='fixed inset-0 bg-black bg-opacity-10 flex items-center justify-center z-[999] backdrop-blur-sm'></div> + )} + <div className='container mt-10 flex flex-col'> <div className='flex items-center justify-center'> <Stepper currentStep={currentStep} numberOfSteps={NUMBER_OF_STEPS} /> </div> |
