Files
front-AT/app/Components/AsignacionMesas.tsx
T

195 lines
5.2 KiB
TypeScript
Raw Normal View History

2025-10-01 00:42:35 -06:00
"use client";
2026-01-16 15:47:16 -06:00
import { useEffect, useState } from "react";
import { envConfig } from "@/app/lib/config";
import toast from "react-hot-toast";
2026-01-28 18:19:59 -06:00
import { getEquipoByCount } from "../lib/getEquipoByCount";
2026-02-06 10:39:20 -06:00
import { getMesaByCount } from "../lib/getMesaByCount";
2026-02-11 15:09:45 -05:00
import Swal from "sweetalert2";
2026-01-13 18:51:56 -06:00
type Props = {
inscripcion: any[];
2026-01-28 17:29:07 -06:00
idCuenta: number;
};
2026-01-28 18:19:59 -06:00
export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
2026-01-22 14:07:59 -06:00
const [puedeContinuar, setPuedeContinuar] = useState(false);
const [mesas, setMesas] = useState<any[]>([]);
const [loadingMesas, setLoadingMesas] = useState(false);
2026-01-28 18:19:59 -06:00
const [mesaSeleccionada, setMesaSeleccionada] = useState<any>(null);
const [tiempo, setTiempo] = useState<number>(15);
2026-02-06 11:03:01 -06:00
const [bitacora, setBitacora] = useState<[] | null>([]);
2026-02-17 17:14:27 -06:00
const [error, setError] = useState<string>();
2026-01-28 18:19:59 -06:00
const fetchByCuenta = async (idCuenta: number) => {
2026-02-06 10:54:04 -06:00
const [result, resultMesa] = await Promise.all([
getEquipoByCount(idCuenta),
getMesaByCount(idCuenta),
]);
2026-01-28 18:19:59 -06:00
2026-02-06 11:03:01 -06:00
if (result?.error && resultMesa?.error) {
2026-01-28 18:19:59 -06:00
setBitacora(null);
} else {
2026-02-06 10:54:04 -06:00
setBitacora([]);
2026-02-06 11:03:01 -06:00
if (!result?.error) toast.error("Ya cuentas con un equipo asignado");
2026-02-18 19:01:31 -06:00
Swal.fire({
2026-02-18 19:16:00 -06:00
title: "Ya cuentas con un equipo asignado!",
icon: "error",
draggable: true,
});
2026-02-06 11:03:01 -06:00
if (!resultMesa?.error) toast.error("Ya cuentas con una mesa asignada");
2026-02-18 19:16:00 -06:00
Swal.fire({
title: "Ya cuentas con mesa asignada!",
icon: "error",
draggable: true,
});
2026-02-06 10:39:20 -06:00
}
2026-01-28 18:19:59 -06:00
};
2026-02-06 10:39:20 -06:00
useEffect(() => {
2026-01-28 18:19:59 -06:00
if (!idCuenta) return;
const Cuenta = idCuenta;
if (isNaN(Cuenta)) return;
fetchByCuenta(Cuenta);
}, [idCuenta]);
2026-01-13 18:51:56 -06:00
useEffect(() => {
2026-01-22 14:07:59 -06:00
if (!Array.isArray(inscripcion) || inscripcion.length === 0) return;
const todasAsistieron = inscripcion.every(
2026-01-28 18:19:59 -06:00
(ins) => ins.platica?.data?.[0] === 1,
2026-01-22 14:07:59 -06:00
);
if (!todasAsistieron) {
toast.error("El alumno no asistió a todas las pláticas");
setPuedeContinuar(false);
return;
}
setPuedeContinuar(true);
}, [inscripcion]);
useEffect(() => {
if (!puedeContinuar || !idCuenta) return;
const fetchMesas = async () => {
try {
setLoadingMesas(true);
2026-01-28 18:19:59 -06:00
const res = await fetch(`${envConfig.apiUrl}/mesa/activo`, {
cache: "no-store",
});
if (!res.ok) {
throw new Error("No se pudieron cargar las mesas");
}
const data = await res.json();
setMesas(data);
} catch (error) {
toast.error("Error al cargar mesas disponibles");
} finally {
setLoadingMesas(false);
}
2026-01-13 18:51:56 -06:00
};
fetchMesas();
2026-01-22 14:07:59 -06:00
}, [puedeContinuar, idCuenta]);
2026-01-28 18:19:59 -06:00
const submit = async (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
if (!mesaSeleccionada) {
toast.error("Selecciona un mesa");
return;
}
try {
const body = {
tiempo_asignado: tiempo,
id_mesa: Number(mesaSeleccionada),
id_alumno_inscrito: inscripcion[0].id_alumno_inscrito,
};
const res = await fetch(`${envConfig.apiUrl}/bitacora-mesa`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error("Error al asignar mesa");
}
setBitacora([]);
toast.success("Mesa asignada correctamente");
2026-02-11 15:09:45 -05:00
Swal.fire({
2026-02-11 16:28:58 -06:00
title: "Mesa asignada correctamente!",
2026-02-11 15:09:45 -05:00
icon: "success",
2026-02-18 19:16:00 -06:00
draggable: true,
2026-02-11 15:09:45 -05:00
});
2026-01-28 18:19:59 -06:00
} catch (error) {
toast.error("No se pudo asignar la mesa");
}
};
2026-01-22 14:07:59 -06:00
if (!puedeContinuar) return null;
2026-01-13 18:51:56 -06:00
2026-01-28 18:19:59 -06:00
if (bitacora) {
2026-02-17 17:14:27 -06:00
return error === "Mesa"
? <p style={{ margin: "1rem", color: "red" }}>Ya cuentas con una mesa asignada</p>
: error === "Equipo"
? <p style={{ margin: "1rem", color: "red" }}>Ya cuentas con un equipo asignado</p>
: null;
2026-01-28 18:19:59 -06:00
}
2026-02-17 17:14:27 -06:00
2025-10-01 00:42:35 -06:00
return (
<div className="containerForm">
2026-01-28 18:19:59 -06:00
<label style={{ marginTop: "1rem" }}>Seleccionar tiempo</label>
<select
value={tiempo}
onChange={(e) => setTiempo(Number(e.target.value))}
>
<option value={15}>15 minutos</option>
<option value={30}>30 minutos</option>
<option value={40}>40 minutos</option>
<option value={60}>60 minutos</option>
</select>
2026-01-28 18:19:59 -06:00
<label style={{ marginTop: "1rem" }}>Seleccione una mesa</label>
2025-10-01 00:42:35 -06:00
<div className="groupInput">
2026-01-28 18:19:59 -06:00
<select
disabled={loadingMesas || mesas.length === 0}
onChange={(e) => setMesaSeleccionada(e.target.value)}
>
{loadingMesas && <option>Cargando mesas...</option>}
{!loadingMesas && mesas.length === 0 && (
<option>No hay mesas disponibles</option>
)}
2026-01-13 18:51:56 -06:00
2026-01-28 18:19:59 -06:00
<option value={0}>Selecciona una mesa</option>
{!loadingMesas &&
mesas.map((mesa) => (
2026-01-28 18:19:59 -06:00
<option key={mesa.id_mesa} value={mesa.id_mesa}>
Mesa {mesa.id_mesa}
</option>
))}
2025-10-01 00:42:35 -06:00
</select>
<button
className="button buttonSearch"
disabled={loadingMesas || mesas.length === 0}
2026-01-28 18:19:59 -06:00
onClick={submit}
>
Asignar Mesa
2025-10-01 00:42:35 -06:00
</button>
</div>
</div>
2025-10-01 00:42:35 -06:00
);
}
2026-02-11 16:46:25 -06:00
//IO