101 lines
2.5 KiB
TypeScript
101 lines
2.5 KiB
TypeScript
"use client";
|
|
|
|
import { envConfig } from "@/app/lib/config";
|
|
import { useEffect, useState } from "react";
|
|
|
|
import "./tableEquipos.css";
|
|
export default function TableEquipos() {
|
|
const [equipos, setEquipos] = useState<any[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
fetch(`${envConfig.apiUrl}/equipo`)
|
|
.then((res) => res.json())
|
|
.then((data) => {
|
|
setEquipos(data);
|
|
setLoading(false);
|
|
})
|
|
.catch((err) => {
|
|
console.error("ERROR API:", err);
|
|
setLoading(false);
|
|
});
|
|
}, []);
|
|
|
|
const toggleEquipo = async (id_equipo: number) => {
|
|
try {
|
|
await fetch(`${envConfig.apiUrl}/equipo/${id_equipo}/activo`, {
|
|
method: "PATCH",
|
|
});
|
|
|
|
setEquipos((prev) =>
|
|
prev.map((e) =>
|
|
e.id_equipo === id_equipo
|
|
? {
|
|
...e,
|
|
activo: {
|
|
data: [e.activo?.data?.[0] === 1 ? 0 : 1],
|
|
},
|
|
}
|
|
: e,
|
|
),
|
|
);
|
|
} catch (error) {
|
|
console.error("Error al cambiar estado del equipo", error);
|
|
}
|
|
};
|
|
|
|
if (loading) {
|
|
return <p>Cargando equipos...</p>;
|
|
}
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
overflow: "auto",
|
|
height: "430px",
|
|
scrollbarColor: "#2563eb white",
|
|
}}
|
|
>
|
|
<table style={{ width: "100%" }}>
|
|
<thead>
|
|
<tr>
|
|
<th>Ubicación</th>
|
|
<th>Nombre</th>
|
|
<th>Plataforma</th>
|
|
<th>Área</th>
|
|
<th>Activo</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{equipos.map((eq) => {
|
|
const isActivo = eq.activo?.data?.[0] === 1;
|
|
|
|
return (
|
|
<tr key={eq.id_equipo}>
|
|
<td>{eq.ubicacion}</td>
|
|
<td>{eq.nombre_equipo}</td>
|
|
<td className={eq.plataforma?.nombre}>
|
|
{eq.plataforma?.nombre}
|
|
</td>
|
|
<td>{eq.areaUbicacion?.nombre || eq.id_area_ubicacion}</td>
|
|
|
|
<td
|
|
style={{ display: "flex", alignItems: "center", gap: "5px" }}
|
|
>
|
|
<span>{isActivo ? "Sí" : "No"}</span>
|
|
<input
|
|
type="checkbox"
|
|
checked={isActivo}
|
|
style={{ height: "2.5rem" }}
|
|
onChange={() => toggleEquipo(eq.id_equipo)}
|
|
/>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
}
|