Private
Public Access
feat: hecho filtro de busqueda para los logs
This commit is contained in:
1 parent
1c0da9d70f
commit
a25afa1750
12 files changed
+288
-57
No files matched your search
@@ -1,41 +1,27 @@
|
||||
<script lang="ts">
|
||||
import Header from '$lib/components/Header.svelte';
|
||||
import {
|
||||
DataTable,
|
||||
Pagination,
|
||||
InlineLoading,
|
||||
Tag,
|
||||
Tile,
|
||||
Button
|
||||
} from 'carbon-components-svelte';
|
||||
import { Pagination, InlineLoading, Tile, Button } from 'carbon-components-svelte';
|
||||
import Renew from 'carbon-icons-svelte/lib/Renew.svelte';
|
||||
import { LogsCache } from './LogsEstado.svelte';
|
||||
import type { LogEntry } from '../../types';
|
||||
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
||||
import { Export } from 'carbon-icons-svelte';
|
||||
import { goto, invalidate } from '$app/navigation';
|
||||
import { ExportarLogs } from './ExportarLogs';
|
||||
import TablaLogs from './TablaLogs.svelte';
|
||||
import FiltrosLogs from './FiltrosLogs.svelte';
|
||||
import { page } from '$app/state';
|
||||
|
||||
const cache = new LogsCache<LogEntry>();
|
||||
let { data } = $props();
|
||||
|
||||
let logs = $state<LogEntry[]>([]);
|
||||
let error = $state<string | null>(null);
|
||||
|
||||
async function irAPagina(pagina: number, forzar = false) {
|
||||
error = null;
|
||||
try {
|
||||
logs = await cache.cargarPagina(pagina, obtenerLogs, forzar);
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Error desconocido al cargar los logs';
|
||||
}
|
||||
function irAPagina(pagina: number) {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.set('pagina', String(pagina));
|
||||
goto(`?${params}`, { replaceState: true });
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
irAPagina(0);
|
||||
});
|
||||
let pageNum = $derived(Number(page.url.searchParams.get('pagina') ?? '0') + 1);
|
||||
|
||||
const cargandoActual = $derived.by(() => cache.estaCargando(cache.pagina));
|
||||
function recargar() {
|
||||
invalidate('logs:pagina');
|
||||
}
|
||||
</script>
|
||||
|
||||
<Header />
|
||||
@@ -48,8 +34,7 @@
|
||||
size="small"
|
||||
icon={Renew}
|
||||
iconDescription="Recargar página actual"
|
||||
disabled={cargandoActual}
|
||||
onclick={() => irAPagina(cache.pagina, true)}
|
||||
onclick={recargar}
|
||||
>
|
||||
Recargar
|
||||
</Button>
|
||||
@@ -58,9 +43,9 @@
|
||||
size="small"
|
||||
icon={Export}
|
||||
iconDescription="Exportar CSV"
|
||||
disabled={!(logs && logs.length != 0)}
|
||||
disabled={!(data.logs && data.logs.length != 0)}
|
||||
onclick={() => {
|
||||
new ExportarLogs(logs).descargarCSV();
|
||||
new ExportarLogs(data.logs).descargarCSV();
|
||||
}}
|
||||
>
|
||||
Exportar CSV
|
||||
@@ -68,28 +53,25 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
{#if data.error}
|
||||
<Tile>
|
||||
<p class="mb-2 text-red-600">{error}</p>
|
||||
<Button size="small" onclick={() => irAPagina(cache.pagina, true)}>Reintentar</Button>
|
||||
<p class="mb-2 text-red-600">{data.error}</p>
|
||||
<Button size="small" onclick={recargar}>Reintentar</Button>
|
||||
</Tile>
|
||||
{:else if cargandoActual && logs.length === 0}
|
||||
<InlineLoading description="Cargando logs…" />
|
||||
{:else if logs.length === 0}
|
||||
{:else if data.logs && data.logs.length === 0}
|
||||
<Tile>
|
||||
<p>No hay logs en esta página.</p>
|
||||
</Tile>
|
||||
{:else}
|
||||
<div class="flex flex-col gap-2">
|
||||
<FiltrosLogs bind:logs />
|
||||
|
||||
<TablaLogs {logs} />
|
||||
<FiltrosLogs />
|
||||
<TablaLogs logs={data.logs} />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<Pagination
|
||||
page={cache.pagina + 1}
|
||||
totalItems={cache.totalItems}
|
||||
page={pageNum}
|
||||
totalItems={data.totalItems}
|
||||
pageSize={10}
|
||||
pageSizes={[10]}
|
||||
pageSizeInputDisabled={true}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { PageLoad } from './$types';
|
||||
import { obtenerLogs } from '$lib/hooks/logs/obtenerLogs';
|
||||
import { busquedaLogs } from '$lib/hooks/logs/busquedaLogs';
|
||||
|
||||
export const load: PageLoad = async ({ url, depends, fetch }) => {
|
||||
depends('logs:pagina');
|
||||
|
||||
const pagina = Number(url.searchParams.get('pagina') ?? '0');
|
||||
const usuarioId = url.searchParams.get('usuarioId');
|
||||
const accion = url.searchParams.get('accion');
|
||||
const fechaInicio = url.searchParams.get('fechaInicio');
|
||||
const fechaFin = url.searchParams.get('fechaFin');
|
||||
|
||||
const tieneFiltros = usuarioId || accion || fechaInicio || fechaFin;
|
||||
|
||||
try {
|
||||
if (tieneFiltros) {
|
||||
let filtros: any = {
|
||||
usuarioId: usuarioId ? Number(usuarioId) : 0,
|
||||
accion: accion ?? ''
|
||||
};
|
||||
|
||||
if (fechaInicio) {
|
||||
filtros.fechaInicio = new Date(fechaInicio);
|
||||
}
|
||||
if (fechaFin) {
|
||||
filtros.fechaFin = new Date(fechaFin);
|
||||
}
|
||||
|
||||
const res = await busquedaLogs(pagina, filtros, fetch);
|
||||
if ('error' in res) {
|
||||
return { logs: [], error: res.error, totalItems: 0, maxPagina: 0 };
|
||||
}
|
||||
if (typeof res.logs === 'undefined') res.logs = [];
|
||||
return {
|
||||
logs: res.logs,
|
||||
error: null,
|
||||
totalItems: res.totalItems,
|
||||
maxPagina: res.maxPagina
|
||||
};
|
||||
}
|
||||
|
||||
const res = await obtenerLogs(pagina, fetch);
|
||||
return { logs: res.logs, error: null, totalItems: res.totalItems, maxPagina: res.maxPagina };
|
||||
} catch (e) {
|
||||
return {
|
||||
logs: [],
|
||||
error: e instanceof Error ? e.message : 'Error desconocido al cargar los logs',
|
||||
totalItems: 0,
|
||||
maxPagina: 0
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import {
|
||||
Button,
|
||||
DatePicker,
|
||||
DatePickerInput,
|
||||
Disclosure,
|
||||
@@ -10,27 +11,66 @@
|
||||
Tile
|
||||
} from 'carbon-components-svelte';
|
||||
|
||||
let { logs = $bindable() } = $props();
|
||||
import { goto, invalidate } from '$app/navigation';
|
||||
|
||||
let filtros = $state({
|
||||
idusu: 0
|
||||
usuarioId: 0,
|
||||
accion: '',
|
||||
fechaInicio: '',
|
||||
fechaFin: ''
|
||||
});
|
||||
|
||||
function aplicarFiltros() {
|
||||
const params = new URLSearchParams();
|
||||
params.set('pagina', '0');
|
||||
if (filtros.usuarioId != 0) params.set('usuarioId', String(filtros.usuarioId));
|
||||
if (filtros.accion != '') params.set('accion', filtros.accion);
|
||||
if (filtros.fechaInicio != '') params.set('fechaInicio', filtros.fechaInicio);
|
||||
if (filtros.fechaFin != '') params.set('fechaFin', filtros.fechaFin);
|
||||
goto(`?${params}`);
|
||||
// invalidate('logs:pagina');
|
||||
}
|
||||
$inspect(filtros);
|
||||
function limpiarFiltros() {
|
||||
filtros = { usuarioId: 0, accion: '', fechaInicio: '', fechaFin: '' };
|
||||
goto('/logs', { replaceState: true });
|
||||
}
|
||||
</script>
|
||||
|
||||
<Tile>
|
||||
<Disclosure summary="Filtros">
|
||||
<Form>
|
||||
<NumberInput fluid labelText="Id de Usuario" min={0} bind:value={filtros.idusu}></NumberInput>
|
||||
<Select fluid labelText="Accion del Log">
|
||||
<Form
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
aplicarFiltros();
|
||||
}}
|
||||
>
|
||||
<NumberInput fluid labelText="Id de Usuario" min={0} bind:value={filtros.usuarioId} />
|
||||
<Select fluid labelText="Accion del Log" bind:selected={filtros.accion}>
|
||||
<SelectItem text="Todas" value="" />
|
||||
<SelectItem text="Actualizar" value="ACTUALIZAR" />
|
||||
<SelectItem text="Crear" value="CREAR" />
|
||||
<SelectItem text="Eliminar" value="ELIMINAR" />
|
||||
<SelectItem text="Alta" value="ALTA" />
|
||||
</Select>
|
||||
|
||||
<DatePicker fluid datePickerType="range" on:change>
|
||||
<DatePicker
|
||||
fluid
|
||||
datePickerType="range"
|
||||
on:change={(e) => {
|
||||
const dates = e.detail;
|
||||
filtros.fechaInicio = dates.selectedDates[0] ?? '';
|
||||
filtros.fechaFin = dates.selectedDates[1] ?? '';
|
||||
}}
|
||||
>
|
||||
<DatePickerInput labelText="Fecha inicio" placeholder="mm/dd/yyyy" />
|
||||
<DatePickerInput labelText="Fecha fin" placeholder="mm/dd/yyyy" />
|
||||
</DatePicker>
|
||||
|
||||
<div class="mt-4! flex gap-2">
|
||||
<Button type="submit">Buscar</Button>
|
||||
<Button kind="secondary" onclick={limpiarFiltros}>Limpiar</Button>
|
||||
</div>
|
||||
</Form>
|
||||
</Disclosure>
|
||||
</Tile>
|
||||
@@ -10,6 +10,9 @@
|
||||
|
||||
let { logs } = $props();
|
||||
|
||||
/**
|
||||
* @param {string | number | Date} iso
|
||||
*/
|
||||
function formatFecha(iso) {
|
||||
return new Date(iso).toLocaleString('es-AR', {
|
||||
day: '2-digit',
|
||||
@@ -21,7 +24,11 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<DataTable {headers} rows={logs} expandable>
|
||||
{#if !logs}
|
||||
<p>No hay logs para mostrar</p>
|
||||
{/if}
|
||||
|
||||
<DataTable {headers} rows={logs ?? []} expandable>
|
||||
<svelte:fragment slot="cell" let:row let:cell>
|
||||
{#if cell.key === 'created_at'}
|
||||
{formatFecha(row.created_at)}
|
||||
|
||||
Reference in new issue
Block a user