Private
Public Access
fix: arreglado paginador
This commit is contained in:
1 parent
aac88856ac
commit
3af069e9e8
8 files changed
+74
-41
No files matched your search
@@ -84,6 +84,7 @@ func generarLogCreate(tx *gorm.DB) {
|
|||||||
|
|
||||||
detalles := []logs.LogDetalle{
|
detalles := []logs.LogDetalle{
|
||||||
{
|
{
|
||||||
|
ID: 0,
|
||||||
NombreTabla: tx.Statement.Table,
|
NombreTabla: tx.Statement.Table,
|
||||||
Columna: "*",
|
Columna: "*",
|
||||||
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", obtenerID(tx.Statement.Dest)),
|
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", obtenerID(tx.Statement.Dest)),
|
||||||
@@ -100,6 +101,7 @@ func generarLogDelete(tx *gorm.DB) {
|
|||||||
|
|
||||||
detalles := []logs.LogDetalle{
|
detalles := []logs.LogDetalle{
|
||||||
{
|
{
|
||||||
|
ID: 0,
|
||||||
NombreTabla: tx.Statement.Table,
|
NombreTabla: tx.Statement.Table,
|
||||||
Columna: "*",
|
Columna: "*",
|
||||||
ValorAnterior: fmt.Sprintf("Registro eliminado (ID: %v)", obtenerID(tx.Statement.Dest)),
|
ValorAnterior: fmt.Sprintf("Registro eliminado (ID: %v)", obtenerID(tx.Statement.Dest)),
|
||||||
@@ -148,6 +150,7 @@ func generarDetalles(tabla string, anterior, nuevo interface{}) []logs.LogDetall
|
|||||||
|
|
||||||
if vAnt != vNue {
|
if vAnt != vNue {
|
||||||
detalles = append(detalles, logs.LogDetalle{
|
detalles = append(detalles, logs.LogDetalle{
|
||||||
|
ID: uint(i),
|
||||||
NombreTabla: tabla,
|
NombreTabla: tabla,
|
||||||
Columna: campo.Name,
|
Columna: campo.Name,
|
||||||
ValorAnterior: vAnt,
|
ValorAnterior: vAnt,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package logs
|
package logs
|
||||||
|
|
||||||
type LogDetalle struct {
|
type LogDetalle struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
NombreTabla string `json:"nombreTabla"`
|
NombreTabla string `json:"nombreTabla"`
|
||||||
Columna string `json:"columna"`
|
Columna string `json:"columna"`
|
||||||
ValorAnterior string `json:"valorAnterior,omitempty"`
|
ValorAnterior string `json:"valorAnterior,omitempty"`
|
||||||
|
|||||||
@@ -11,5 +11,6 @@ export async function obtenerLogs(pagina: number): Promise<RespuestaPagina<LogEn
|
|||||||
});
|
});
|
||||||
if (!res.ok) throw new Error(`No se pudieron cargar los logs (${res.status})`);
|
if (!res.ok) throw new Error(`No se pudieron cargar los logs (${res.status})`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
return { logs: data.logs, maxPagina: data.maxLenght };
|
const totalPages = Math.ceil(data.maxlenght / 10);
|
||||||
|
return { logs: data.logs, maxPagina: Math.max(0, totalPages - 1), totalItems: data.maxlenght };
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import Header from '$lib/components/Header.svelte';
|
import Header from '$lib/components/Header.svelte';
|
||||||
import {
|
import {
|
||||||
DataTable,
|
DataTable,
|
||||||
PaginationNav,
|
Pagination,
|
||||||
InlineLoading,
|
InlineLoading,
|
||||||
Tag,
|
Tag,
|
||||||
Tile,
|
Tile,
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
import { LogsCache, type RespuestaPagina } from './LogsEstado.svelte';
|
import { LogsCache, type RespuestaPagina } from './LogsEstado.svelte';
|
||||||
import type { LogEntry } from '../../types';
|
import type { LogEntry } from '../../types';
|
||||||
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
||||||
|
import { ArrowRight } from 'carbon-icons-svelte';
|
||||||
|
|
||||||
const cache = new LogsCache<LogEntry>();
|
const cache = new LogsCache<LogEntry>();
|
||||||
|
|
||||||
@@ -32,8 +33,8 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
const headers = [
|
const headers = [
|
||||||
|
{ key: 'id', value: '#' },
|
||||||
{ key: 'created_at', value: 'Fecha' },
|
{ key: 'created_at', value: 'Fecha' },
|
||||||
{ key: 'empresaId', value: 'Empresa' },
|
|
||||||
{ key: 'usuario', value: 'Usuario' },
|
{ key: 'usuario', value: 'Usuario' },
|
||||||
{ key: 'accion', value: 'Acción' }
|
{ key: 'accion', value: 'Acción' }
|
||||||
];
|
];
|
||||||
@@ -48,7 +49,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const cargandoActual = $derived(() => cache.estaCargando(cache.paginaActual()));
|
const cargandoActual = $derived.by(() => cache.estaCargando(cache.pagina));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Header />
|
<Header />
|
||||||
@@ -60,8 +61,8 @@
|
|||||||
size="small"
|
size="small"
|
||||||
icon={Renew}
|
icon={Renew}
|
||||||
iconDescription="Recargar página actual"
|
iconDescription="Recargar página actual"
|
||||||
disabled={cargandoActual()}
|
disabled={cargandoActual}
|
||||||
onclick={() => irAPagina(cache.paginaActual(), true)}
|
onclick={() => irAPagina(cache.pagina, true)}
|
||||||
>
|
>
|
||||||
Recargar
|
Recargar
|
||||||
</Button>
|
</Button>
|
||||||
@@ -70,9 +71,9 @@
|
|||||||
{#if error}
|
{#if error}
|
||||||
<Tile>
|
<Tile>
|
||||||
<p class="mb-2 text-red-600">{error}</p>
|
<p class="mb-2 text-red-600">{error}</p>
|
||||||
<Button size="small" onclick={() => irAPagina(cache.paginaActual(), true)}>Reintentar</Button>
|
<Button size="small" onclick={() => irAPagina(cache.pagina, true)}>Reintentar</Button>
|
||||||
</Tile>
|
</Tile>
|
||||||
{:else if cargandoActual() && logs.length === 0}
|
{:else if cargandoActual && logs.length === 0}
|
||||||
<InlineLoading description="Cargando logs…" />
|
<InlineLoading description="Cargando logs…" />
|
||||||
{:else if logs.length === 0}
|
{:else if logs.length === 0}
|
||||||
<Tile>
|
<Tile>
|
||||||
@@ -92,18 +93,33 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
|
|
||||||
<svelte:fragment slot="expanded-row" let:row>
|
<svelte:fragment slot="expandedRow" let:row>
|
||||||
{#if row.detalles && row.detalles.length > 0}
|
{#if row.detalles && row.detalles.length > 0}
|
||||||
<div class="flex flex-col gap-2 py-2">
|
{@const detallesConId = row.detalles.map((d, i) => ({ ...d, id: d.id || i + 1 }))}
|
||||||
{#each row.detalles as d}
|
<DataTable
|
||||||
<div class="grid grid-cols-4 gap-2 text-sm">
|
headers={[
|
||||||
<span class="font-medium">{d.nombreTabla}</span>
|
{ key: 'nombreTabla', value: 'Tabla' },
|
||||||
<span>{d.columna}</span>
|
{ key: 'columna', value: 'Columna' },
|
||||||
<span class="text-red-600 line-through">{d.valorAnterior ?? '—'}</span>
|
{ key: 'valorAnterior', value: 'Valor anterior' },
|
||||||
<span class="text-green-600">{d.valorNuevo ?? '—'}</span>
|
{ key: 'valorNuevo', value: 'Valor nuevo' }
|
||||||
</div>
|
]}
|
||||||
{/each}
|
rows={detallesConId}
|
||||||
</div>
|
size="compact"
|
||||||
|
>
|
||||||
|
<svelte:fragment slot="cell" let:cell let:row>
|
||||||
|
{#if cell.key === 'valorAnterior'}
|
||||||
|
<span class="text-red-600">{cell.value ?? '—'}</span>
|
||||||
|
{:else if cell.key === 'valorNuevo'}
|
||||||
|
{#if cell.value}
|
||||||
|
<span class="text-green-600">{cell.value}</span>
|
||||||
|
{:else}
|
||||||
|
<span>—</span>
|
||||||
|
{/if}
|
||||||
|
{:else}
|
||||||
|
{cell.value}
|
||||||
|
{/if}
|
||||||
|
</svelte:fragment>
|
||||||
|
</DataTable>
|
||||||
{:else}
|
{:else}
|
||||||
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
|
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
|
||||||
{/if}
|
{/if}
|
||||||
@@ -111,14 +127,15 @@
|
|||||||
</DataTable>
|
</DataTable>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<div class="flex items-center gap-3">
|
<Pagination
|
||||||
<PaginationNav
|
page={cache.pagina + 1}
|
||||||
page={cache.paginaActual()}
|
totalItems={cache.totalItems}
|
||||||
total={cache.paginaMaxima() + 1}
|
pageSize={10}
|
||||||
on:change={(e) => irAPagina(e.detail)}
|
pageSizes={[10]}
|
||||||
|
pageSizeInputDisabled={true}
|
||||||
|
on:change={(e) => irAPagina(e.detail.page - 1)}
|
||||||
/>
|
/>
|
||||||
{#if cache.ultimoFueCache()}
|
<!-- {#if cache.ultimoHit}
|
||||||
<span class="text-xs text-gray-500">desde caché</span>
|
<span class="text-xs text-gray-500">desde caché</span>
|
||||||
{/if}
|
{/if} -->
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
@@ -4,20 +4,19 @@ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
|||||||
export interface RespuestaPagina<T> {
|
export interface RespuestaPagina<T> {
|
||||||
logs: T[];
|
logs: T[];
|
||||||
maxPagina: number;
|
maxPagina: number;
|
||||||
|
totalItems: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LogsCache<T = unknown> {
|
export class LogsCache<T = unknown> {
|
||||||
#cache = new SvelteMap<number, T[]>();
|
#cache = new SvelteMap<number, T[]>();
|
||||||
#cargando = new SvelteSet<number>();
|
#cargando = new SvelteSet<number>();
|
||||||
#enVuelo = new Map<number, Promise<T[]>>();
|
#enVuelo = new Map<number, Promise<T[]>>();
|
||||||
#pagina = $state(0);
|
pagina = $state(0);
|
||||||
#maxPagina = $state(0);
|
maxPagina = $state(0);
|
||||||
|
totalItems = $state(0);
|
||||||
|
ultimoHit = $state(true);
|
||||||
|
|
||||||
#ultimoHit = $state(true);
|
paginaActMax = $derived(() => `${this.pagina}/${this.maxPagina}`);
|
||||||
ultimoFueCache = $derived(() => this.#ultimoHit);
|
|
||||||
paginaActual = $derived(() => this.#pagina);
|
|
||||||
paginaMaxima = $derived(() => this.#maxPagina);
|
|
||||||
paginaActMax = $derived(() => `${this.#pagina}/${this.#maxPagina}`);
|
|
||||||
|
|
||||||
tieneCache(pagina: number): boolean {
|
tieneCache(pagina: number): boolean {
|
||||||
return this.#cache.has(pagina);
|
return this.#cache.has(pagina);
|
||||||
@@ -36,10 +35,10 @@ export class LogsCache<T = unknown> {
|
|||||||
fetcher: (pagina: number) => Promise<RespuestaPagina<T>>,
|
fetcher: (pagina: number) => Promise<RespuestaPagina<T>>,
|
||||||
forzar = false
|
forzar = false
|
||||||
): Promise<T[]> {
|
): Promise<T[]> {
|
||||||
this.#pagina = pagina;
|
this.pagina = pagina;
|
||||||
|
|
||||||
if (!forzar && this.#cache.has(pagina)) {
|
if (!forzar && this.#cache.has(pagina)) {
|
||||||
this.#ultimoHit = true;
|
this.ultimoHit = true;
|
||||||
return this.#cache.get(pagina)!;
|
return this.#cache.get(pagina)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,12 +46,13 @@ export class LogsCache<T = unknown> {
|
|||||||
return this.#enVuelo.get(pagina)!;
|
return this.#enVuelo.get(pagina)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#ultimoHit = false;
|
this.ultimoHit = false;
|
||||||
this.#cargando.add(pagina);
|
this.#cargando.add(pagina);
|
||||||
const promesa = fetcher(pagina)
|
const promesa = fetcher(pagina)
|
||||||
.then(({ logs, maxPagina }) => {
|
.then(({ logs, maxPagina, totalItems }) => {
|
||||||
this.#cache.set(pagina, logs);
|
this.#cache.set(pagina, logs);
|
||||||
this.#maxPagina = maxPagina;
|
this.maxPagina = maxPagina;
|
||||||
|
this.totalItems = totalItems;
|
||||||
return logs;
|
return logs;
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
@@ -77,10 +77,10 @@ export class LogsCache<T = unknown> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get puedeAvanzar(): boolean {
|
get puedeAvanzar(): boolean {
|
||||||
return this.#pagina < this.#maxPagina;
|
return this.pagina < this.maxPagina;
|
||||||
}
|
}
|
||||||
|
|
||||||
get puedeRetroceder(): boolean {
|
get puedeRetroceder(): boolean {
|
||||||
return this.#pagina > 0;
|
return this.pagina > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
let { open = $bindable(), onClose } = $props();
|
let { open = $bindable(), onClose } = $props();
|
||||||
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
|
let enviando = $state(false);
|
||||||
let repuesto: CrearRepuestoDto = $state({
|
let repuesto: CrearRepuestoDto = $state({
|
||||||
nombre: '',
|
nombre: '',
|
||||||
descripcion: '',
|
descripcion: '',
|
||||||
@@ -35,6 +36,8 @@
|
|||||||
id="crearRepuestoForm"
|
id="crearRepuestoForm"
|
||||||
onsubmit={async (e) => {
|
onsubmit={async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (enviando) return;
|
||||||
|
enviando = true;
|
||||||
error = '';
|
error = '';
|
||||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||||
if (repuesto.marca == '') error += 'Falta Marca, ';
|
if (repuesto.marca == '') error += 'Falta Marca, ';
|
||||||
@@ -48,6 +51,7 @@
|
|||||||
invalidate('repuestos:load');
|
invalidate('repuestos:load');
|
||||||
handleClose();
|
handleClose();
|
||||||
}
|
}
|
||||||
|
enviando = false;
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -56,6 +60,7 @@
|
|||||||
size={'lg'}
|
size={'lg'}
|
||||||
modalHeading="Registrar Repuesto"
|
modalHeading="Registrar Repuesto"
|
||||||
primaryButtonText="Registrar"
|
primaryButtonText="Registrar"
|
||||||
|
primaryButtonDisabled={enviando}
|
||||||
secondaryButtonText="Cerrar"
|
secondaryButtonText="Cerrar"
|
||||||
on:click:button--secondary={handleClose}
|
on:click:button--secondary={handleClose}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
||||||
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
|
let enviando = $state(false);
|
||||||
|
|
||||||
function handleClose() {
|
function handleClose() {
|
||||||
reset();
|
reset();
|
||||||
@@ -33,6 +34,8 @@
|
|||||||
id="actualizarRepuestoForm"
|
id="actualizarRepuestoForm"
|
||||||
onsubmit={async (e) => {
|
onsubmit={async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (enviando) return;
|
||||||
|
enviando = true;
|
||||||
error = '';
|
error = '';
|
||||||
if (repuesto.id == 0) error += 'Falta id';
|
if (repuesto.id == 0) error += 'Falta id';
|
||||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||||
@@ -47,6 +50,7 @@
|
|||||||
invalidate('repuestos:load');
|
invalidate('repuestos:load');
|
||||||
handleClose();
|
handleClose();
|
||||||
}
|
}
|
||||||
|
enviando = false;
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -55,6 +59,7 @@
|
|||||||
size={'lg'}
|
size={'lg'}
|
||||||
modalHeading={`Editar Repuesto: ${repuesto.id}`}
|
modalHeading={`Editar Repuesto: ${repuesto.id}`}
|
||||||
primaryButtonText="Editar"
|
primaryButtonText="Editar"
|
||||||
|
primaryButtonDisabled={enviando}
|
||||||
secondaryButtonText="Cerrar"
|
secondaryButtonText="Cerrar"
|
||||||
on:click:button--secondary={handleClose}
|
on:click:button--secondary={handleClose}
|
||||||
>
|
>
|
||||||
|
|||||||
Vendored
+1
@@ -23,6 +23,7 @@ export interface LogEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface LogDetalle {
|
export interface LogDetalle {
|
||||||
|
id: number;
|
||||||
nombreTabla: string;
|
nombreTabla: string;
|
||||||
columna: string;
|
columna: string;
|
||||||
valorAnterior?: string;
|
valorAnterior?: string;
|
||||||
|
|||||||
Reference in new issue
Block a user