Private
Public Access
algunos refactors y arregle el tema de que no se eliminaban los
subgrupos
This commit is contained in:
1 parent
ab88be0ba5
commit
df2412f29a
25 files changed
+415
-261
No files matched your search
@@ -1,42 +1,32 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Button,
|
||||
ComboBox,
|
||||
DataTableSkeleton,
|
||||
InlineNotification,
|
||||
Modal,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableContainer,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TextInput,
|
||||
TextArea
|
||||
} from 'carbon-components-svelte';
|
||||
import { Button, DataTableSkeleton, InlineNotification, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow } from 'carbon-components-svelte';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import Header from '$lib/components/Header.svelte';
|
||||
import Toast from '$lib/components/Toast.svelte';
|
||||
import GrupoFormModal from './GrupoFormModal.svelte';
|
||||
import DeleteGrupoModal from './DeleteGrupoModal.svelte';
|
||||
import AgregarPermisoModal from './AgregarPermisoModal.svelte';
|
||||
import AgregarSubgrupoModal from './AgregarSubgrupoModal.svelte';
|
||||
import QuitarSubgrupoModal from './QuitarSubgrupoModal.svelte';
|
||||
import {
|
||||
obtenerGrupos,
|
||||
crearGrupo,
|
||||
actualizarGrupo,
|
||||
eliminarGrupo,
|
||||
obtenerPermisos,
|
||||
asignarPermisoAGrupo,
|
||||
quitarPermisoAGrupo,
|
||||
agregarSubGrupo,
|
||||
quitarSubGrupo
|
||||
} from '$lib/api';
|
||||
import type { Grupo, Permiso } from '../../types';
|
||||
import type { Grupo } from '../../types';
|
||||
import type { PageData } 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 { data }: { data: PageData } = $props();
|
||||
|
||||
let loading = $state(false);
|
||||
let error = $state('');
|
||||
|
||||
let modalOpen = $state(false);
|
||||
@@ -53,8 +43,9 @@
|
||||
|
||||
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 quitarSubgrupoModalOpen = $state(false);
|
||||
let quitarSubgrupoId = $state(0);
|
||||
let quitarSubgrupoNombre = $state('');
|
||||
|
||||
let successMsg = $state('');
|
||||
let errorMsg = $state('');
|
||||
@@ -66,48 +57,6 @@
|
||||
{ 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;
|
||||
}
|
||||
@@ -121,12 +70,20 @@
|
||||
}, 4000);
|
||||
}
|
||||
|
||||
function handleSecondary() {
|
||||
modalOpen = false;
|
||||
}
|
||||
|
||||
function handleDeleteSecondary() {
|
||||
deleteModalOpen = false;
|
||||
async function recargar() {
|
||||
loading = true;
|
||||
error = '';
|
||||
try {
|
||||
await invalidate(() => true);
|
||||
if (selectedGrupo) {
|
||||
const updated = data.grupos.find((g) => g.id === selectedGrupo!.id);
|
||||
if (updated) selectedGrupo = updated;
|
||||
}
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Error al recargar';
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function guardarGrupo() {
|
||||
@@ -154,7 +111,7 @@
|
||||
}
|
||||
modalOpen = false;
|
||||
selectedGrupo = null;
|
||||
await cargarGrupos();
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al guardar grupo';
|
||||
notifOpen = true;
|
||||
@@ -170,7 +127,7 @@
|
||||
deleteModalOpen = false;
|
||||
selectedGrupo = null;
|
||||
mostrarNotificacion('Grupo eliminado', '');
|
||||
await cargarGrupos();
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al eliminar grupo';
|
||||
notifOpen = true;
|
||||
@@ -180,25 +137,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
function abrirModalAgregarPermiso() {
|
||||
selectedPermisoItem = null;
|
||||
permisoModalOpen = true;
|
||||
}
|
||||
|
||||
function abrirModalAgregarSubgrupo() {
|
||||
selectedSubgrupoItem = null;
|
||||
subgrupoModalOpen = true;
|
||||
}
|
||||
|
||||
async function agregarPermiso() {
|
||||
if (!selectedGrupo || !selectedPermisoItem) return;
|
||||
async function agregarPermiso(item: { id: number; text: string }) {
|
||||
if (!selectedGrupo) return;
|
||||
try {
|
||||
await asignarPermisoAGrupo(selectedGrupo.id, selectedPermisoItem.id);
|
||||
await asignarPermisoAGrupo(selectedGrupo.id, item.id);
|
||||
mostrarNotificacion('Permiso asignado', '');
|
||||
permisoModalOpen = false;
|
||||
await cargarGrupos();
|
||||
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
||||
if (updated) selectedGrupo = updated;
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al asignar permiso';
|
||||
notifOpen = true;
|
||||
@@ -213,9 +158,7 @@
|
||||
try {
|
||||
await quitarPermisoAGrupo(selectedGrupo.id, permisoId);
|
||||
mostrarNotificacion('Permiso quitado', '');
|
||||
await cargarGrupos();
|
||||
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
||||
if (updated) selectedGrupo = updated;
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al quitar permiso';
|
||||
notifOpen = true;
|
||||
@@ -225,15 +168,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function agregarSubgrupo() {
|
||||
if (!selectedGrupo || !selectedSubgrupoItem) return;
|
||||
async function agregarSubgrupo(item: { id: number; text: string }) {
|
||||
if (!selectedGrupo) return;
|
||||
try {
|
||||
await agregarSubGrupo(selectedGrupo.id, selectedSubgrupoItem.id);
|
||||
await agregarSubGrupo(selectedGrupo.id, item.id);
|
||||
mostrarNotificacion('Subgrupo agregado', '');
|
||||
subgrupoModalOpen = false;
|
||||
await cargarGrupos();
|
||||
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
||||
if (updated) selectedGrupo = updated;
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al agregar subgrupo';
|
||||
notifOpen = true;
|
||||
@@ -243,14 +184,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function quitarSubgrupo(childGroupId: number) {
|
||||
async function confirmarQuitarSubgrupo() {
|
||||
if (!selectedGrupo) return;
|
||||
try {
|
||||
await quitarSubGrupo(selectedGrupo.id, childGroupId);
|
||||
await quitarSubGrupo(selectedGrupo.id, quitarSubgrupoId);
|
||||
mostrarNotificacion('Subgrupo quitado', '');
|
||||
await cargarGrupos();
|
||||
const updated = grupos.find((g) => g.id === selectedGrupo!.id);
|
||||
if (updated) selectedGrupo = updated;
|
||||
quitarSubgrupoModalOpen = false;
|
||||
await recargar();
|
||||
} catch (e) {
|
||||
errorMsg = e instanceof Error ? e.message : 'Error al quitar subgrupo';
|
||||
notifOpen = true;
|
||||
@@ -261,20 +201,15 @@
|
||||
}
|
||||
|
||||
let permisosDisponibles = $derived(
|
||||
allPermisos.filter((p) => !selectedGrupo?.permisos?.some((sp) => sp.id === p.id))
|
||||
data.permisos.filter((p) => !selectedGrupo?.permisos?.some((sp) => sp.id === p.id))
|
||||
);
|
||||
|
||||
let gruposDisponibles = $derived(
|
||||
grupos.filter(
|
||||
data.grupos.filter(
|
||||
(g) =>
|
||||
g.id !== selectedGrupo?.id && !selectedGrupo?.grupos_hijos?.some((sg) => sg.id === g.id)
|
||||
)
|
||||
);
|
||||
|
||||
$effect(() => {
|
||||
cargarGrupos();
|
||||
cargarPermisos();
|
||||
});
|
||||
</script>
|
||||
|
||||
<Header />
|
||||
@@ -282,7 +217,7 @@
|
||||
<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>
|
||||
<Button onclick={() => { modalEditando = false; formId = 0; formNombre = ''; formDescripcion = ''; modalOpen = true; }} icon={Add}>Nuevo Grupo</Button>
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
@@ -303,7 +238,7 @@
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each grupos as g (g.id)}
|
||||
{#each data.grupos as g (g.id)}
|
||||
<TableRow
|
||||
class="selected-row={selectedGrupo?.id === g.id}"
|
||||
onclick={() => seleccionarGrupo(g)}
|
||||
@@ -315,14 +250,14 @@
|
||||
<TableCell>{String(g[header.key] ?? '-')}</TableCell>
|
||||
{/each}
|
||||
<TableCell class="acciones-cell">
|
||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => abrirModalEditar(g)}>
|
||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => { modalEditando = true; formId = g.id; formNombre = g.nombre; formDescripcion = g.descripcion ?? ''; modalOpen = true; }}>
|
||||
Editar
|
||||
</Button>
|
||||
<Button
|
||||
kind="ghost"
|
||||
size="small"
|
||||
icon={TrashCan}
|
||||
onclick={() => abrirModalEliminar(g)}
|
||||
onclick={() => { deleteId = g.id; deleteNombre = g.nombre; deleteModalOpen = true; }}
|
||||
>
|
||||
Eliminar
|
||||
</Button>
|
||||
@@ -348,7 +283,7 @@
|
||||
<div class="detail-col">
|
||||
<div class="detail-col-header">
|
||||
<h3>Permisos</h3>
|
||||
<Button kind="ghost" size="small" icon={Add} onclick={abrirModalAgregarPermiso}>
|
||||
<Button kind="ghost" size="small" icon={Add} onclick={() => permisoModalOpen = true}>
|
||||
Agregar
|
||||
</Button>
|
||||
</div>
|
||||
@@ -389,7 +324,7 @@
|
||||
<div class="detail-col">
|
||||
<div class="detail-col-header">
|
||||
<h3>Subgrupos</h3>
|
||||
<Button kind="ghost" size="small" icon={Add} onclick={abrirModalAgregarSubgrupo}>
|
||||
<Button kind="ghost" size="small" icon={Add} onclick={() => subgrupoModalOpen = true}>
|
||||
Agregar
|
||||
</Button>
|
||||
</div>
|
||||
@@ -412,7 +347,7 @@
|
||||
kind="ghost"
|
||||
size="small"
|
||||
icon={Close}
|
||||
onclick={() => quitarSubgrupo(sg.id)}
|
||||
onclick={() => { quitarSubgrupoId = sg.id; quitarSubgrupoNombre = sg.nombre; quitarSubgrupoModalOpen = true; }}
|
||||
>
|
||||
Quitar
|
||||
</Button>
|
||||
@@ -431,85 +366,42 @@
|
||||
{/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>
|
||||
<GrupoFormModal
|
||||
bind:open={modalOpen}
|
||||
editando={modalEditando}
|
||||
bind:nombre={formNombre}
|
||||
bind:descripcion={formDescripcion}
|
||||
alGuardar={guardarGrupo}
|
||||
alCerrar={() => (modalOpen = false)}
|
||||
/>
|
||||
|
||||
<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>
|
||||
<DeleteGrupoModal
|
||||
bind:open={deleteModalOpen}
|
||||
nombre={deleteNombre}
|
||||
alConfirmar={confirmarEliminar}
|
||||
alCerrar={() => (deleteModalOpen = false)}
|
||||
/>
|
||||
|
||||
<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>
|
||||
<AgregarPermisoModal
|
||||
bind:open={permisoModalOpen}
|
||||
{permisosDisponibles}
|
||||
alSeleccionar={agregarPermiso}
|
||||
alCerrar={() => (permisoModalOpen = false)}
|
||||
/>
|
||||
|
||||
<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>
|
||||
<AgregarSubgrupoModal
|
||||
bind:open={subgrupoModalOpen}
|
||||
{gruposDisponibles}
|
||||
alSeleccionar={agregarSubgrupo}
|
||||
alCerrar={() => (subgrupoModalOpen = false)}
|
||||
/>
|
||||
|
||||
<QuitarSubgrupoModal
|
||||
bind:open={quitarSubgrupoModalOpen}
|
||||
nombre={quitarSubgrupoNombre}
|
||||
alConfirmar={confirmarQuitarSubgrupo}
|
||||
alCerrar={() => (quitarSubgrupoModalOpen = false)}
|
||||
/>
|
||||
|
||||
<Toast
|
||||
kind={successMsg ? 'success' : 'error'}
|
||||
|
||||
Reference in new issue
Block a user