Files
front-AT/app/(private)/Programas/page.tsx
T

236 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-09-10 13:09:26 -06:00
"use client";
2026-01-29 12:19:38 -06:00
import { useState, useEffect } from "react";
2026-01-29 12:41:15 -06:00
import { envConfig } from "@/app/lib/config";
import styleprograms from "./programas.module.css";
2026-02-23 11:26:00 -06:00
import axios from "axios";
import Swal from "sweetalert2";
import Cookies from "js-cookie";
2026-01-29 12:19:38 -06:00
2026-01-29 12:41:15 -06:00
interface Programa {
2026-01-29 12:19:38 -06:00
id_programa: number;
programa: string;
}
2025-09-02 19:50:17 -04:00
export default function Page() {
2025-09-23 14:13:23 -06:00
const [modo, setModo] = useState("ver");
2026-01-29 12:19:38 -06:00
const [programaSeleccionado, setProgramaSeleccionado] = useState("");
const [programas, setProgramas] = useState<Programa[]>([]);
2026-01-29 12:59:50 -06:00
const [nombrePrograma, setNombrePrograma] = useState("");
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-02-23 11:26:00 -06:00
const cargarProgramas = async () => {
try {
2026-03-02 10:34:42 -06:00
const response = await axios.get(`${envConfig.apiUrl}/programa/`, { headers }
);
2026-02-23 11:26:00 -06:00
setProgramas(response.data);
} catch (error: any) {
console.error("Error al cargar programas:", error);
Swal.fire({
title: "Error al cargar programas",
text: error.response?.data?.message || error.message,
icon: "error",
});
}
};
2026-01-29 12:19:38 -06:00
useEffect(() => {
2026-02-23 11:26:00 -06:00
cargarProgramas();
2026-01-29 12:19:38 -06:00
}, []);
2026-01-29 13:04:59 -06:00
2026-02-03 12:45:31 -05:00
const handleInsertar = async () => {
2026-02-23 11:26:00 -06:00
if (!nombrePrograma) return;
2026-02-03 12:45:31 -05:00
2026-02-23 11:26:00 -06:00
try {
await axios.post(`${envConfig.apiUrl}/programa`, {
programa: nombrePrograma,
2026-03-02 10:34:42 -06:00
}, { headers }
);
2026-02-23 11:26:00 -06:00
await cargarProgramas();
setNombrePrograma("");
setModo("ver");
Swal.fire({
title: "Programa creado correctamente",
icon: "success",
});
} catch (error: any) {
Swal.fire({
title: "Error al insertar",
text: error.response?.data?.message || error.message,
icon: "error",
});
}
};
2026-02-03 12:45:31 -05:00
2026-01-29 12:59:50 -06:00
const handleGuardar = async () => {
2026-01-29 13:16:16 -06:00
if (!programaSeleccionado) return;
2026-02-23 11:26:00 -06:00
try {
2026-02-23 11:26:00 -06:00
await axios.patch(
`${envConfig.apiUrl}/programa/${programaSeleccionado}`,
{
programa: nombrePrograma,
},
{ headers },
2026-02-23 11:26:00 -06:00
);
await cargarProgramas();
setModo("ver");
Swal.fire({
title: "Programa actualizado correctamente",
icon: "success",
});
} catch (error: any) {
Swal.fire({
title: "Error al actualizar",
text: error.response?.data?.message || error.message,
icon: "error",
});
}
2026-01-29 13:16:16 -06:00
};
2025-09-23 14:13:23 -06:00
2026-01-29 12:19:38 -06:00
const handleNuevo = () => setModo("nuevo");
const handleCancelar = () => setModo("ver");
2026-02-23 11:26:00 -06:00
2026-01-29 12:59:50 -06:00
const handleEditar = () => {
2026-01-29 13:16:16 -06:00
const programa = programas.find(
2026-02-23 11:26:00 -06:00
(p) => p.id_programa === Number(programaSeleccionado)
2026-01-29 13:16:16 -06:00
);
2026-01-29 12:59:50 -06:00
2026-01-29 13:16:16 -06:00
if (programa) {
setNombrePrograma(programa.programa);
setModo("editar");
}
};
2025-09-10 13:09:26 -06:00
2026-02-23 11:26:00 -06:00
const handleDelete = async () => {
if (!programaSeleccionado) return;
try {
await axios.delete(
`${envConfig.apiUrl}/programa/${programaSeleccionado}`,
{ headers },
2026-02-23 11:26:00 -06:00
);
await cargarProgramas();
setProgramaSeleccionado("");
Swal.fire({
title: "Programa eliminado exitosamente",
icon: "success",
});
} catch (error: any) {
Swal.fire({
title: "Error al eliminar",
text: error.response?.data?.message || error.message,
icon: "error",
});
}
};
2025-09-02 19:50:17 -04:00
return (
2025-10-01 10:12:41 -06:00
<section className="containerSection">
2025-09-23 14:13:23 -06:00
{modo === "ver" && (
2025-09-10 13:09:26 -06:00
<section className="containersection">
2025-09-23 14:13:23 -06:00
<h3 className="title">PROGRAMAS</h3>
2026-02-23 11:26:00 -06:00
<label className="label">
Selecciona el programa que quieres editar
</label>
2026-01-29 12:19:38 -06:00
<select
value={programaSeleccionado}
onChange={(e) => setProgramaSeleccionado(e.target.value)}
>
<option value="">Selecciona un programa</option>
{programas.map((programa) => (
<option key={programa.id_programa} value={programa.id_programa}>
{programa.programa}
</option>
))}
2025-09-23 14:13:23 -06:00
</select>
2026-01-29 12:19:38 -06:00
2025-09-10 13:09:26 -06:00
<div className="margin">
2026-01-29 12:41:15 -06:00
<button className="button buttonSearch" onClick={handleEditar}>
2025-09-10 13:09:26 -06:00
Editar
</button>
2026-01-29 12:19:38 -06:00
2025-09-22 16:59:47 -06:00
<button
2026-02-23 11:26:00 -06:00
className="button buttonCharge"
style={{ margin: "5px" }}
2025-09-22 16:59:47 -06:00
onClick={handleNuevo}
>
2025-09-10 13:09:26 -06:00
Nuevo
</button>
</div>
</section>
2025-09-23 14:13:23 -06:00
)}
{modo === "editar" && (
2025-09-10 13:09:26 -06:00
<section className="containersection">
2026-01-29 12:19:38 -06:00
<h2 className="title">Editar Programa</h2>
2026-01-29 12:59:50 -06:00
<input
2026-01-29 13:16:16 -06:00
type="text"
value={nombrePrograma}
onChange={(e) => setNombrePrograma(e.target.value)}
placeholder="Nombre del programa"
/>
2026-01-29 12:19:38 -06:00
2025-09-10 13:09:26 -06:00
<div className="margin">
2026-02-23 11:26:00 -06:00
<button className="button buttonSearch" onClick={handleGuardar}>
Guardar
</button>
2026-01-29 12:19:38 -06:00
2025-09-23 14:13:23 -06:00
<button
2025-09-24 18:34:13 -06:00
className={`button buttonCancel ${styleprograms.button}`}
2025-09-23 14:13:23 -06:00
onClick={handleCancelar}
>
2025-09-10 13:09:26 -06:00
Cancelar
</button>
2026-02-24 18:29:42 -06:00
<button
className={`button buttonCancel ${styleprograms.button}`}
onClick={handleDelete}
>
Eliminar
</button>
2025-09-10 13:09:26 -06:00
</div>
</section>
)}
2025-09-22 16:59:47 -06:00
2025-09-23 14:13:23 -06:00
{modo === "nuevo" && (
2025-09-22 16:59:47 -06:00
<section className="containersection">
2025-09-23 14:13:23 -06:00
<h2 className="title">Nuevo Programa</h2>
2026-01-29 12:19:38 -06:00
2026-02-03 12:45:31 -05:00
<input
2026-02-23 11:26:00 -06:00
type="text"
value={nombrePrograma}
onChange={(e) => setNombrePrograma(e.target.value)}
placeholder="Coloca el programa"
/>
2026-01-29 12:19:38 -06:00
2025-09-22 16:59:47 -06:00
<div className="margin">
2026-02-03 12:45:31 -05:00
<button className="button buttonSearch" onClick={handleInsertar}>
2026-02-23 11:26:00 -06:00
Insertar
</button>
2026-01-29 12:19:38 -06:00
2025-09-23 14:13:23 -06:00
<button
2025-09-24 18:34:13 -06:00
className={`button buttonCancel ${styleprograms.button}`}
2025-09-23 14:13:23 -06:00
onClick={handleCancelar}
>
2025-09-22 16:59:47 -06:00
Cancelar
</button>
</div>
</section>
)}
2025-10-01 10:12:41 -06:00
</section>
2025-09-02 19:50:17 -04:00
);
}
2026-02-23 11:26:00 -06:00
//IO