fix: arreglado paginador

This commit is contained in:
fede committed 2026-07-09 04:05:43 -03:00
1 parent aac88856ac
commit 3af069e9e8
8 files changed
+74 -41

No files matched your search

+3
View File
@@ -84,6 +84,7 @@ func generarLogCreate(tx *gorm.DB) {
detalles := []logs.LogDetalle{
{
ID: 0,
NombreTabla: tx.Statement.Table,
Columna: "*",
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", obtenerID(tx.Statement.Dest)),
@@ -100,6 +101,7 @@ func generarLogDelete(tx *gorm.DB) {
detalles := []logs.LogDetalle{
{
ID: 0,
NombreTabla: tx.Statement.Table,
Columna: "*",
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 {
detalles = append(detalles, logs.LogDetalle{
ID: uint(i),
NombreTabla: tabla,
Columna: campo.Name,
ValorAnterior: vAnt,
+1
View File
@@ -1,6 +1,7 @@
package logs
type LogDetalle struct {
ID uint `json:"id"`
NombreTabla string `json:"nombreTabla"`
Columna string `json:"columna"`
ValorAnterior string `json:"valorAnterior,omitempty"`
+2 -1
View File
@@ -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})`);
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 };
}
+43 -26
View File
@@ -2,7 +2,7 @@
import Header from '$lib/components/Header.svelte';
import {
DataTable,
PaginationNav,
Pagination,
InlineLoading,
Tag,
Tile,
@@ -12,6 +12,7 @@
import { LogsCache, type RespuestaPagina } from './LogsEstado.svelte';
import type { LogEntry } from '../../types';
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
import { ArrowRight } from 'carbon-icons-svelte';
const cache = new LogsCache<LogEntry>();
@@ -32,8 +33,8 @@
});
const headers = [
{ key: 'id', value: '#' },
{ key: 'created_at', value: 'Fecha' },
{ key: 'empresaId', value: 'Empresa' },
{ key: 'usuario', value: 'Usuario' },
{ 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>
<Header />
@@ -60,8 +61,8 @@
size="small"
icon={Renew}
iconDescription="Recargar página actual"
disabled={cargandoActual()}
onclick={() => irAPagina(cache.paginaActual(), true)}
disabled={cargandoActual}
onclick={() => irAPagina(cache.pagina, true)}
>
Recargar
</Button>
@@ -70,9 +71,9 @@
{#if error}
<Tile>
<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>
{:else if cargandoActual() && logs.length === 0}
{:else if cargandoActual && logs.length === 0}
<InlineLoading description="Cargando logs…" />
{:else if logs.length === 0}
<Tile>
@@ -92,18 +93,33 @@
{/if}
</svelte:fragment>
<svelte:fragment slot="expanded-row" let:row>
<svelte:fragment slot="expandedRow" let:row>
{#if row.detalles && row.detalles.length > 0}
<div class="flex flex-col gap-2 py-2">
{#each row.detalles as d}
<div class="grid grid-cols-4 gap-2 text-sm">
<span class="font-medium">{d.nombreTabla}</span>
<span>{d.columna}</span>
<span class="text-red-600 line-through">{d.valorAnterior ?? '—'}</span>
<span class="text-green-600">{d.valorNuevo ?? '—'}</span>
</div>
{/each}
</div>
{@const detallesConId = row.detalles.map((d, i) => ({ ...d, id: d.id || i + 1 }))}
<DataTable
headers={[
{ key: 'nombreTabla', value: 'Tabla' },
{ key: 'columna', value: 'Columna' },
{ key: 'valorAnterior', value: 'Valor anterior' },
{ key: 'valorNuevo', value: 'Valor nuevo' }
]}
rows={detallesConId}
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}
<p class="py-2 text-sm text-gray-500">Sin detalles registrados.</p>
{/if}
@@ -111,14 +127,15 @@
</DataTable>
{/if}
<div class="flex items-center gap-3">
<PaginationNav
page={cache.paginaActual()}
total={cache.paginaMaxima() + 1}
on:change={(e) => irAPagina(e.detail)}
<Pagination
page={cache.pagina + 1}
totalItems={cache.totalItems}
pageSize={10}
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>
{/if}
</div>
{/if} -->
</div>
+14 -14
View File
@@ -4,20 +4,19 @@ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
export interface RespuestaPagina<T> {
logs: T[];
maxPagina: number;
totalItems: number;
}
export class LogsCache<T = unknown> {
#cache = new SvelteMap<number, T[]>();
#cargando = new SvelteSet<number>();
#enVuelo = new Map<number, Promise<T[]>>();
#pagina = $state(0);
#maxPagina = $state(0);
pagina = $state(0);
maxPagina = $state(0);
totalItems = $state(0);
ultimoHit = $state(true);
#ultimoHit = $state(true);
ultimoFueCache = $derived(() => this.#ultimoHit);
paginaActual = $derived(() => this.#pagina);
paginaMaxima = $derived(() => this.#maxPagina);
paginaActMax = $derived(() => `${this.#pagina}/${this.#maxPagina}`);
paginaActMax = $derived(() => `${this.pagina}/${this.maxPagina}`);
tieneCache(pagina: number): boolean {
return this.#cache.has(pagina);
@@ -36,10 +35,10 @@ export class LogsCache<T = unknown> {
fetcher: (pagina: number) => Promise<RespuestaPagina<T>>,
forzar = false
): Promise<T[]> {
this.#pagina = pagina;
this.pagina = pagina;
if (!forzar && this.#cache.has(pagina)) {
this.#ultimoHit = true;
this.ultimoHit = true;
return this.#cache.get(pagina)!;
}
@@ -47,12 +46,13 @@ export class LogsCache<T = unknown> {
return this.#enVuelo.get(pagina)!;
}
this.#ultimoHit = false;
this.ultimoHit = false;
this.#cargando.add(pagina);
const promesa = fetcher(pagina)
.then(({ logs, maxPagina }) => {
.then(({ logs, maxPagina, totalItems }) => {
this.#cache.set(pagina, logs);
this.#maxPagina = maxPagina;
this.maxPagina = maxPagina;
this.totalItems = totalItems;
return logs;
})
.finally(() => {
@@ -77,10 +77,10 @@ export class LogsCache<T = unknown> {
}
get puedeAvanzar(): boolean {
return this.#pagina < this.#maxPagina;
return this.pagina < this.maxPagina;
}
get puedeRetroceder(): boolean {
return this.#pagina > 0;
return this.pagina > 0;
}
}
@@ -7,6 +7,7 @@
let { open = $bindable(), onClose } = $props();
let error = $state('');
let enviando = $state(false);
let repuesto: CrearRepuestoDto = $state({
nombre: '',
descripcion: '',
@@ -35,6 +36,8 @@
id="crearRepuestoForm"
onsubmit={async (e) => {
e.preventDefault();
if (enviando) return;
enviando = true;
error = '';
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
if (repuesto.marca == '') error += 'Falta Marca, ';
@@ -48,6 +51,7 @@
invalidate('repuestos:load');
handleClose();
}
enviando = false;
}}
>
<Modal
@@ -56,6 +60,7 @@
size={'lg'}
modalHeading="Registrar Repuesto"
primaryButtonText="Registrar"
primaryButtonDisabled={enviando}
secondaryButtonText="Cerrar"
on:click:button--secondary={handleClose}
>
@@ -11,6 +11,7 @@
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
let error = $state('');
let enviando = $state(false);
function handleClose() {
reset();
@@ -33,6 +34,8 @@
id="actualizarRepuestoForm"
onsubmit={async (e) => {
e.preventDefault();
if (enviando) return;
enviando = true;
error = '';
if (repuesto.id == 0) error += 'Falta id';
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
@@ -47,6 +50,7 @@
invalidate('repuestos:load');
handleClose();
}
enviando = false;
}}
>
<Modal
@@ -55,6 +59,7 @@
size={'lg'}
modalHeading={`Editar Repuesto: ${repuesto.id}`}
primaryButtonText="Editar"
primaryButtonDisabled={enviando}
secondaryButtonText="Cerrar"
on:click:button--secondary={handleClose}
>
+1
View File
@@ -23,6 +23,7 @@ export interface LogEntry {
}
export interface LogDetalle {
id: number;
nombreTabla: string;
columna: string;
valorAnterior?: string;