import React, { useState, useEffect, useMemo, useRef } from 'react'; import { Controller, set, useForm } from 'react-hook-form'; import { usePengajuanTempoStoreDokumen } from '../../../../src-migrate/modules/register/stores/usePengajuanTempoStore'; import ProgressBar from '@ramonak/react-progress-bar'; import { UseToastOptions } from '@chakra-ui/react'; import { toast } from 'react-hot-toast'; import getFileBase64 from '@/core/utils/getFileBase64'; const Dokumen = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => { const { control, watch } = useForm(); const { formDokumen, errorsDokumen, validateDokumen, updateFormDokumen, getJumlahDokumenDiisi, } = usePengajuanTempoStoreDokumen(); // const handleInputChange = (event) => { // const { name, value } = event.target; // updateFormDokumen(name, value); // validateDokumen(); // }; const handleInputChange = async (event) => { let fileBase64 = ''; const { name } = event.target; const file = event.target.files?.[0]; // Allowed file extensions const allowedExtensions = ['pdf', 'png', 'jpg', 'jpeg']; let fileExtension = ''; if (file) { fileExtension = file.name.split('.').pop()?.toLowerCase(); // Extract file extension // Check if the file extension is allowed if (!fileExtension || !allowedExtensions.includes(fileExtension)) { toast.error( 'Format file yang diijinkan adalah .pdf, .png, .jpg, atau .jpeg', { duration: 4000 } ); event.target.value = ''; return; } // Check for file size if (file.size > 2000000) { toast.error('Maksimal ukuran file adalah 2MB', { duration: 4000 }); event.target.value = ''; return; } // Convert file to Base64 fileBase64 = await getFileBase64(file); updateFormDokumen(name, file.name, fileExtension, fileBase64); validateDokumen(); } }; const isFormValid = useMemo( () => Object.keys(errorsDokumen).length === 0, [errorsDokumen] ); const dokumenNibRef = useRef(null); const dokumenNpwpRef = useRef(null); const dokumenSppkpRef = useRef(null); const dokumenAktaPerubahanRef = useRef(null); const dokumenKtpDirutRef = useRef(null); const dokumenAktaPendirianRef = useRef(null); const dokumenLaporanKeuanganRef = useRef(null); const dokumenFotoKantorRef = useRef(null); const dokumenTempatBekerjaRef = useRef(null); useEffect(() => { const loadIndustries = async () => { if (!isFormValid) { const options = { behavior: 'smooth', block: 'center', }; if (errorsDokumen.dokumenNib && dokumenNibRef.current) { dokumenNibRef.current.scrollIntoView(options); return; } if (errorsDokumen.dokumenNpwp && dokumenNpwpRef.current) { dokumenNpwpRef.current.scrollIntoView(options); return; } if (errorsDokumen.dokumenSppkp && dokumenSppkpRef.current) { dokumenSppkpRef.current.scrollIntoView(options); return; } if ( errorsDokumen.dokumenAktaPerubahan && dokumenAktaPerubahanRef.current ) { dokumenAktaPerubahanRef.current.scrollIntoView(options); return; } if (errorsDokumen.dokumenKtpDirut && dokumenKtpDirutRef.current) { dokumenKtpDirutRef.current.scrollIntoView(options); return; } if ( errorsDokumen.dokumenAktaPendirian && dokumenAktaPendirianRef.current ) { dokumenAktaPendirianRef.current.scrollIntoView(options); return; } if ( errorsDokumen.dokumenLaporanKeuangan && dokumenLaporanKeuanganRef.current ) { dokumenLaporanKeuanganRef.current.scrollIntoView(options); return; } if (errorsDokumen.dokumenFotoKantor && dokumenFotoKantorRef.current) { dokumenFotoKantorRef.current.scrollIntoView(options); return; } if ( errorsDokumen.dokumenTempatBekerja && dokumenTempatBekerjaRef.current ) { dokumenTempatBekerjaRef.current.scrollIntoView(options); return; } } }; loadIndustries(); }, [buttonSubmitClick, chekValid]); useEffect(() => { validateDokumen(); }, [buttonSubmitClick]); return ( <>
Upload Progress
{parseInt((getJumlahDokumenDiisi() / 9) * 100)} % {getJumlahDokumenDiisi() >= 4 ? getJumlahDokumenDiisi() == 9 ? 'Selesai' : 'Sedikit Lagi' : ''}