Files
front-AT/app/Components/InformacionEquipos/SelectorEquipo.tsx
T
2026-01-27 18:27:33 -06:00

66 lines
1.6 KiB
TypeScript

"use client";
import { envConfig } from "@/app/lib/config";
import axios from "axios";
import { useEffect, useState } from "react";
interface Equipos {
id_equipo: number;
id_plataforma: number;
id_area_ubicacion: number;
nombre_equipo: string;
ubicacion: string;
activo: boolean;
ip: string;
areaUbicacion: { area: string };
plataforma: { nombre: string };
}
interface Props {
onSearch: (ubicacion: string) => void;
}
export default function SelectorEquipo({ onSearch }: Props) {
const [sala, setSala] = useState<Equipos[]>();
const [selected, setSelected] = useState<string>();
const handleChangeEquipo = (e: React.ChangeEvent<HTMLSelectElement>) => {
const value = e.target.value;
setSelected(value);
if (value) {
onSearch(value);
}
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!selected) return;
onSearch(selected);
};
useEffect(() => {
const fetchData = async () => {
const response = await axios.get(`${envConfig.apiUrl}/equipo`);
setSala(response.data);
};
fetchData();
}, []);
return (
<form className="form-container">
<select value={selected} onChange={handleChangeEquipo}>
<option value="">-- Selecciona una equipo --</option>
{sala &&
sala.map((eq) => (
<option key={eq.id_equipo} value={eq.ubicacion}>
{eq.ubicacion} {eq.nombre_equipo} {eq.plataforma.nombre}{" "}
{eq.areaUbicacion.area}
</option>
))}
</select>
</form>
);
}