"use client"; import { useEffect, useState } from "react"; import { AppShell } from "@/components/AppShell"; import { useAuth, useCan } from "@/lib/abilities"; import { ROLE_LABEL, ROLES_DESC } from "@/lib/labels"; import { createUser, deleteUser, listUsers, resetUserPassword, updateUser, } from "@/lib/api"; import type { Role, UserRow } from "@/lib/types"; export default function UsuariosPage() { return ( ); } type FormState = { name: string; email: string; password: string; role: Role; active: boolean; }; const EMPTY_FORM: FormState = { name: "", email: "", password: "", role: "STAFF", active: true, }; function UsuariosAdmin() { const me = useAuth(); const allowed = useCan("user:manage"); const [users, setUsers] = useState(null); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); // null = create mode; a user id = editing that row. const [editingId, setEditingId] = useState(null); const [form, setForm] = useState(EMPTY_FORM); const [saving, setSaving] = useState(false); // Inline "reset password" target + value. const [pwTarget, setPwTarget] = useState(null); const [pwValue, setPwValue] = useState(""); function refresh() { listUsers() .then(setUsers) .catch((e) => setError(e?.message ?? "No se pudieron cargar los usuarios.")); } useEffect(() => { if (allowed) refresh(); }, [allowed]); if (!allowed) { return (

Usuarios

No tiene permisos para administrar usuarios.
); } function startCreate() { setEditingId(null); setForm(EMPTY_FORM); setNotice(null); setError(null); } function startEdit(u: UserRow) { setEditingId(u.id); setForm({ name: u.name, email: u.email, password: "", role: u.role, active: u.active }); setNotice(null); setError(null); } async function submit(e: React.FormEvent) { e.preventDefault(); setSaving(true); setError(null); setNotice(null); try { if (editingId) { await updateUser(editingId, { name: form.name, email: form.email, role: form.role, active: form.active, }); setNotice("Usuario actualizado."); } else { await createUser({ name: form.name, email: form.email, password: form.password, role: form.role, active: form.active, }); setNotice("Usuario creado."); } startCreate(); refresh(); } catch (e2) { setError((e2 as Error)?.message ?? "No se pudo guardar el usuario."); } finally { setSaving(false); } } async function submitPassword(id: string) { setError(null); try { await resetUserPassword(id, pwValue); setPwTarget(null); setPwValue(""); setNotice("Contraseña restablecida."); } catch (e) { setError((e as Error)?.message ?? "No se pudo restablecer la contraseña."); } } async function submitDelete(u: UserRow) { if (!window.confirm(`¿Eliminar al usuario "${u.name}"? Esta acción no se puede deshacer.`)) { return; } setError(null); setNotice(null); try { await deleteUser(u.id); if (editingId === u.id) startCreate(); setNotice("Usuario eliminado."); refresh(); } catch (e) { setError((e as Error)?.message ?? "No se pudo eliminar el usuario."); } } return ( <>

Usuarios

{error &&
{error}
} {notice &&
{notice}
} {/* Create / edit form */}

{editingId ? "Editar usuario" : "Nuevo usuario"}

El rol define el acceso: Solo lectura no puede escribir; Personal y superior sí. Administrador gestiona usuarios.

{!editingId && ( )}
{editingId && ( )}
{/* List */}
{users === null ? (
) : users.length === 0 ? (
Sin usuarios.
) : (
{users.map((u) => ( ))}
Nombre Correo Rol Estado Acciones
{u.name} {u.id === me?.id && ( (usted) )} {u.email} {ROLE_LABEL[u.role]} {u.active ? "Activo" : "Inactivo"} {pwTarget === u.id ? (
setPwValue(e.target.value)} style={{ maxWidth: 180 }} />
) : (
)}
)}
); }