From 46ad510d1dc855b3b82491b52e5d179a592062f0 Mon Sep 17 00:00:00 2001 From: IO420 <320154041@pcpuma.acatlan.unam.mx> Date: Tue, 27 Jan 2026 18:27:33 -0600 Subject: [PATCH] get programs by machine --- .../InformacionEquipo/InformacionEquipo.tsx | 7 +-- .../InformacionEquipos/ProgramSelector.tsx | 52 ++++++++++++++++--- .../InformacionEquipos/SelectorEquipo.tsx | 25 +++++---- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/app/(private)/InformacionEquipo/InformacionEquipo.tsx b/app/(private)/InformacionEquipo/InformacionEquipo.tsx index e9524d9..a9fadce 100644 --- a/app/(private)/InformacionEquipo/InformacionEquipo.tsx +++ b/app/(private)/InformacionEquipo/InformacionEquipo.tsx @@ -16,6 +16,7 @@ export default function InformacionEquipo({ defaultKey?: string; }) { const [id, setId] = useState(null); + const [ubicacion, setUbicacion] = useState(null); return (
@@ -36,10 +37,10 @@ export default function InformacionEquipo({ <> { - setId(sala); + setUbicacion(sala); }} /> - , + , ), }, @@ -53,7 +54,7 @@ export default function InformacionEquipo({ setId(sala); }} /> - + ), }, diff --git a/app/Components/InformacionEquipos/ProgramSelector.tsx b/app/Components/InformacionEquipos/ProgramSelector.tsx index 76ff9e4..c32d804 100644 --- a/app/Components/InformacionEquipos/ProgramSelector.tsx +++ b/app/Components/InformacionEquipos/ProgramSelector.tsx @@ -2,6 +2,7 @@ import { envConfig } from "@/app/lib/config"; import axios from "axios"; import { useEffect, useState } from "react"; +import toast from "react-hot-toast"; interface Equipos { id_equipo: number; @@ -18,20 +19,23 @@ interface Programa { programa: string; } -export default function ProgramSelector() { +export default function ProgramSelector({ + ubicacion, + id, +}: { + ubicacion?: string | null; + id?: number | null; +}) { const [equipoSeleccionado, setEquipoSeleccionado] = useState(""); - const [equipos, setEquipos] = useState([]); const [programas, setProgramas] = useState([]); const [checkboxes, setCheckboxes] = useState>({}); useEffect(() => { const fetchData = async () => { - const [equiposRes, programasRes] = await Promise.all([ - axios.get(`${envConfig.apiUrl}/equipo`), + const [programasRes] = await Promise.all([ axios.get(`${envConfig.apiUrl}/programa`), ]); - setEquipos(equiposRes.data); setProgramas(programasRes.data); const initialCheckboxes: Record = {}; @@ -66,9 +70,45 @@ export default function ProgramSelector() { console.log("Programas seleccionados:", programasSeleccionados); }; + useEffect(() => { + if (!programas.length) return; + + const buildCheckboxes = async () => { + const baseCheckboxes: Record = {}; + programas.forEach((p) => { + baseCheckboxes[p.id_programa] = false; + }); + + if (ubicacion) { + try { + const res = await axios.get( + `${envConfig.apiUrl}/programa-equipo/${ubicacion}`, + ); + + res.data.forEach((item: any) => { + baseCheckboxes[item.id_programa] = true; + }); + } catch (error: any) { + if (axios.isAxiosError(error)) { + if (error.response?.status === 404) { + toast.error("Equipo sin programas"); + } else { + toast.error("Error al cargar programas del equipo"); + } + } else { + toast.error("Error inesperado"); + } + } + } + + setCheckboxes(baseCheckboxes); + }; + + buildCheckboxes(); + }, [programas, ubicacion]); + return (
-
{programas.map((prog) => (