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

117 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-01-21 17:01:44 -06:00
"use client";
2026-01-21 17:30:26 -06:00
import { envConfig } from "@/app/lib/config";
2026-01-21 17:01:44 -06:00
import { useEffect, useState } from "react";
2026-02-23 12:47:08 -06:00
import axios from "axios";
import Cookies from "js-cookie";
import "./tableEquipos.css";
2026-02-23 12:47:08 -06:00
2026-01-21 17:01:44 -06:00
export default function TableEquipos() {
const [equipos, setEquipos] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-01-21 17:01:44 -06:00
useEffect(() => {
2026-02-23 12:47:08 -06:00
const fetchEquipos = async () => {
try {
const response = await axios.get(`${envConfig.apiUrl}/equipo`,
{ headers },
);
2026-02-23 12:47:08 -06:00
setEquipos(response.data);
} catch (error: any) {
console.error("ERROR API:", error.response?.data || error.message);
} finally {
2026-01-21 17:01:44 -06:00
setLoading(false);
2026-02-23 12:47:08 -06:00
}
};
fetchEquipos();
2026-01-21 17:01:44 -06:00
}, []);
2026-01-29 12:59:02 -06:00
const toggleEquipo = async (id_equipo: number) => {
try {
2026-02-23 12:47:08 -06:00
await axios.patch(
`${envConfig.apiUrl}/equipo/${id_equipo}/activo`,
{ headers },
2026-02-23 12:47:08 -06:00
);
2026-01-29 12:59:02 -06:00
setEquipos((prev) =>
prev.map((e) =>
e.id_equipo === id_equipo
? {
2026-02-23 12:47:08 -06:00
...e,
activo: {
data: [e.activo?.data?.[0] === 1 ? 0 : 1],
},
}
: e
)
);
} catch (error: any) {
console.error(
"Error al cambiar estado del equipo",
error.response?.data || error.message
2026-01-29 12:59:02 -06:00
);
}
};
2026-01-21 17:01:44 -06:00
if (loading) {
return <p>Cargando equipos...</p>;
}
2025-10-02 20:47:38 -06:00
return (
2026-01-21 17:28:33 -06:00
<div
style={{
overflow: "auto",
height: "430px",
scrollbarColor: "#2563eb white",
}}
>
2026-02-05 13:26:17 -06:00
<table style={{ width: "100%" }}>
2026-01-21 17:28:33 -06:00
<thead>
<tr>
<th>Ubicación</th>
<th>Nombre</th>
<th>Plataforma</th>
<th>Área</th>
<th>Activo</th>
2026-01-21 17:01:44 -06:00
</tr>
2026-01-21 17:28:33 -06:00
</thead>
<tbody>
2026-01-29 12:35:31 -06:00
{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?.area || eq.id_area_ubicacion}</td>
2026-01-29 12:35:31 -06:00
<td
style={{ display: "flex", alignItems: "center", gap: "5px" }}
>
<span>{isActivo ? "Sí" : "No"}</span>
2026-01-29 12:59:02 -06:00
<input
type="checkbox"
checked={isActivo}
style={{ height: "2.5rem" }}
onChange={() => toggleEquipo(eq.id_equipo)}
/>
2026-01-29 12:35:31 -06:00
</td>
</tr>
);
})}
2026-01-21 17:28:33 -06:00
</tbody>
</table>
</div>
2025-10-02 20:47:38 -06:00
);
}
//IO