import { MouseEvent, useCallback, useEffect, useRef, useState } from 'react'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { useTransactionContext } from '../hooks'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { apiConfig } from '@/config/api.config'; import { Alert, KeenIcon, useDataGrid } from '@/components'; import { toast } from 'sonner'; import { useCallApi } from '@/hooks'; import { doSaveLogActivity } from '@/actions/GlobalActions'; import clsx from 'clsx'; const API_URL = apiConfig.service_disbursement; const UploadBatchDialog = () => { const parentRef = useRef(null); const { showUploadBatchDialog, handleUploadBatchDialog } = useTransactionContext(); const { reload } = useDataGrid(); const { PostData, PostDataFile, GetData } = useCallApi(); const [alert, setAlert] = useState({ show: false, message: '' }); const initialState: { execution_date: string, file: File | null; } = { execution_date: '', file: null } const [formField, setFormField] = useState(initialState); const resetForm = () => { setFormField(initialState); setAlert({ show: false, message: '' }); }; /* actions */ const doUploadBatch = useCallback( async (e: React.FormEvent) => { e.preventDefault(); const formData = new FormData(); formData.append('execution_date', formField.execution_date); if (formField.file) { formData.append('file', formField.file); } try { const response = await PostDataFile(`${API_URL}/upload-excel`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }); if (response?.status) { handleUploadBatchDialog(false); resetForm(); reload(); const createActivity = { module: 'Disbursement', description: `Create New Disbursement => ${formField.file?.name}`, action: 'C' }; doSaveLogActivity(createActivity); toast.success('Success Create Disbursement'); } else { toast.error('Failed to create disbursement'); setAlert({ show: true, message: response?.message ?? 'Failed to create disbursement.' }); } } catch (error) { toast.error('Error uploading batch'); setAlert({ show: true, message: 'Something went wrong. Please try again.' }); } }, [formField] ); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); console.log('Form data before submit:', formField); if ( formField.execution_date.trim() === '' || formField.file === null ) { setAlert({ show: true, message: 'Please fill in all required fields.' }); return; } doUploadBatch(e); // console.log(formField); setAlert({ show: false, message: '' }); }; useEffect(() => { if (showUploadBatchDialog === false) { resetForm(); } }, [showUploadBatchDialog]); return ( handleUploadBatchDialog(open)}>

Upload Batch

{ handleUploadBatchDialog(false); resetForm(); }} >
{alert.show && (

{alert.message}

)}
setFormField((prev) => ({ ...prev, execution_date: target.value })) } />
setFormField((prev) => ({ ...prev, file: target.files?.[0] ?? null })) } />
); }; export { UploadBatchDialog };