diff --git a/app/(private)/Periodo/page.tsx b/app/(private)/Periodo/page.tsx index 887a3a1..901266f 100644 --- a/app/(private)/Periodo/page.tsx +++ b/app/(private)/Periodo/page.tsx @@ -1,35 +1,143 @@ "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; +import axios from "axios"; + +interface Periodo { + id_periodo: number; + semestre: string; + fecha_inicio_servicio: string; + fecha_fin_servicio: string; + activo: boolean; +} export default function PeriodoPage() { - const [periodos] = useState([ - { id: 1, nombre: "2024-2", activo: true }, - { id: 2, nombre: "2025-1", activo: false }, - { id: 3, nombre: "2025-2", activo: false }, - { id: 4, nombre: "2026-1", activo: true }, - { id: 5, nombre: "2026-2", activo: false }, - { id: 6, nombre: "2027-1", activo: false }, - ]); + + const [periodos, setPeriodos] = useState([]); + const [semestre, setSemestre] = useState(""); + const [fechaInicio, setFechaInicio] = useState(""); + const [fechaFin, setFechaFin] = useState(""); + + const API = "http://localhost:3000/periodo"; // NO CAMBIO RUTA + + + const config = { + headers: { + Authorization: `Bearer ${localStorage.getItem("token")}`, + }, + }; + + + const obtenerPeriodos = async () => { + try { + const res = await axios.get(API, config); + setPeriodos(res.data); + } catch (error) { + console.error("Error al obtener periodos", error); + } + }; + + useEffect(() => { + obtenerPeriodos(); + }, []); + + // CREAR PERIODO + + const crearPeriodo = async () => { + try { + await axios.post( + API, + { + semestre, + fecha_inicio_servicio: fechaInicio, + fecha_fin_servicio: fechaFin, + activo: true, + }, + config + ); + + setSemestre(""); + setFechaInicio(""); + setFechaFin(""); + + 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); + obtenerPeriodos(); + } catch (error) { + console.error("Error al eliminar periodo", error); + } + }; return (
-

PERIODO ESCOLAR

+

PERIODO ESCOLAR

- + {/* ================= CREAR ================= */} +
+ setSemestre(e.target.value)} + /> + + setFechaInicio(e.target.value)} + /> + + setFechaFin(e.target.value)} + /> + + +
+ + {/* ================= LISTADO ================= */} +
- + + + + {periodos.map((p) => ( - - - + + + + + + ))} diff --git a/app/globals.css b/app/globals.css index 3efcefd..4569bcb 100644 --- a/app/globals.css +++ b/app/globals.css @@ -255,10 +255,13 @@ button:disabled { background-color: #2563eb; } + + .buttonSearch:hover { background-color: #1d4ed8; } + .buttonCharge { background-color: #16a34a; }
IDPeriodoSemestreInicioFin ActivoAcción
{p.id}{p.nombre}
{p.id_periodo}{p.semestre}{p.fecha_inicio_servicio}{p.fecha_fin_servicio} {p.activo ? "Sí" : "No"} + +