Files
front-AT/app/Components/BitacoraSanciones/TableSancion.tsx
T

130 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-09-29 22:48:06 -06:00
"use client";
import { useEffect, useState } from "react";
import axios from "axios";
2025-10-14 14:27:03 -06:00
import toast from "react-hot-toast";
import { envConfig } from "@/app/lib/config";
2025-09-29 15:37:56 -06:00
2025-10-14 14:27:03 -06:00
interface Alumno {
2025-09-29 15:37:56 -06:00
id_cuenta: number;
nombre: string;
credito: number;
}
2025-10-14 14:27:03 -06:00
interface Sancion {
2025-09-29 15:37:56 -06:00
id_sancion: number;
sancion: string;
duracion: number;
}
2025-10-14 14:27:03 -06:00
interface AlumnoSancion {
id_alumno_sancion: number;
fecha_inicio: string;
sancion: Sancion;
}
2025-10-07 15:17:07 -06:00
if (!envConfig.apiUrl) {
console.error("API URL is not defined in envConfig");
}
2025-10-14 14:27:03 -06:00
export default function TableSancion({ idCuenta }: { idCuenta: number }) {
const [sanciones, setSanciones] = useState<Sancion[]>([]);
const [alumno, setAlumno] = useState<Alumno | null>(null);
const [alumnoSanciones, setAlumnoSanciones] = useState<AlumnoSancion[]>([]);
const [selectedSancion, setSelectedSancion] = useState("");
2025-10-07 15:17:07 -06:00
useEffect(() => {
2025-10-14 14:27:03 -06:00
if (!idCuenta) return;
// Obtener sanciones del alumno
2025-10-07 15:17:07 -06:00
axios
.get(
`${envConfig.apiUrl}/asignacionTiempo_test/alumno-sancion/${idCuenta}`
2025-10-07 15:17:07 -06:00
)
2025-10-14 14:27:03 -06:00
.then((res) => {
setAlumno(res.data.student);
setAlumnoSanciones(
res.data.alusancion?.length ? res.data.alusancion : []
);
2025-10-07 15:17:07 -06:00
})
2025-10-14 14:27:03 -06:00
.catch((err) =>
toast.error("Error al obtener las sanciones del alumno:", err)
);
2025-10-07 15:17:07 -06:00
2025-10-14 14:27:03 -06:00
// Obtener catálogo de sanciones
2025-10-07 15:17:07 -06:00
axios
.get(`${envConfig.apiUrl}/asignacionTiempo_test/sancion`)
2025-10-14 14:27:03 -06:00
.then((res) => setSanciones(res.data))
.catch((err) =>
toast.error("Error al obtener el catálogo de sanciones:", err)
);
}, [idCuenta]);
2025-10-07 15:17:07 -06:00
const calcularFechaFin = (fechaInicio: string, duracionSemanas: number) => {
const fecha = new Date(fechaInicio);
fecha.setDate(fecha.getDate() + duracionSemanas * 7);
return fecha.toLocaleDateString();
};
2025-09-29 15:37:56 -06:00
return (
<>
2025-10-14 14:27:03 -06:00
<div style={{ margin: "1rem 0" }}>
<table>
2025-09-29 15:37:56 -06:00
<thead>
<tr>
<th>Cuenta</th>
2025-10-01 10:12:41 -06:00
<th>Motivo de la sanción</th>
2025-10-14 14:27:03 -06:00
<th>Duración (Semanas)</th>
2025-10-01 10:12:41 -06:00
<th>Fecha Sanción</th>
2025-10-14 14:27:03 -06:00
<th>Podrá utilizar el servicio hasta</th>
2025-09-29 15:37:56 -06:00
</tr>
</thead>
2025-10-07 15:17:07 -06:00
<tbody>
2025-10-14 14:27:03 -06:00
{alumnoSanciones.length > 0 ? (
alumnoSanciones.map((item) => (
2025-10-07 15:17:07 -06:00
<tr key={item.id_alumno_sancion}>
2025-10-14 14:27:03 -06:00
<td>{alumno?.id_cuenta}</td>
<td>{item.sancion.sancion}</td>
<td>{item.sancion.duracion}</td>
2025-10-07 15:17:07 -06:00
<td>{new Date(item.fecha_inicio).toLocaleDateString()}</td>
<td>
2025-10-14 14:27:03 -06:00
{calcularFechaFin(item.fecha_inicio, item.sancion.duracion)}
2025-10-07 15:17:07 -06:00
</td>
</tr>
))
) : (
<tr>
2025-10-14 14:27:03 -06:00
<td colSpan={5}>No hay sanciones registradas</td>
2025-10-07 15:17:07 -06:00
</tr>
)}
</tbody>
2025-09-29 15:37:56 -06:00
</table>
</div>
<form className="containerForm">
<div className="groupInput">
2025-10-07 15:17:07 -06:00
<select
value={selectedSancion}
onChange={(e) => setSelectedSancion(e.target.value)}
>
2025-10-01 10:12:41 -06:00
<option value="">-- Selecciona una sanción --</option>
2025-10-14 14:27:03 -06:00
{sanciones.map((sancion) => (
2025-10-07 15:17:07 -06:00
<option key={sancion.id_sancion} value={sancion.id_sancion}>
{sancion.sancion} ({sancion.duracion} semana/s)
</option>
))}
2025-09-29 15:37:56 -06:00
</select>
</div>
</form>
2025-10-14 14:27:03 -06:00
2025-10-07 15:17:07 -06:00
<button
className="button buttonSearch"
style={{ margin: "1rem 0" }}
2025-10-14 14:27:03 -06:00
onClick={() => alert(`Sanción ${selectedSancion} aplicada`)}
2025-10-07 15:17:07 -06:00
>
2025-10-01 10:12:41 -06:00
Aplicar sanción
</button>
2025-09-29 15:37:56 -06:00
</>
);
}