import { useCallback, useEffect, useRef, useState } from 'react'; import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext'; import { Alert, useDataGrid } from '@/components'; import { useCallApi } from '@/hooks'; import { getAuth } from '@/auth'; import { apiConfig } from '@/config/api.config'; import { toast } from 'sonner'; import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command'; interface MunicipioProps { id: number; name: string; } const API_URL = apiConfig.service_master_data; const EditDialog = () => { const parentRef = useRef(null); const { showEditDialog, handleEditDialog, selectedPostoAdms, postoAdms } = useManagePostoAdmsContext(); const { reload } = useDataGrid(); const { PutData, GetData } = useCallApi(); const parsedUser = getAuth()?.user; const [open, setOpen] = useState(false); const [municipios, setMunicipios] = useState([]); const [alert, setAlert] = useState({ show: false, message: '' }); const initialState = { name: '', municipio_id: 0, updated_by: '', updated_at: '' }; const [formField, setFormField] = useState(initialState); const created_time = new Date(); const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' '); const resetForm = () => { setFormField(initialState); setAlert({ show: false, message: '' }); }; const doUpdatePostoAdm = useCallback( async (e: React.FormEvent) => { e.preventDefault(); const response = await PutData(`${API_URL}/postoadms/update/${selectedPostoAdms}`, formField); if (response?.status) { handleEditDialog(false, null); resetForm(); toast.success('Success Update Posto Adm'); reload(); } else { toast.error('Error Update Posto Adm'); setAlert({ show: true, message: 'Failed to update posto adm. Please try again.' }); } }, [selectedPostoAdms, formField] ); const handleUpdate = (e: React.FormEvent) => { e.preventDefault(); if (formField.name.trim() === '' || formField.municipio_id === 0) { setAlert({ show: true, message: 'Please fill in all required fields.' }); return; } doUpdatePostoAdm(e); console.log(formField); setAlert({ show: false, message: '' }); }; useEffect(() => { if (selectedPostoAdms) { setFormField({ ...formField, updated_by: parsedUser?.username, updated_at: formattedTime }); } }, [formattedTime]); useEffect(() => { try { const fetchMunicipios = async (sorting: any) => { sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; const response = await GetData(`${API_URL}/municipios/list`, { limit: 100, page: 1, with_deleted: false, order_field: sorting[0].id, order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' }); // console.log(response?.data); setMunicipios(response?.data.list); }; fetchMunicipios([{ id: 'name', desc: false }]); } catch (error) { console.error('Error fetching municipios', error); } }, []); // console.log(selectedPostoAdms); return ( handleEditDialog(open, null)}> Posto Adm - Update
{alert.show && (

{alert.message}

)}
setFormField({ ...formField, name: e.target.value })} />
No Municipio found. {municipios.map((municipio) => ( { setFormField({ ...formField, municipio_id: municipio.id }); setOpen(false); }} > {municipio.name} ))}
); }; export default EditDialog;