184 lines
4.3 KiB
TypeScript
184 lines
4.3 KiB
TypeScript
"use client";
|
|
|
|
import { envConfig } from "@/app/lib/config";
|
|
import { useEffect, useState } from "react";
|
|
|
|
import axios from "axios";
|
|
import Cookies from "js-cookie";
|
|
|
|
import "./tableEquipos.css";
|
|
|
|
type AreaUbicacion = {
|
|
id_area_ubicacion: number;
|
|
area: string;
|
|
};
|
|
|
|
type Equipo = {
|
|
id_equipo: number;
|
|
nombre_equipo: string;
|
|
ubicacion: string;
|
|
plataforma: {
|
|
nombre: string
|
|
};
|
|
areaUbicacion: {area:string};
|
|
ocupado: boolean;
|
|
activo: {
|
|
data: number[]
|
|
}
|
|
};
|
|
|
|
export default function TableEquipos() {
|
|
const [machines, setMachines] = useState<Equipo[]>([]);
|
|
const [areas, setAreas] = useState<AreaUbicacion[]>([]);
|
|
const [selectedArea, setSelectedArea] = useState("Todo");
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const token = Cookies.get("token");
|
|
const headers = { Authorization: `Bearer ${token}` };
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchEquipos = async () => {
|
|
try {
|
|
const response = await axios.get(`${envConfig.apiUrl}/equipo`,
|
|
{ headers },
|
|
);
|
|
setMachines(response.data);
|
|
} catch (error: any) {
|
|
console.error("ERROR API:", error.response?.data || error.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchEquipos();
|
|
fetchAreas();
|
|
|
|
}, []);
|
|
|
|
const fetchAreas = async () => {
|
|
try {
|
|
const res = await axios.get(
|
|
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`,
|
|
{ headers },
|
|
|
|
);
|
|
|
|
setAreas(res.data);
|
|
} catch (error) {
|
|
console.error("Error cargando áreas:", error);
|
|
}
|
|
};
|
|
|
|
const toggleEquipo = async (id_equipo: number) => {
|
|
try {
|
|
await axios.patch(
|
|
`${envConfig.apiUrl}/equipo/${id_equipo}/activo`,
|
|
{},
|
|
{ headers },
|
|
);
|
|
|
|
setMachines((prev) =>
|
|
prev.map((e) =>
|
|
e.id_equipo === id_equipo
|
|
? {
|
|
...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
|
|
);
|
|
}
|
|
};
|
|
|
|
if (loading) {
|
|
return <p>Cargando equipos...</p>;
|
|
}
|
|
|
|
const filteredMachines =
|
|
selectedArea === "Todo"
|
|
? machines
|
|
: machines.filter(
|
|
(machine) => machine.areaUbicacion.area === selectedArea,
|
|
);
|
|
|
|
|
|
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>
|
|
<tr style={{ position: "relative", zIndex: "0" }}>
|
|
<th />
|
|
<th />
|
|
<th />
|
|
<th>
|
|
<select
|
|
style={{ minWidth: "50px" }}
|
|
value={selectedArea}
|
|
onChange={(e) => setSelectedArea(e.target.value)}
|
|
>
|
|
<option value="Todo">Todo</option>
|
|
{areas.map((area) => (
|
|
<option key={area.id_area_ubicacion} value={area.area}>
|
|
{area.area}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</th>
|
|
<th />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{filteredMachines.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}</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>
|
|
);
|
|
}
|
|
//IO
|