Refactor AdminFormPage and Formulario components to remove unused state and improve form handling; update plantilla data structure to include new fields.

This commit is contained in:
jalvarado
2025-05-23 09:57:19 -06:00
parent 20cc8bc4e7
commit 8007d2f409
4 changed files with 81 additions and 108 deletions
+17 -8
View File
@@ -11,7 +11,7 @@ import axiosInstance from '@/utils/api-config';
import { useRouter } from 'next/navigation';
import { getAxiosError } from '@/utils/errors-utils';
interface DatosAlumno {
interface GetAlumnoResponse {
id_ncuenta: number;
nombre: string;
apellidos: string;
@@ -34,6 +34,7 @@ export default function Formulario({
apellidos: string;
correo: string;
genero: 'M' | 'F';
carrera: string;
}>(null);
const [respuestas, setRespuestas] = useState<Record<string, string>>({});
const [isSubmitting, setIsSubmitting] = useState(false); // New state for submission
@@ -47,7 +48,7 @@ export default function Formulario({
(async () => {
try {
setDatosAuto(null);
const { data } = await axiosInstance.get<DatosAlumno>(
const { data } = await axiosInstance.get<GetAlumnoResponse>(
`/alumnos/${cuenta}`
);
console.log('Datos del alumno:', data);
@@ -57,6 +58,7 @@ export default function Formulario({
apellidos: data.apellidos.toString(),
correo: data.id_ncuenta + '@pcpuma.acatlan.unam.mx',
genero: data.genero,
carrera: data.carrera,
});
}
} catch (err) {
@@ -99,6 +101,10 @@ export default function Formulario({
) {
nuevasRespuestas[`pregunta_${id}`] = 'FES Acatlán';
}
if (pregunta.pregunta.toLowerCase().includes('carrera')) {
nuevasRespuestas[`pregunta_${id}`] = datosAuto.carrera;
}
});
});
@@ -188,8 +194,6 @@ export default function Formulario({
const validarRespuestasObligatorias = (): boolean => {
const faltantes: number[] = [];
const errores: { id: number; mensaje: string }[] = [];
console.log(secciones);
console.log('Validando respuestas:', respuestas);
secciones.forEach((seccion) => {
seccion.preguntas.forEach(({ pregunta }) => {
@@ -198,7 +202,7 @@ export default function Formulario({
const tipo = pregunta.tipo_pregunta.tipo_pregunta;
const validacion = pregunta.validacion;
console.log('Validando pregunta:', pregunta.pregunta);
console.log('Validando pregunta:', pregunta.pregunta, ' ---------- ');
console.table({
id,
valor,
@@ -320,6 +324,10 @@ export default function Formulario({
defaultValue = 'FES Acatlán';
disabled = true;
}
if (p.pregunta.toLowerCase().includes('carrera')) {
defaultValue = datosAuto.carrera;
disabled = true;
}
}
return (
@@ -329,12 +337,13 @@ export default function Formulario({
name={`pregunta_${p.id_pregunta}`}
defaultValue={defaultValue}
disabled={disabled}
onChange={(e) =>
onChange={(e) => {
console.log('Respuesta:', e.target.value);
actualizarRespuesta(
`pregunta_${p.id_pregunta}`,
e.target.value
)
}
);
}}
/>
{p.validacion === 'correo' && (
<div className="alert alert-info">