diff --git a/app/(private)/AsignacionEquipo/PlaticaGate.tsx b/app/(private)/AsignacionEquipo/PlaticaGate.tsx index b85f876..7ce0813 100644 --- a/app/(private)/AsignacionEquipo/PlaticaGate.tsx +++ b/app/(private)/AsignacionEquipo/PlaticaGate.tsx @@ -4,21 +4,48 @@ import { useEffect, useState } from "react"; import { envConfig } from "@/app/lib/config"; import toast from "react-hot-toast"; +import "./asignacion.css"; +import { getEquipoByCount } from "@/app/lib/getEquipoByCount"; + type Props = { inscripcion: any[]; - idCuenta: number; + numAcount: number; }; -export default function PlaticaGate({ inscripcion, idCuenta }: Props) { +export default function PlaticaGate({ inscripcion, numAcount }: Props) { const [puedeContinuar, setPuedeContinuar] = useState(false); const [equipos, setEquipos] = useState([]); const [loadingEquipos, setLoadingEquipos] = useState(false); + const [equipoSeleccionado, setEquipoSeleccionado] = useState(null); + const [open, setOpen] = useState(false); + const [tiempo, setTiempo] = useState(15); + const [bitacora, setBitacora] = useState(null); + + const fetchByCuenta = async (idCuenta: number) => { + const result = await getEquipoByCount(idCuenta); + + if (result?.error) { + toast.error(result.error); + setBitacora(null); + } else { + setBitacora(result); + toast.error("Ya cuentas con un equipo asignado"); + } + }; + + useEffect(() => { + if (!numAcount) return; + const idCuenta = numAcount; + if (isNaN(idCuenta)) return; + + fetchByCuenta(idCuenta); + }, [numAcount]); useEffect(() => { if (!Array.isArray(inscripcion) || inscripcion.length === 0) return; const todasAsistieron = inscripcion.every( - (ins) => ins.platica?.data?.[0] === 1 + (ins) => ins.platica?.data?.[0] === 1, ); if (!todasAsistieron) { @@ -31,17 +58,17 @@ export default function PlaticaGate({ inscripcion, idCuenta }: Props) { }, [inscripcion]); useEffect(() => { - if (!puedeContinuar || !idCuenta) return; + if (!puedeContinuar || !numAcount) return; const fetchEquipos = async () => { try { setLoadingEquipos(true); const res = await fetch( - `${envConfig.apiUrl}/equipo/student/${idCuenta}`, + `${envConfig.apiUrl}/equipo/student/${numAcount}`, { cache: "no-store", - } + }, ); if (!res.ok) { @@ -58,49 +85,120 @@ export default function PlaticaGate({ inscripcion, idCuenta }: Props) { }; fetchEquipos(); - }, [puedeContinuar, idCuenta]); + }, [puedeContinuar, numAcount]); if (!puedeContinuar) return null; + const submit = async (e: React.MouseEvent) => { + e.preventDefault(); + + if (!equipoSeleccionado) { + toast.error("Selecciona un equipo"); + return; + } + + try { + const body = { + tiempo_asignado: tiempo, + ubicacion: equipoSeleccionado.ubicacion ?? null, + id_equipo: equipoSeleccionado.id_equipo, + id_alumno_inscrito: inscripcion[0].id_alumno_inscrito, + }; + + const res = await fetch(`${envConfig.apiUrl}/bitacora`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }); + + if (!res.ok) { + throw new Error("Error al asignar equipo"); + } + + toast.success("Equipo asignado correctamente"); + } catch (error) { + toast.error("No se pudo asignar el equipo"); + } + }; + + if (bitacora) { + return (<> + ); +} + return (
- + - setTiempo(Number(e.target.value))} + > + + + + + - +
- + {open && ( +
+ {equipos.length === 0 && ( +
+ No hay equipos disponibles +
+ )} + + {equipos.map((eq) => ( +
{ + setEquipoSeleccionado(eq); + setOpen(false); + }} + > + {eq.ubicacion} {eq.nombre_equipo} +
+ ))} +
+ )} +
diff --git a/app/(private)/AsignacionEquipo/asignacion.css b/app/(private)/AsignacionEquipo/asignacion.css new file mode 100644 index 0000000..997a905 --- /dev/null +++ b/app/(private)/AsignacionEquipo/asignacion.css @@ -0,0 +1,93 @@ +.customSelect { + border: 1px solid #ccc; + border-radius: 6px; + position: relative; + flex: 1; + width: 100%; + font-size: 14px; +} + +.customSelect.disabled { + opacity: 0.6; + pointer-events: none; +} + +.option { + padding: 10px 12px; + cursor: pointer; + display: flex; + align-items: center; + gap: 8px; + background: #fff; +} + +.option:not(.disabled):hover { + background-color: #f0f0f0; +} + +.option.disabled { + cursor: not-allowed; + color: #999; +} + +.option.active { + background-color: #e6f0ff; + font-weight: 600; +} + +.option:hover { + background-color: #f0f0f0; +} + +/* ICONOS */ +.WINDOWS::before { + content: ""; + width: 20px; + height: 20px; + background: url("/windows.png") no-repeat center / contain; +} + +.MACINTOSH::before { + content: ""; + width: 20px; + height: 20px; + background: url("/apple.png") no-repeat center / contain; +} + +.PROFESORES::before { + content: ""; + width: 20px; + height: 20px; + background: url("/teacher.png") no-repeat center / contain; +} + +.selectHeader { + border-radius: 6px; + padding: 8px 12px; + display: flex; + justify-content: space-between; + align-items: center; + cursor: pointer; + background: #fff; +} + +.placeholder { + color: #999; +} + +.arrow { + font-size: 12px; +} + +.options { + position: absolute; + bottom: 105%; + left: 0; + right: 0; + border: 1px solid #ccc; + border-radius: 6px; + background: #fff; + z-index: 10; + max-height: 250px; + overflow-y: auto; +} \ No newline at end of file diff --git a/app/(private)/AsignacionEquipo/page.tsx b/app/(private)/AsignacionEquipo/page.tsx index 013836d..e7975ae 100644 --- a/app/(private)/AsignacionEquipo/page.tsx +++ b/app/(private)/AsignacionEquipo/page.tsx @@ -112,7 +112,7 @@ export default async function Page(props: { )} diff --git a/app/Components/CheckBoxEquipo.tsx b/app/Components/CheckBoxEquipo.tsx index be09b19..38ae9ca 100644 --- a/app/Components/CheckBoxEquipo.tsx +++ b/app/Components/CheckBoxEquipo.tsx @@ -7,23 +7,7 @@ import axios from "axios"; import Information from "./Global/Information/information"; import toast from "react-hot-toast"; import { useRouter } from "next/navigation"; - -async function getEquipoByCount(idCuenta: number) { - try { - const res = await axios.get( - `${envConfig.apiUrl}/bitacora/cuenta/${idCuenta}`, - ); - return res.data; - } catch (error: any) { - if (axios.isAxiosError(error)) { - return { - error: error.response?.data?.message || "Error al consultar equipo", - }; - } - - return { error: "Error desconocido" }; - } -} +import { getEquipoByCount } from "../lib/getEquipoByCount"; async function getEquipoId(idEquipo: number) { try { diff --git a/app/lib/getEquipoByCount.ts b/app/lib/getEquipoByCount.ts new file mode 100644 index 0000000..a041689 --- /dev/null +++ b/app/lib/getEquipoByCount.ts @@ -0,0 +1,19 @@ +import axios from "axios"; +import { envConfig } from "./config"; + +export async function getEquipoByCount(idCuenta: number) { + try { + const res = await axios.get( + `${envConfig.apiUrl}/bitacora/cuenta/${idCuenta}`, + ); + return res.data; + } catch (error: any) { + if (axios.isAxiosError(error)) { + return { + error: error.response?.data?.message || "Error al consultar equipo", + }; + } + + return { error: "Error desconocido" }; + } +} \ No newline at end of file