auto complete alumnos

This commit is contained in:
jorgemike
2025-04-06 16:04:56 -06:00
parent 6c51e326dd
commit a0e86b3873
7 changed files with 468 additions and 323 deletions
+57
View File
@@ -0,0 +1,57 @@
import { CuestionarioResponse } from "@/types/responder-formulario";
import toast from "react-hot-toast";
import { validarRespuesta } from "./validador";
export const validarRespuestasObligatorias = (
cuestionario: CuestionarioResponse | null,
respuestas: Record<number, string | number | Array<string | number>>
): boolean => {
const faltantes: number[] = [];
const errores: { id: number; mensaje: string }[] = [];
cuestionario?.cuestionario.secciones.forEach((seccion) => {
seccion.preguntas.forEach(({ pregunta }) => {
const id = pregunta.id_pregunta;
const valor = respuestas[id];
const tipo = pregunta.tipo_pregunta.tipo_pregunta;
const validacion = pregunta.validacion;
const respondida =
(tipo === 'Abierta' &&
typeof valor === 'string' &&
valor.trim() !== '') ||
(tipo === 'Cerrada' && valor !== undefined) ||
(tipo === 'Multiple' && Array.isArray(valor) && valor.length > 0);
if (!respondida) {
if (pregunta.obligatoria) {
faltantes.push(id);
}
return; // no hay nada más que validar si está vacía
}
if (tipo === 'Abierta' && validacion) {
const resultado = validarRespuesta(valor as string, validacion);
if (!resultado.valido) {
errores.push({ id, mensaje: resultado.mensaje });
}
}
});
});
if (faltantes.length > 0) {
toast.error('Responde todas las preguntas obligatorias antes de enviar.');
return false;
}
if (errores.length > 0) {
toast.error(
`Corrige las respuestas con error de formato:\n${errores
.map((e) => `${e.mensaje}`)
.join('\n')}`
);
return false;
}
return true;
};