Files

125 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2026-01-23 19:34:03 -06:00
"use client";
import { useEffect, useState } from "react";
import styles from "./Page.module.css";
2026-02-23 12:47:08 -06:00
import axios from "axios";
import Cookies from "js-cookie";
2026-01-23 19:34:03 -06:00
2026-01-27 16:28:34 -06:00
type AreaUbicacion = {
id_area_ubicacion: number;
area: string;
};
2026-01-23 19:34:03 -06:00
type Equipo = {
id_equipo: number;
nombre_equipo: string;
ubicacion: string;
2026-01-27 16:56:35 -06:00
plataforma: string;
area: string;
2026-02-23 12:47:08 -06:00
ocupado: boolean;
2026-01-23 19:34:03 -06:00
};
export default function MachineTableActive() {
const [machines, setMachines] = useState<Equipo[]>([]);
2026-01-27 16:28:34 -06:00
const [areas, setAreas] = useState<AreaUbicacion[]>([]);
const [selectedArea, setSelectedArea] = useState("Todo");
2026-01-23 19:34:03 -06:00
const [loading, setLoading] = useState(true);
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-01-23 19:34:03 -06:00
const fetchMachines = async () => {
2026-02-23 12:47:08 -06:00
try {
2026-02-23 12:47:08 -06:00
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/equipo/active`,
{ headers },
2026-02-23 12:47:08 -06:00
);
setMachines(res.data);
} catch (error) {
console.error("Error cargando equipos:", error);
} finally {
setLoading(false);
}
2026-01-27 16:28:34 -06:00
};
const fetchAreas = async () => {
2026-02-23 12:47:08 -06:00
try {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`,
{ headers },
2026-02-23 12:47:08 -06:00
);
setAreas(res.data);
} catch (error) {
console.error("Error cargando áreas:", error);
}
2026-01-23 19:34:03 -06:00
};
useEffect(() => {
fetchMachines();
2026-01-27 16:28:34 -06:00
fetchAreas();
2026-01-23 19:34:03 -06:00
}, []);
2026-01-27 16:28:34 -06:00
const filteredMachines =
selectedArea === "Todo"
? machines
: machines.filter(
2026-02-23 12:47:08 -06:00
(machine) => machine.area === selectedArea,
);
2026-01-27 16:28:34 -06:00
if (loading) return <p>Cargando equipos...</p>;
2026-01-23 19:34:03 -06:00
return (
<div className={styles.tableContainer}>
<table className={styles.machineTable}>
<thead>
2026-01-27 16:28:34 -06:00
<tr>
2026-01-23 19:34:03 -06:00
<th>Ubicación</th>
<th>Nombre Equipo</th>
<th>Plataforma</th>
<th>Área</th>
<th>Disponible</th>
</tr>
2026-02-23 12:47:08 -06:00
<tr style={{ position: "relative", zIndex: "0" }}>
2026-01-27 16:28:34 -06:00
<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>
2026-01-23 19:34:03 -06:00
</thead>
<tbody>
2026-01-27 16:28:34 -06:00
{filteredMachines.map((machine) => (
2026-02-23 12:47:08 -06:00
<tr key={machine.id_equipo} className={!machine.ocupado ? '' : styles.ocupado}>
2026-01-27 16:28:34 -06:00
<td>{machine.ubicacion}</td>
<td>{machine.nombre_equipo}</td>
2026-01-27 16:56:35 -06:00
<td className={styles[machine.plataforma]}>
{machine.plataforma}
2026-01-27 16:28:34 -06:00
</td>
2026-01-27 16:56:35 -06:00
<td>{machine.area}</td>
<td>{!machine.ocupado ? "Sí" : "No"}</td>
2026-01-27 16:28:34 -06:00
</tr>
))}
2026-01-23 19:34:03 -06:00
</tbody>
</table>
</div>
);
}
2026-02-23 12:47:08 -06:00
//IO