From 552353f4d708f8d10f441465af0a6790b41a0f4d Mon Sep 17 00:00:00 2001 From: frcarlos <307100636@pcpuma.acatlan.unam.mx> Date: Fri, 27 Feb 2026 16:30:52 -0500 Subject: [PATCH] PeriodoPage --- app/(private)/Periodo/page.tsx | 208 +++++++++++++++++++++++---------- 1 file changed, 145 insertions(+), 63 deletions(-) diff --git a/app/(private)/Periodo/page.tsx b/app/(private)/Periodo/page.tsx index 901266f..1605a02 100644 --- a/app/(private)/Periodo/page.tsx +++ b/app/(private)/Periodo/page.tsx @@ -2,6 +2,7 @@ import { useState, useEffect } from "react"; import axios from "axios"; +import Cookies from "js-cookie"; interface Periodo { id_periodo: number; @@ -17,20 +18,17 @@ export default function PeriodoPage() { const [semestre, setSemestre] = useState(""); const [fechaInicio, setFechaInicio] = useState(""); const [fechaFin, setFechaFin] = useState(""); + const [modoEdicion, setModoEdicion] = useState(false); + const [idEditar, setIdEditar] = useState(null); - const API = "http://localhost:3000/periodo"; // NO CAMBIO RUTA + const API = "http://localhost:5000/periodo"; - - const config = { - headers: { - Authorization: `Bearer ${localStorage.getItem("token")}`, - }, - }; + const token = Cookies.get("token"); + const headers = { Authorization: `Bearer ${token}` }; - const obtenerPeriodos = async () => { try { - const res = await axios.get(API, config); + const res = await axios.get(API, { headers }); setPeriodos(res.data); } catch (error) { console.error("Error al obtener periodos", error); @@ -41,9 +39,22 @@ export default function PeriodoPage() { obtenerPeriodos(); }, []); - // CREAR PERIODO - + // VALIDAR SEMESTRE + const handleSemestreChange = (e: React.ChangeEvent) => { + const valor = e.target.value; + + + if (/^\d{0,6}$/.test(valor)) { + setSemestre(valor); + } + }; + const crearPeriodo = async () => { + if (semestre.length !== 6) { + alert("El semestre debe tener exactamente 6 dígitos."); + return; + } + try { await axios.post( API, @@ -53,95 +64,166 @@ export default function PeriodoPage() { fecha_fin_servicio: fechaFin, activo: true, }, - config + { headers } ); - setSemestre(""); - setFechaInicio(""); - setFechaFin(""); - + limpiarFormulario(); obtenerPeriodos(); } catch (error) { console.error("Error al crear periodo", error); } }; - // ELIMINAR PERIODO - + const eliminarPeriodo = async (id_periodo: number) => { try { - await axios.delete(`${API}/${id_periodo}`, config); + await axios.delete(`${API}/${id_periodo}`, { headers }); obtenerPeriodos(); } catch (error) { console.error("Error al eliminar periodo", error); } }; - return ( -
-

PERIODO ESCOLAR

+ const cargarPeriodo = (p: Periodo) => { + setModoEdicion(true); + setIdEditar(p.id_periodo); + setSemestre(p.semestre); + setFechaInicio(p.fecha_inicio_servicio); + setFechaFin(p.fecha_fin_servicio); + }; - {/* ================= CREAR ================= */} -
+ + const modificarPeriodo = async () => { + if (semestre.length !== 6) { + alert("El semestre debe tener exactamente 6 dígitos."); + return; + } + + try { + await axios.put( + `${API}/${idEditar}`, + { + semestre, + fecha_inicio_servicio: fechaInicio, + fecha_fin_servicio: fechaFin, + }, + { headers } + ); + + limpiarFormulario(); + obtenerPeriodos(); + } catch (error) { + console.error("Error al modificar periodo", error); + } + }; + + const limpiarFormulario = () => { + setSemestre(""); + setFechaInicio(""); + setFechaFin(""); + setModoEdicion(false); + setIdEditar(null); + }; + + return ( +
+

PERIODO ESCOLAR

+ + {/* ================= FORMULARIO ================= */} +
setSemestre(e.target.value)} + maxLength={6} + onChange={handleSemestreChange} /> + setFechaInicio(e.target.value)} /> + setFechaFin(e.target.value)} /> - + {modoEdicion ? ( + <> + + + + + ) : ( + + )}
- {/* ================= LISTADO ================= */} - - - - - - - - - - - - - {periodos.map((p) => ( - - - - - - - + {/* ================= TABLA ================= */} +
+
IDSemestreInicioFinActivoAcción
{p.id_periodo}{p.semestre}{p.fecha_inicio_servicio}{p.fecha_fin_servicio}{p.activo ? "Sí" : "No"} - -
+ + + + + + + + - ))} - -
IDPERIODOInicioFinActivoAcción
+ + + {periodos.map((p) => ( + + {p.id_periodo} + {p.semestre} + {p.fecha_inicio_servicio} + {p.fecha_fin_servicio} + {p.activo === true ? "Sí" : "No"} + + + + + + + ))} + + +
); } \ No newline at end of file