Private
Public Access
579 lines
14 KiB
Svelte
579 lines
14 KiB
Svelte
<script lang="ts">
|
|
import {
|
|
Button,
|
|
ComboBox,
|
|
DataTableSkeleton,
|
|
InlineNotification,
|
|
Modal,
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableContainer,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
TextInput,
|
|
TextArea
|
|
} from 'carbon-components-svelte';
|
|
import Header from '$lib/components/Header.svelte';
|
|
import Toast from '$lib/components/Toast.svelte';
|
|
import {
|
|
obtenerGrupos,
|
|
crearGrupo,
|
|
actualizarGrupo,
|
|
eliminarGrupo,
|
|
obtenerPermisos,
|
|
asignarPermisoAGrupo,
|
|
quitarPermisoAGrupo,
|
|
agregarSubGrupo,
|
|
quitarSubGrupo
|
|
} from '$lib/api';
|
|
import type { Grupo, Permiso } from '../../types';
|
|
import Add from 'carbon-icons-svelte/lib/Add.svelte';
|
|
import Edit from 'carbon-icons-svelte/lib/Edit.svelte';
|
|
import TrashCan from 'carbon-icons-svelte/lib/TrashCan.svelte';
|
|
import Close from 'carbon-icons-svelte/lib/Close.svelte';
|
|
|
|
let grupos: Grupo[] = $state([]);
|
|
let allPermisos: Permiso[] = $state([]);
|
|
let loading = $state(true);
|
|
let error = $state('');
|
|
|
|
let modalOpen = $state(false);
|
|
let modalEditando = $state(false);
|
|
let formId = $state(0);
|
|
let formNombre = $state('');
|
|
let formDescripcion = $state('');
|
|
|
|
let deleteModalOpen = $state(false);
|
|
let deleteId = $state(0);
|
|
let deleteNombre = $state('');
|
|
|
|
let selectedGrupo: Grupo | null = $state(null);
|
|
|
|
let permisoModalOpen = $state(false);
|
|
let subgrupoModalOpen = $state(false);
|
|
let selectedPermisoItem = $state<{ id: number; text: string } | null>(null);
|
|
let selectedSubgrupoItem = $state<{ id: number; text: string } | null>(null);
|
|
|
|
let successMsg = $state('');
|
|
let errorMsg = $state('');
|
|
let notifOpen = $state(false);
|
|
|
|
const headers = [
|
|
{ key: 'id' as const, value: 'ID' },
|
|
{ key: 'nombre' as const, value: 'Nombre' },
|
|
{ key: 'descripcion' as const, value: 'Descripción' }
|
|
];
|
|
|
|
async function cargarGrupos() {
|
|
loading = true;
|
|
error = '';
|
|
try {
|
|
grupos = await obtenerGrupos();
|
|
} catch (e) {
|
|
error = e instanceof Error ? e.message : 'Error al cargar grupos';
|
|
} finally {
|
|
loading = false;
|
|
}
|
|
}
|
|
|
|
async function cargarPermisos() {
|
|
try {
|
|
allPermisos = await obtenerPermisos();
|
|
} catch {
|
|
// silently fail, permisos list is not critical
|
|
}
|
|
}
|
|
|
|
function abrirModalCrear() {
|
|
modalEditando = false;
|
|
formId = 0;
|
|
formNombre = '';
|
|
formDescripcion = '';
|
|
modalOpen = true;
|
|
}
|
|
|
|
function abrirModalEditar(g: Grupo) {
|
|
modalEditando = true;
|
|
formId = g.id;
|
|
formNombre = g.nombre;
|
|
formDescripcion = g.descripcion ?? '';
|
|
modalOpen = true;
|
|
}
|
|
|
|
function abrirModalEliminar(g: Grupo) {
|
|
deleteId = g.id;
|
|
deleteNombre = g.nombre;
|
|
deleteModalOpen = true;
|
|
}
|
|
|
|
function seleccionarGrupo(g: Grupo) {
|
|
selectedGrupo = selectedGrupo?.id === g.id ? null : g;
|
|
}
|
|
|
|
function mostrarNotificacion(titulo: string, msg: string) {
|
|
successMsg = titulo;
|
|
errorMsg = msg;
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
|
|
function handleSecondary() {
|
|
modalOpen = false;
|
|
}
|
|
|
|
function handleDeleteSecondary() {
|
|
deleteModalOpen = false;
|
|
}
|
|
|
|
async function guardarGrupo() {
|
|
if (!formNombre) {
|
|
errorMsg = 'Nombre es obligatorio';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
return;
|
|
}
|
|
try {
|
|
if (modalEditando) {
|
|
await actualizarGrupo(formId, {
|
|
nombre: formNombre,
|
|
descripcion: formDescripcion
|
|
});
|
|
mostrarNotificacion('Grupo actualizado', '');
|
|
} else {
|
|
await crearGrupo({
|
|
nombre: formNombre,
|
|
descripcion: formDescripcion
|
|
});
|
|
mostrarNotificacion('Grupo creado', '');
|
|
}
|
|
modalOpen = false;
|
|
selectedGrupo = null;
|
|
await cargarGrupos();
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al guardar grupo';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
async function confirmarEliminar() {
|
|
try {
|
|
await eliminarGrupo(deleteId);
|
|
deleteModalOpen = false;
|
|
selectedGrupo = null;
|
|
mostrarNotificacion('Grupo eliminado', '');
|
|
await cargarGrupos();
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al eliminar grupo';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
function abrirModalAgregarPermiso() {
|
|
selectedPermisoItem = null;
|
|
permisoModalOpen = true;
|
|
}
|
|
|
|
function abrirModalAgregarSubgrupo() {
|
|
selectedSubgrupoItem = null;
|
|
subgrupoModalOpen = true;
|
|
}
|
|
|
|
async function agregarPermiso() {
|
|
if (!selectedGrupo || !selectedPermisoItem) return;
|
|
try {
|
|
await asignarPermisoAGrupo(selectedGrupo.id, selectedPermisoItem.id);
|
|
mostrarNotificacion('Permiso asignado', '');
|
|
permisoModalOpen = false;
|
|
await cargarGrupos();
|
|
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
|
if (updated) selectedGrupo = updated;
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al asignar permiso';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
async function quitarPermiso(permisoId: number) {
|
|
if (!selectedGrupo) return;
|
|
try {
|
|
await quitarPermisoAGrupo(selectedGrupo.id, permisoId);
|
|
mostrarNotificacion('Permiso quitado', '');
|
|
await cargarGrupos();
|
|
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
|
if (updated) selectedGrupo = updated;
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al quitar permiso';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
async function agregarSubgrupo() {
|
|
if (!selectedGrupo || !selectedSubgrupoItem) return;
|
|
try {
|
|
await agregarSubGrupo(selectedGrupo.id, selectedSubgrupoItem.id);
|
|
mostrarNotificacion('Subgrupo agregado', '');
|
|
subgrupoModalOpen = false;
|
|
await cargarGrupos();
|
|
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
|
if (updated) selectedGrupo = updated;
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al agregar subgrupo';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
async function quitarSubgrupo(childGroupId: number) {
|
|
if (!selectedGrupo) return;
|
|
try {
|
|
await quitarSubGrupo(selectedGrupo.id, childGroupId);
|
|
mostrarNotificacion('Subgrupo quitado', '');
|
|
await cargarGrupos();
|
|
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
|
if (updated) selectedGrupo = updated;
|
|
} catch (e) {
|
|
errorMsg = e instanceof Error ? e.message : 'Error al quitar subgrupo';
|
|
notifOpen = true;
|
|
setTimeout(() => {
|
|
notifOpen = false;
|
|
}, 4000);
|
|
}
|
|
}
|
|
|
|
let permisosDisponibles = $derived(
|
|
allPermisos.filter((p) => !selectedGrupo?.permisos?.some((sp) => sp.id === p.id))
|
|
);
|
|
|
|
let gruposDisponibles = $derived(
|
|
grupos.filter(
|
|
(g) =>
|
|
g.id !== selectedGrupo?.id && !selectedGrupo?.grupos_hijos?.some((sg) => sg.id === g.id)
|
|
)
|
|
);
|
|
|
|
$effect(() => {
|
|
cargarGrupos();
|
|
cargarPermisos();
|
|
});
|
|
</script>
|
|
|
|
<Header />
|
|
|
|
<div class="page-container">
|
|
<div class="flex justify-between">
|
|
<h1 class="page-title">Gestión de Grupos</h1>
|
|
<Button onclick={abrirModalCrear} icon={Add}>Nuevo Grupo</Button>
|
|
</div>
|
|
|
|
{#if error}
|
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
|
{/if}
|
|
|
|
{#if loading}
|
|
<DataTableSkeleton {headers} rows={5} size="compact" showHeader={false} showToolbar={false} />
|
|
{:else}
|
|
<TableContainer title="Grupos" description="Listado de grupos del sistema">
|
|
<Table>
|
|
<TableHead>
|
|
<TableRow>
|
|
{#each headers as header}
|
|
<TableHeader>{header.value}</TableHeader>
|
|
{/each}
|
|
<TableHeader>Acciones</TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{#each grupos as g (g.id)}
|
|
<TableRow
|
|
class="selected-row={selectedGrupo?.id === g.id}"
|
|
onclick={() => seleccionarGrupo(g)}
|
|
onkeydown={(e) => e.key === 'Enter' && seleccionarGrupo(g)}
|
|
tabindex={0}
|
|
style="cursor: pointer"
|
|
>
|
|
{#each headers as header}
|
|
<TableCell>{String(g[header.key] ?? '-')}</TableCell>
|
|
{/each}
|
|
<TableCell class="acciones-cell">
|
|
<Button kind="ghost" size="small" icon={Edit} onclick={() => abrirModalEditar(g)}>
|
|
Editar
|
|
</Button>
|
|
<Button
|
|
kind="ghost"
|
|
size="small"
|
|
icon={TrashCan}
|
|
onclick={() => abrirModalEliminar(g)}
|
|
>
|
|
Eliminar
|
|
</Button>
|
|
</TableCell>
|
|
</TableRow>
|
|
{/each}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
|
|
{#if selectedGrupo}
|
|
<section class="detail-section">
|
|
<div class="detail-header">
|
|
<h2 class="section-title">
|
|
<strong>{selectedGrupo.nombre}</strong>
|
|
</h2>
|
|
{#if selectedGrupo.descripcion}
|
|
<p class="text-secondary">{selectedGrupo.descripcion}</p>
|
|
{/if}
|
|
</div>
|
|
|
|
<div class="detail-grid">
|
|
<div class="detail-col">
|
|
<div class="detail-col-header">
|
|
<h3>Permisos</h3>
|
|
<Button kind="ghost" size="small" icon={Add} onclick={abrirModalAgregarPermiso}>
|
|
Agregar
|
|
</Button>
|
|
</div>
|
|
|
|
{#if selectedGrupo.permisos && selectedGrupo.permisos.length > 0}
|
|
<Table size="compact">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Nombre</TableHeader>
|
|
<TableHeader>Alias</TableHeader>
|
|
<TableHeader></TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{#each selectedGrupo.permisos as permiso}
|
|
<TableRow>
|
|
<TableCell>{permiso.nombre}</TableCell>
|
|
<TableCell>{permiso.alias || '-'}</TableCell>
|
|
<TableCell>
|
|
<Button
|
|
kind="ghost"
|
|
size="small"
|
|
icon={Close}
|
|
onclick={() => quitarPermiso(permiso.id)}
|
|
>
|
|
Quitar
|
|
</Button>
|
|
</TableCell>
|
|
</TableRow>
|
|
{/each}
|
|
</TableBody>
|
|
</Table>
|
|
{:else}
|
|
<p class="text-secondary">Sin permisos asignados.</p>
|
|
{/if}
|
|
</div>
|
|
|
|
<div class="detail-col">
|
|
<div class="detail-col-header">
|
|
<h3>Subgrupos</h3>
|
|
<Button kind="ghost" size="small" icon={Add} onclick={abrirModalAgregarSubgrupo}>
|
|
Agregar
|
|
</Button>
|
|
</div>
|
|
{#if selectedGrupo.grupos_hijos && selectedGrupo.grupos_hijos.length > 0}
|
|
<Table size="compact">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Nombre</TableHeader>
|
|
<TableHeader>Descripción</TableHeader>
|
|
<TableHeader></TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{#each selectedGrupo.grupos_hijos as sg}
|
|
<TableRow>
|
|
<TableCell>{sg.nombre}</TableCell>
|
|
<TableCell>{sg.descripcion || '-'}</TableCell>
|
|
<TableCell>
|
|
<Button
|
|
kind="ghost"
|
|
size="small"
|
|
icon={Close}
|
|
onclick={() => quitarSubgrupo(sg.id)}
|
|
>
|
|
Quitar
|
|
</Button>
|
|
</TableCell>
|
|
</TableRow>
|
|
{/each}
|
|
</TableBody>
|
|
</Table>
|
|
{:else}
|
|
<p class="text-secondary">Sin subgrupos.</p>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
|
|
<form id="grupo-form" onsubmit={guardarGrupo}>
|
|
<Modal
|
|
bind:open={modalOpen}
|
|
modalHeading={modalEditando ? 'Editar Grupo' : 'Nuevo Grupo'}
|
|
primaryButtonText={modalEditando ? 'Guardar' : 'Crear'}
|
|
secondaryButtonText="Cancelar"
|
|
formId="grupo-form"
|
|
on:click:button--secondary={handleSecondary}
|
|
hasForm
|
|
>
|
|
<TextInput bind:value={formNombre} labelText="Nombre" placeholder="ej: Administradores" />
|
|
<TextArea
|
|
bind:value={formDescripcion}
|
|
labelText="Descripción"
|
|
placeholder="Descripción del grupo"
|
|
/>
|
|
</Modal>
|
|
</form>
|
|
|
|
<form id="delete-form" onsubmit={confirmarEliminar}>
|
|
<Modal
|
|
bind:open={deleteModalOpen}
|
|
modalHeading="Eliminar Grupo"
|
|
primaryButtonText="Eliminar"
|
|
secondaryButtonText="Cancelar"
|
|
formId="delete-form"
|
|
hasForm
|
|
on:click:button--secondary={handleDeleteSecondary}
|
|
danger
|
|
>
|
|
<p>¿Estás seguro de que querés eliminar el grupo <strong>{deleteNombre}</strong>?</p>
|
|
</Modal>
|
|
</form>
|
|
|
|
<form id="permiso-form" onsubmit={agregarPermiso}>
|
|
<Modal
|
|
bind:open={permisoModalOpen}
|
|
modalHeading="Agregar Permiso"
|
|
primaryButtonText="Agregar"
|
|
secondaryButtonText="Cancelar"
|
|
formId="permiso-form"
|
|
hasForm
|
|
on:click:button--secondary={() => (permisoModalOpen = false)}
|
|
>
|
|
{#if permisosDisponibles.length === 0}
|
|
<p class="text-secondary">No hay permisos disponibles para agregar.</p>
|
|
{:else}
|
|
<ComboBox
|
|
labelText="Seleccionar permiso"
|
|
items={permisosDisponibles.map((p) => ({ id: p.id, text: `${p.nombre} (${p.alias})` }))}
|
|
itemToString={(item: { id: number; text: string } | null) => (item ? item.text : '')}
|
|
on:select={(e) => (selectedPermisoItem = e.detail.selectedItem)}
|
|
/>
|
|
{/if}
|
|
</Modal>
|
|
</form>
|
|
|
|
<form id="subgrupo-form" onsubmit={agregarSubgrupo}>
|
|
<Modal
|
|
bind:open={subgrupoModalOpen}
|
|
modalHeading="Agregar Subgrupo"
|
|
primaryButtonText="Agregar"
|
|
secondaryButtonText="Cancelar"
|
|
formId="subgrupo-form"
|
|
hasForm
|
|
on:click:button--secondary={() => (subgrupoModalOpen = false)}
|
|
>
|
|
{#if gruposDisponibles.length === 0}
|
|
<p class="text-secondary">No hay grupos disponibles para agregar como subgrupo.</p>
|
|
{:else}
|
|
<ComboBox
|
|
labelText="Seleccionar subgrupo"
|
|
items={gruposDisponibles.map((g) => ({ id: g.id, text: g.nombre }))}
|
|
itemToString={(item: { id: number; text: string } | null) => (item ? item.text : '')}
|
|
on:select={(e) => (selectedSubgrupoItem = e.detail.selectedItem)}
|
|
/>
|
|
{/if}
|
|
</Modal>
|
|
</form>
|
|
|
|
<Toast
|
|
kind={successMsg ? 'success' : 'error'}
|
|
title={successMsg || 'Error'}
|
|
subtitle={successMsg ? '' : errorMsg}
|
|
open={notifOpen}
|
|
/>
|
|
|
|
<style>
|
|
.page-container {
|
|
padding: 6rem 0 4rem;
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.page-title {
|
|
font-size: 1.75rem;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.detail-section {
|
|
margin-top: 2rem;
|
|
padding: 1.5rem;
|
|
border: 1px solid var(--cds-border-subtle-01);
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.detail-header {
|
|
margin-bottom: 1.5rem;
|
|
}
|
|
|
|
.section-title {
|
|
font-size: 1.25rem;
|
|
font-weight: 600;
|
|
margin: 0;
|
|
}
|
|
|
|
.text-secondary {
|
|
color: var(--cds-text-secondary, #6f6f6f);
|
|
margin-top: 0.25rem;
|
|
}
|
|
|
|
.detail-grid {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 1.5rem;
|
|
}
|
|
|
|
.detail-col {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.75rem;
|
|
}
|
|
|
|
.detail-col-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
}
|
|
|
|
.detail-col-header h3 {
|
|
font-size: 1rem;
|
|
font-weight: 600;
|
|
margin: 0;
|
|
}
|
|
</style>
|