From 5f33ecd5aafd86c51f67620529c0d23754b322cb Mon Sep 17 00:00:00 2001 From: IO420 <320154041@pcpuma.acatlan.unam.mx> Date: Tue, 27 Jan 2026 16:28:34 -0600 Subject: [PATCH] added filter --- app/(private)/Monitor/MachineTable.tsx | 101 +++++++++++------- app/(private)/Monitor/MachineTableActive.tsx | 105 ++++++++++++------- 2 files changed, 130 insertions(+), 76 deletions(-) diff --git a/app/(private)/Monitor/MachineTable.tsx b/app/(private)/Monitor/MachineTable.tsx index 8f69da3..fb4518a 100644 --- a/app/(private)/Monitor/MachineTable.tsx +++ b/app/(private)/Monitor/MachineTable.tsx @@ -3,48 +3,61 @@ import { useEffect, useState } from "react"; import styles from "./Page.module.css"; +type AreaUbicacion = { + id_area_ubicacion: number; + area: string; +}; + type Equipo = { id_equipo: number; nombre_equipo: string; ubicacion: string; - activo: { - data: number[]; - }; - plataforma: { - nombre: string; - }; - areaUbicacion: { - area: string; - }; + activo: { data: number[] }; + plataforma: { nombre: string }; + areaUbicacion: { area: string }; }; export default function MachineTable() { const [machines, setMachines] = useState([]); + const [areas, setAreas] = useState([]); + const [selectedArea, setSelectedArea] = useState("Todo"); const [loading, setLoading] = useState(true); const fetchMachines = async () => { - try { - const res = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/equipo/disable`, - { cache: "no-store" }, - ); - const data = await res.json(); - setMachines(data); - } catch (error) { - console.error("Error al cargar equipos", error); - } finally { - setLoading(false); - } + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/equipo/disable`, + { cache: "no-store" }, + ); + setMachines(await res.json()); + setLoading(false); + }; + + const fetchAreas = async () => { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`, + { cache: "no-store" }, + ); + setAreas(await res.json()); }; useEffect(() => { fetchMachines(); + fetchAreas(); }, []); if (loading) { return

Cargando equipos...

; } + const filteredMachines = + selectedArea === "Todo" + ? machines + : machines.filter( + (machine) => machine.areaUbicacion.area === selectedArea, + ); + + if (loading) return

Cargando equipos...

; + return (
@@ -56,24 +69,40 @@ export default function MachineTable() { + + + - {machines.map((machine) => { - const disponible = machine.activo.data[0] === 1; - - return ( - - - - - - - - ); - })} + {filteredMachines.map((machine) => ( + + + + + + + + ))}
Área Disponible
+ + + + + +
{machine.ubicacion}{machine.nombre_equipo} - {machine.plataforma.nombre} - {machine.areaUbicacion.area}{disponible ? "Sí" : "No"}
{machine.ubicacion}{machine.nombre_equipo} + {machine.plataforma.nombre} + {machine.areaUbicacion.area}{machine.activo.data[0] === 1 ? "Sí" : "No"}
diff --git a/app/(private)/Monitor/MachineTableActive.tsx b/app/(private)/Monitor/MachineTableActive.tsx index 29169ed..32ae172 100644 --- a/app/(private)/Monitor/MachineTableActive.tsx +++ b/app/(private)/Monitor/MachineTableActive.tsx @@ -3,77 +3,102 @@ import { useEffect, useState } from "react"; import styles from "./Page.module.css"; +type AreaUbicacion = { + id_area_ubicacion: number; + area: string; +}; + type Equipo = { id_equipo: number; nombre_equipo: string; ubicacion: string; - activo: { - data: number[]; - }; - plataforma: { - nombre: string; - }; - areaUbicacion: { - area: string; - }; + activo: { data: number[] }; + plataforma: { nombre: string }; + areaUbicacion: { area: string }; }; export default function MachineTableActive() { const [machines, setMachines] = useState([]); + const [areas, setAreas] = useState([]); + const [selectedArea, setSelectedArea] = useState("Todo"); const [loading, setLoading] = useState(true); const fetchMachines = async () => { - try { - const res = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/equipo/active`, - { cache: "no-store" }, - ); - const data = await res.json(); - setMachines(data); - } catch (error) { - console.error("Error al cargar equipos", error); - } finally { - setLoading(false); - } + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/equipo/active`, + { cache: "no-store" }, + ); + setMachines(await res.json()); + setLoading(false); + }; + + const fetchAreas = async () => { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`, + { cache: "no-store" }, + ); + setAreas(await res.json()); }; useEffect(() => { fetchMachines(); + fetchAreas(); }, []); - if (loading) { - return

Cargando equipos...

; - } + const filteredMachines = + selectedArea === "Todo" + ? machines + : machines.filter( + (machine) => machine.areaUbicacion.area === selectedArea, + ); + + if (loading) return

Cargando equipos...

; return (
- + + + + - {machines.map((machine) => { - const disponible = machine.activo.data[0] === 1; - - return ( - - - - - - - - ); - })} + {filteredMachines.map((machine) => ( + + + + + + + + ))}
Ubicación Nombre Equipo Plataforma Área Disponible
+ + + + + +
{machine.ubicacion}{machine.nombre_equipo} - {machine.plataforma.nombre} - {machine.areaUbicacion.area}{disponible ? "Sí" : "No"}
{machine.ubicacion}{machine.nombre_equipo} + {machine.plataforma.nombre} + {machine.areaUbicacion.area}{machine.activo.data[0] === 1 ? "Sí" : "No"}