sibmit mesa
This commit is contained in:
@@ -33,31 +33,34 @@ export default async function Page(props: {
|
||||
const numAcount = params?.numAcount ?? null;
|
||||
|
||||
let student: any = null;
|
||||
let platica: any = null;
|
||||
let inscripcion: any[] = [];
|
||||
let errorMessage = "";
|
||||
|
||||
if (numAcount) {
|
||||
const idCuenta = parseInt(numAcount);
|
||||
|
||||
const result = await axios.get(`${envConfig.apiUrl}/bitacora-mesa/cuenta/${idCuenta}`);
|
||||
student = result.data
|
||||
const result = await GetRegisterStudent(idCuenta);
|
||||
if (result.error) {
|
||||
errorMessage = `${result.error}`;
|
||||
} else {
|
||||
student = result[0]?.alumno as Student;
|
||||
const rawPlatica = result[0]?.platica;
|
||||
platica =
|
||||
rawPlatica?.type === "Buffer"
|
||||
? Boolean(rawPlatica.data[0])
|
||||
: Boolean(rawPlatica);
|
||||
console.log(result);
|
||||
}
|
||||
|
||||
const inscResult = await getInscripcion(idCuenta);
|
||||
if (!inscResult.error && Array.isArray(inscResult)) {
|
||||
inscripcion = inscResult;
|
||||
} else {
|
||||
inscripcion = [];
|
||||
}
|
||||
}
|
||||
|
||||
const tableData = Array.isArray(inscripcion)
|
||||
? inscripcion.map((ins) => ({
|
||||
Inscrito: ins.plataforma?.nombre || "—",
|
||||
Tiempo: ins.tiempo_disponible
|
||||
? `${ins.tiempo_disponible} minutos`
|
||||
: "—",
|
||||
Confirmó: ins.platica?.data?.[0] === 1 ? "sí" : "no",
|
||||
}))
|
||||
: [];
|
||||
|
||||
return (
|
||||
<section className="containerSection">
|
||||
{errorMessage && <ShowError message={errorMessage} />}
|
||||
@@ -80,14 +83,12 @@ export default async function Page(props: {
|
||||
<Information
|
||||
NoCuenta={student.id_cuenta}
|
||||
nombre={student.nombre}
|
||||
carrera={student.carrera}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<AsignacionMesas
|
||||
idCuenta={student.id_cuenta}
|
||||
inscripcion={inscripcion}
|
||||
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -3,25 +3,45 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { envConfig } from "@/app/lib/config";
|
||||
import toast from "react-hot-toast";
|
||||
import { getEquipoByCount } from "../lib/getEquipoByCount";
|
||||
|
||||
type Props = {
|
||||
inscripcion: any[];
|
||||
idCuenta: number;
|
||||
};
|
||||
|
||||
export default function AsignacionMesas({
|
||||
idCuenta,
|
||||
inscripcion,
|
||||
}: Props) {
|
||||
export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
|
||||
const [puedeContinuar, setPuedeContinuar] = useState(false);
|
||||
const [mesas, setMesas] = useState<any[]>([]);
|
||||
const [loadingMesas, setLoadingMesas] = useState(false);
|
||||
const [mesaSeleccionada, setMesaSeleccionada] = useState<any>(null);
|
||||
const [tiempo, setTiempo] = useState<number>(15);
|
||||
const [bitacora, setBitacora] = useState<[] | null>(null);
|
||||
|
||||
const fetchByCuenta = async (idCuenta: number) => {
|
||||
const result = await getEquipoByCount(idCuenta);
|
||||
|
||||
if (result?.error) {
|
||||
setBitacora(null);
|
||||
} else {
|
||||
setBitacora(result);
|
||||
toast.error("Ya cuentas con un equipo asignado");
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!idCuenta) return;
|
||||
const Cuenta = idCuenta;
|
||||
if (isNaN(Cuenta)) return;
|
||||
|
||||
fetchByCuenta(Cuenta);
|
||||
}, [idCuenta]);
|
||||
|
||||
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) {
|
||||
@@ -34,16 +54,16 @@ export default function AsignacionMesas({
|
||||
}, [inscripcion]);
|
||||
|
||||
useEffect(() => {
|
||||
console.log(envConfig.apiUrl);
|
||||
console.log(puedeContinuar, idCuenta);
|
||||
if (!puedeContinuar || !idCuenta) return;
|
||||
|
||||
const fetchMesas = async () => {
|
||||
try {
|
||||
setLoadingMesas(true);
|
||||
|
||||
const res = await fetch(
|
||||
`${envConfig.apiUrl}/mesa/activo`,
|
||||
{ cache: "no-store" }
|
||||
);
|
||||
const res = await fetch(`${envConfig.apiUrl}/mesa/activo`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("No se pudieron cargar las mesas");
|
||||
@@ -61,37 +81,77 @@ export default function AsignacionMesas({
|
||||
fetchMesas();
|
||||
}, [puedeContinuar, idCuenta]);
|
||||
|
||||
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");
|
||||
} catch (error) {
|
||||
toast.error("No se pudo asignar la mesa");
|
||||
}
|
||||
};
|
||||
|
||||
if (!puedeContinuar) return null;
|
||||
|
||||
if (bitacora) {
|
||||
return <></>;
|
||||
}
|
||||
return (
|
||||
<div className="containerForm">
|
||||
<label style={{ marginTop: "1rem" }}>
|
||||
Seleccionar tiempo
|
||||
</label>
|
||||
<label style={{ marginTop: "1rem" }}>Seleccionar tiempo</label>
|
||||
|
||||
<select>
|
||||
<option>15 minutos</option>
|
||||
<option>30 minutos</option>
|
||||
<option>40 minutos</option>
|
||||
<option>60 minutos</option>
|
||||
<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>
|
||||
|
||||
<label style={{ marginTop: "1rem" }}>
|
||||
Seleccione una mesa
|
||||
</label>
|
||||
<label style={{ marginTop: "1rem" }}>Seleccione una mesa</label>
|
||||
|
||||
<div className="groupInput">
|
||||
<select disabled={loadingMesas || mesas.length === 0}>
|
||||
<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>
|
||||
)}
|
||||
|
||||
<option value={0}>Selecciona una mesa</option>
|
||||
{!loadingMesas &&
|
||||
mesas.map((mesa) => (
|
||||
<option key={mesa.idMesa} value={mesa.idMesa}>
|
||||
Mesa {mesa.idMesa}
|
||||
<option key={mesa.id_mesa} value={mesa.id_mesa}>
|
||||
Mesa {mesa.id_mesa}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
@@ -99,6 +159,7 @@ export default function AsignacionMesas({
|
||||
<button
|
||||
className="button buttonSearch"
|
||||
disabled={loadingMesas || mesas.length === 0}
|
||||
onClick={submit}
|
||||
>
|
||||
Asignar Mesa
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user