Se agregaron nuevas funcionalidades como en el cambio de contraseña

This commit is contained in:
santiago
2025-10-30 22:09:38 +01:00
parent 872f592816
commit 7ea17feb2c
5 changed files with 150 additions and 45 deletions
@@ -0,0 +1,95 @@
'use client'
import { axiosInstance } from "@/api/config";
import EditarResponsable from "@/components/administrador/editar-responsable";
import ReasignacionProgramas from "@/components/administrador/reasignacion-programas";
import BotonRegresar from "@/components/boton-regresar";
import { useEffect, useState } from "react";
interface ResponsableResponse {
idUsuario: number;
nombre: string;
usuario: string; // correo
}
export default function Editar() {
const token1 = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
const [data, setData] = useState<ResponsableResponse>({
idUsuario: 0,
nombre: "",
usuario: "",
});
const imprimirError = (message: unknown) => {
// eslint-disable-next-line no-alert
alert(`¡Hubo un error!\n\n${message}`);
};
const imprimirMensaje = (message: string) => {
// eslint-disable-next-line no-alert
alert(`¡Felicidades!\n\n${message}`);
};
const imprimirWarning = (message: string, onConfirm: () => void = () => {}, title = '¡Espera un minuto!', onCancel: () => void = () => {}) => {
// eslint-disable-next-line no-alert
const ok = confirm(`${title}\n\n${message}`);
if (ok) onConfirm(); else onCancel();
};
const updateIsLoading = (booleanValue: boolean) => {
// Aquí podrías implementar alguna lógica para mostrar un indicador de carga
console.log('isLoading:', booleanValue);
}
// Peticion para obtener la info del usuario
const editarResponsable = async () => {
const idUsuario = localStorage.getItem("idResponsable")
console.log("Id responsable: ", idUsuario)
try {
updateIsLoading(true)
const res = await axiosInstance.get(`usuario/responsable?idUsuario=${idUsuario}`)
//imprimirMensaje(res.data.message)
setData(res.data)
} catch (error) {
const msg: unknown = error;
imprimirError(msg);
}
}
useEffect(() => {
editarResponsable();
}, []);
return (
<section>
<BotonRegresar />
<EditarResponsable
responsable={data}
imprimirError={imprimirError}
imprimirMensaje={imprimirMensaje}
imprimirWarning={imprimirWarning}
updateIsLoading={updateIsLoading}
/>
<ReasignacionProgramas
responsable={data}
imprimirError={imprimirError}
imprimirMensaje={imprimirMensaje}
imprimirWarning={imprimirWarning}
updateIsLoading={updateIsLoading}
/>
{/*
<EditarResponsable
token={token ?? undefined}
imprimirError={imprimirError}
imprimirMensaje={imprimirMensaje}
imprimirWarning={imprimirWarning}
updateIsLoading={updateIsLoading}
/>
*/}
</section>
);
}
@@ -9,8 +9,10 @@ import { FaUpload } from "react-icons/fa";
export default function CargaMasiva({datos}: {datos: CargaMasivaProps}) {
const router = useRouter();
const [csv, setCsv] = useState<File | null>(null)
//const link = `${process.env.api}/plantilla.csv`,
//const [link] = useState(`${process.env.NEXT_PUBLIC_API}/plantilla.csv`)
const url = process.env.NEXT_PUBLIC_API_URL
const link = `${url}/plantilla.csv`
//const [link] = useState(`${url}/plantilla.csv`)
const validarExt = (file: File | null) => {
if (!file) return
@@ -89,7 +91,7 @@ export default function CargaMasiva({datos}: {datos: CargaMasivaProps}) {
<Button
variant="link"
//href={link}
href={link}
target="_blank"
rel="noopener noreferrer">
Descargar plantilla
@@ -7,17 +7,18 @@ import validator from "validator";
import { useRouter } from "next/navigation";
interface Responsable {
idUsuario: number;
nombre: string;
usuario: string; // correo
idUsuario?: number;
nombre?: string;
usuario?: string; // correo
}
interface Admin {
token: { headers: Record<string, string> };
//token: { headers: Record<string, string> };
token?: string;
}
interface Props {
admin: Admin;
//admin: Admin;
responsable: Responsable;
imprimirError: (err: unknown) => void;
imprimirMensaje: (msg: string) => void;
@@ -26,7 +27,7 @@ interface Props {
}
export default function EditarResponsable({
admin,
//admin,
responsable,
imprimirError,
imprimirMensaje,
@@ -36,6 +37,11 @@ export default function EditarResponsable({
const [nuevo, setNuevo] = useState<{ nombre?: string; correo?: string }>({});
const router = useRouter();
console.log("Informacion que se la pasa:", responsable);
console.log("Entrando al campo nombre: ", responsable.nombre)
console.log("Entrando al campo usuario:", responsable.usuario)
// Validar si el botón debe estar deshabilitado
const mostrarBoton = (): boolean => {
if (nuevo.correo) {
@@ -53,20 +59,20 @@ export default function EditarResponsable({
if (nuevo.nombre) data.nombre = nuevo.nombre;
try {
updateIsLoading(true);
const res = await axiosInstance.put(`/usuario/responsable/update`, data, admin.token);
imprimirMensaje(res.data.message);
router.push("/admin/responsables/responsable");
updateIsLoading(true);
const res = await axiosInstance.put(`/usuario/responsable/update`, data);
imprimirMensaje(res.data.message);
router.push("/administrador/responsables/responsable");
} catch (err: unknown) {
let msg: unknown = err;
if (isAxiosError(err) && err.response?.data) {
msg = err.response.data;
} else if (err instanceof Error) {
msg = err.message;
}
imprimirError(msg);
let msg: unknown = err;
if (isAxiosError(err) && err.response?.data) {
msg = err.response.data;
} else if (err instanceof Error) {
msg = err.message;
}
imprimirError(msg);
} finally {
updateIsLoading(false);
updateIsLoading(false);
}
};
@@ -75,22 +81,23 @@ export default function EditarResponsable({
const data = { idUsuario: responsable.idUsuario };
try {
updateIsLoading(true);
const res = await axiosInstance.put(`/usuario/new_password_responsable`, data, admin.token);
imprimirMensaje(res.data.message);
router.push("/admin/responsables/responsable");
updateIsLoading(true);
const res = await axiosInstance.put(`/usuario/new_password_responsable`, data);
imprimirMensaje(res.data.message);
router.push("/administrador/responsables/responsable");
} catch (err: unknown) {
let msg: unknown = err;
if (isAxiosError(err) && err.response?.data) {
msg = err.response.data;
} else if (err instanceof Error) {
msg = err.message;
}
imprimirError(msg);
let msg: unknown = err;
if (isAxiosError(err) && err.response?.data) {
msg = err.response.data;
} else if (err instanceof Error) {
msg = err.message;
}
imprimirError(msg);
} finally {
updateIsLoading(false);
}
};
return (
<div className="space-y-4 mt-6">
@@ -156,7 +156,7 @@ export default function InformacionResponsable({ admin, imprimirError, updateIsL
<div className="mt-4 text-center">
<button
className="btn btn-primary"
onClick={() => router.push('/admin/responsables/responsable/editar')}
onClick={() => router.push('/administrador/responsables/responsable/editar')}
>
Editar información
</button>
@@ -6,7 +6,8 @@ import { isAxiosError } from 'axios';
import { useRouter } from "next/navigation";
interface Props {
admin: { token: { headers: { token: string } } };
//admin: { token: { headers: { token: string } } };
admin?: string;
responsable: { idUsuario: number; usuario: string };
imprimirError: (err: unknown) => void;
imprimirMensaje: (msg: string) => void;
@@ -42,17 +43,17 @@ export default function ReasignacionProgramas({
};
try {
updateIsLoading(true);
const res = await axiosInstance.put(`/programa/reasignar_programas`, data, admin.token);
updateIsLoading(false);
imprimirMensaje(res.data.message);
router.push("/admin/responsables/responsable");
} catch (err: unknown) {
updateIsLoading(false);
if (isAxiosError(err)) imprimirError(err.response?.data || err.message);
else if (err instanceof Error) imprimirError(err.message);
else imprimirError(err);
}
updateIsLoading(true);
const res = await axiosInstance.put(`/programa/reasignar_programas`, data);
updateIsLoading(false);
imprimirMensaje(res.data.message);
router.push("/admin/responsables/responsable");
} catch (err: unknown) {
updateIsLoading(false);
if (isAxiosError(err)) imprimirError(err.response?.data || err.message);
else if (err instanceof Error) imprimirError(err.message);
else imprimirError(err);
}
};
return (