import { useRef, useState } from 'react'; import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Alert, KeenIcon } from '@/components'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext'; import { apiConfig } from '@/config/api.config'; import axios from 'axios'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; interface PostoAdmsProps { id: number; name: string; } const API_URL = apiConfig.service_master_data; const SearchDialog = () => { const parentRef = useRef(null); const { showSearchDialog, handleSearchDialog, municipios } = useManageMunicipiosContext(); const [alert, setAlert] = useState({ show: false, message: '' }); const initialState = { id: 0, name: '' }; const [formField, setFormField] = useState(initialState); const resetForm = () => { setFormField(initialState); }; const [postoadms, setPostoadms] = useState([]); const [isFound, setIsFound] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const id = Number(formField.id); if (formField.id === 0) { setAlert({ show: true, message: 'Please fill name field.' }); return; } try { const response = await axios.get(`${API_URL}/municipios/postoadms/${id}`); if (response.data.status) { setPostoadms(response.data.data); setIsFound(true); // console.log('Found postoadms: ', response.data.data); } else { setPostoadms([]); setIsFound(false); setAlert({ show: true, message: 'No postoadms found.' }); } } catch (error) { console.error('Error fetching postoadms', error); setAlert({ show: true, message: 'Failed to fetch postoadms. Please try again.' }); } setAlert({ show: false, message: '' }); }; const handleReset = () => { setFormField(initialState); setIsFound(false); setPostoadms([]); }; // console.log(municipios); return ( handleSearchDialog(open)}>

Search Postoadms

{ handleSearchDialog(false); resetForm(); }} >
{alert.show && ( {alert.message} )}
{isFound && postoadms.length > 0 && (

Postu Administravo:


{postoadms.map((posto) => posto.name).join(', ')}
)}
); }; export default SearchDialog;