Files
front-AT/app/Components/Equipos/equipos.tsx
T

255 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-09-24 15:46:04 -06:00
"use client";
import axios from "axios";
import { envConfig } from "@/app/lib/config";
import { useState } from "react";
2026-01-27 17:32:42 -06:00
import toast from "react-hot-toast";
2025-09-24 15:46:04 -06:00
interface Data {
nombre_equipo: string;
id_equipo: number;
id_plataforma: number;
id_area_ubicacion: number;
2026-01-13 18:51:56 -06:00
ubicacion: string;
2026-01-27 17:51:44 -06:00
ip: string;
2026-01-13 18:51:56 -06:00
}
interface Plataforma {
id_plataforma: number;
nombre: string;
}
interface Area {
id_area_ubicacion: number;
area: string;
extra: string;
2025-09-24 15:46:04 -06:00
}
2025-09-23 14:13:23 -06:00
export default function Equipos() {
2026-01-13 18:51:56 -06:00
const [id, setId] = useState("");
const [data, setData] = useState<Data | null>(null);
const [plataforma, setPlataforma] = useState<Plataforma[]>([]);
const [area, setArea] = useState<Area[]>([]);
const [ubicacion, setUbicacion] = useState("");
const [nombre, setNombre] = useState("");
2025-09-24 15:46:04 -06:00
2026-01-13 18:51:56 -06:00
const [idPlataforma, setIdPlataforma] = useState<number | "">("");
const [idArea, setIdArea] = useState<number | "">("");
2026-01-27 17:32:42 -06:00
const [isEditing, setIsEditing] = useState(false);
2026-01-27 17:51:44 -06:00
const [isCreating, setIsCreating] = useState(false);
2026-01-27 17:32:42 -06:00
2026-01-13 18:51:56 -06:00
const handleSubmit = async (e: React.FormEvent) => {
2025-10-01 10:12:41 -06:00
e.preventDefault();
2025-09-24 15:46:04 -06:00
2026-01-13 18:51:56 -06:00
const [equipoRes, plataformaRes, areaRes] = await Promise.all([
axios.get(`${envConfig.apiUrl}/equipo/${id}`),
axios.get(`${envConfig.apiUrl}/alumno-inscrito/plataforma`),
2026-01-27 17:32:42 -06:00
axios.get(`${envConfig.apiUrl}/area-ubicacion`),
2026-01-13 18:51:56 -06:00
]);
const equipo = equipoRes.data as Data;
setData(equipo);
setNombre(equipo.nombre_equipo);
setUbicacion(equipo.ubicacion);
setIdPlataforma(equipo.id_plataforma);
setIdArea(equipo.id_area_ubicacion);
setPlataforma(plataformaRes.data);
setArea(areaRes.data);
2025-09-24 15:46:04 -06:00
};
2026-01-27 17:32:42 -06:00
const handleSave = async () => {
if (!data) return;
try {
await axios.patch(`${envConfig.apiUrl}/equipo`, {
id_equipo: data.id_equipo,
nombre_equipo: nombre,
ubicacion: ubicacion,
id_plataforma: idPlataforma,
id_area_ubicacion: idArea,
2026-01-27 17:51:44 -06:00
ip: data.ip,
2026-01-27 17:32:42 -06:00
});
// Actualiza el estado local
setData({
...data,
nombre_equipo: nombre,
ubicacion,
id_plataforma: idPlataforma as number,
id_area_ubicacion: idArea as number,
});
setIsEditing(false);
toast.success("Equipo actualizado correctamente");
} catch (error) {
console.error(error);
toast.error("Error al guardar el equipo");
}
};
2026-01-27 17:51:44 -06:00
const handleNuevo = async () => {
try {
const [plataformaRes, areaRes] = await Promise.all([
axios.get(`${envConfig.apiUrl}/alumno-inscrito/plataforma`),
axios.get(`${envConfig.apiUrl}/area-ubicacion`),
]);
setPlataforma(plataformaRes.data);
setArea(areaRes.data);
setData({
id_equipo: 0,
nombre_equipo: "",
ubicacion: "",
id_plataforma: 0,
id_area_ubicacion: 0,
ip: "",
});
setNombre("");
setUbicacion("");
setIdPlataforma("");
setIdArea("");
setIsCreating(true);
setIsEditing(true);
} catch (error) {
toast.error("Error al preparar formulario");
}
};
const handleCreate = async () => {
try {
await axios.post(`${envConfig.apiUrl}/equipo`, {
nombre_equipo: nombre,
ubicacion,
id_plataforma: idPlataforma,
id_area_ubicacion: idArea,
ip: "0.0.0.0",
});
toast.success("Equipo creado correctamente");
setIsCreating(false);
setIsEditing(false);
setData(null);
} catch (error: any) {
const message =
error?.response?.data?.message || "Error al crear el equipo";
toast.error(message);
}
};
2025-09-23 14:13:23 -06:00
return (
2026-01-13 18:51:56 -06:00
<form className="containerForm" onSubmit={handleSubmit}>
2026-01-27 17:51:44 -06:00
{!isCreating && (
<div className="groupInput">
<input
placeholder="Número de Equipo a buscar..."
value={id}
onChange={(e) => setId(e.target.value)}
/>
<button type="submit" className="button buttonSearch">
Buscar
</button>
</div>
)}
2025-09-24 15:46:04 -06:00
2026-01-13 18:51:56 -06:00
{data && (
2025-09-24 15:46:04 -06:00
<div>
2026-01-13 18:51:56 -06:00
<label className="label">Ubicación</label>
2025-09-24 15:46:04 -06:00
<input
type="text"
2026-01-13 18:51:56 -06:00
value={ubicacion}
2026-01-27 17:32:42 -06:00
disabled={!isEditing}
2026-01-13 18:51:56 -06:00
onChange={(e) => setUbicacion(e.target.value)}
2025-09-24 15:46:04 -06:00
/>
<label className="label">Nombre</label>
2026-01-13 18:51:56 -06:00
<input
type="text"
value={nombre}
2026-01-27 17:32:42 -06:00
disabled={!isEditing}
2026-01-13 18:51:56 -06:00
onChange={(e) => setNombre(e.target.value)}
/>
2025-09-24 15:46:04 -06:00
<label className="label">Plataforma</label>
2026-01-13 18:51:56 -06:00
<select
value={idPlataforma}
2026-01-27 17:32:42 -06:00
disabled={!isEditing}
2026-01-13 18:51:56 -06:00
onChange={(e) => setIdPlataforma(Number(e.target.value))}
>
<option value="">Elige</option>
{plataforma.map((plat) => (
<option key={plat.id_plataforma} value={plat.id_plataforma}>
{plat.nombre}
</option>
))}
2025-09-24 15:46:04 -06:00
</select>
2026-01-13 18:51:56 -06:00
<label className="label">Área Ubicación</label>
<select
value={idArea}
2026-01-27 17:32:42 -06:00
disabled={!isEditing}
2026-01-13 18:51:56 -06:00
onChange={(e) => setIdArea(Number(e.target.value))}
>
<option value="">Elige</option>
{area.map((a) => (
<option key={a.id_area_ubicacion} value={a.id_area_ubicacion}>
{a.area}
</option>
))}
2025-09-24 15:46:04 -06:00
</select>
2026-01-13 18:51:56 -06:00
<div className="containerButton" style={{ marginTop: "1rem" }}>
2026-01-27 17:32:42 -06:00
{isEditing ? (
<>
<button
type="button"
className="button buttonSearch"
2026-01-27 17:51:44 -06:00
onClick={isCreating ? handleCreate : handleSave}
2026-01-27 17:32:42 -06:00
>
Guardar
</button>
<button
type="button"
className="button buttonCancel"
2026-01-27 17:51:44 -06:00
onClick={() => {
setIsEditing(false);
setIsCreating(false);
setData(null);
}}
2026-01-27 17:32:42 -06:00
>
Cancelar
</button>
</>
) : (
<>
2026-01-27 17:51:44 -06:00
<button
type="button"
className="button buttonCharge"
onClick={handleNuevo}
>
2026-01-27 17:32:42 -06:00
Nuevo
</button>
<button
type="button"
className="button buttonSearch"
onClick={() => setIsEditing(true)}
>
Editar
</button>
</>
)}
2025-09-24 15:46:04 -06:00
</div>
</div>
)}
2025-09-23 14:13:23 -06:00
</form>
);
}