mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
fix: añadida paginacion
This commit is contained in:
@@ -33,10 +33,12 @@
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
usuarios: UserResponseDto[];
|
usuarios: UserResponseDto[];
|
||||||
|
hayMas: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { usuarios = $bindable() }: Props = $props();
|
let { usuarios = $bindable(), hayMas }: Props = $props();
|
||||||
|
|
||||||
|
let hayMass = $state(hayMas);
|
||||||
let open = $state(false);
|
let open = $state(false);
|
||||||
let openModificarUsuario = $state(false);
|
let openModificarUsuario = $state(false);
|
||||||
let openDarAdmin = $state(false);
|
let openDarAdmin = $state(false);
|
||||||
@@ -112,19 +114,18 @@
|
|||||||
|
|
||||||
// $inspect(usuarios);
|
// $inspect(usuarios);
|
||||||
let timeoutId: ReturnType<typeof setTimeout> | number | undefined;
|
let timeoutId: ReturnType<typeof setTimeout> | number | undefined;
|
||||||
|
|
||||||
function buscarUsuarios() {
|
function buscarUsuarios() {
|
||||||
if (timeoutId) {
|
if (timeoutId) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
|
|
||||||
timeoutId = setTimeout(async () => {
|
timeoutId = setTimeout(async () => {
|
||||||
paginaActual = 1;
|
|
||||||
if (search === '') {
|
if (search === '') {
|
||||||
usuariosFiltrados = usuarios;
|
search = '';
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
usuariosFiltrados = await busquedaAdminUsuarios(search);
|
let ret = await busquedaAdminUsuarios(search, ITEMS_POR_PAGINA, paginaActual);
|
||||||
|
usuariosFiltrados = ret.usuarios;
|
||||||
|
hayMass = ret.hayMas;
|
||||||
}, 200);
|
}, 200);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -135,11 +136,9 @@
|
|||||||
|
|
||||||
let paginaActual = $state(1);
|
let paginaActual = $state(1);
|
||||||
|
|
||||||
const totalPaginas = $derived(Math.ceil(usuariosFiltrados.length / ITEMS_POR_PAGINA));
|
// const usuariosPaginados = $derived(
|
||||||
|
// usuariosFiltrados.slice((paginaActual - 1) * ITEMS_POR_PAGINA, paginaActual * ITEMS_POR_PAGINA)
|
||||||
const usuariosPaginados = $derived(
|
// );
|
||||||
usuariosFiltrados.slice((paginaActual - 1) * ITEMS_POR_PAGINA, paginaActual * ITEMS_POR_PAGINA)
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="mb-4 flex gap-2">
|
<div class="mb-4 flex gap-2">
|
||||||
@@ -184,7 +183,7 @@
|
|||||||
<p class="text-center">No hay usuarios por el nombre de: {search}</p>
|
<p class="text-center">No hay usuarios por el nombre de: {search}</p>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>{:else}
|
</TableRow>{:else}
|
||||||
{#each usuariosPaginados as usuario}
|
{#each usuariosFiltrados as usuario}
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell
|
<TableCell
|
||||||
>@<a href={'/' + usuario.username}>
|
>@<a href={'/' + usuario.username}>
|
||||||
@@ -253,23 +252,25 @@
|
|||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
<div class="mt-4 flex items-center justify-between">
|
<div class="mt-4 flex items-center justify-between">
|
||||||
<p class="text-sm text-muted-foreground">
|
<Button
|
||||||
Página {paginaActual} de {totalPaginas}
|
disabled={paginaActual === 1}
|
||||||
</p>
|
onclick={() => {
|
||||||
|
paginaActual--;
|
||||||
|
buscarUsuarios();
|
||||||
|
}}
|
||||||
|
variant="secondary"
|
||||||
|
>
|
||||||
|
Anterior
|
||||||
|
</Button>
|
||||||
|
|
||||||
<div class="flex gap-2">
|
<Button
|
||||||
<Button disabled={paginaActual === 1} onclick={() => paginaActual--} variant="secondary">
|
disabled={!hayMass}
|
||||||
Anterior
|
onclick={() => {
|
||||||
</Button>
|
paginaActual++;
|
||||||
|
buscarUsuarios();
|
||||||
<Button
|
}}
|
||||||
disabled={paginaActual === totalPaginas || totalPaginas === 0}
|
variant="secondary">Siguiente</Button
|
||||||
onclick={() => paginaActual++}
|
>
|
||||||
variant="secondary"
|
|
||||||
>
|
|
||||||
Siguiente
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<BorrarUsuario bind:open={openBorrar} usuario={usuarioBorrar} />
|
<BorrarUsuario bind:open={openBorrar} usuario={usuarioBorrar} />
|
||||||
<RecuperarContraseña bind:open usuario={usuarioCambioPass} />
|
<RecuperarContraseña bind:open usuario={usuarioCambioPass} />
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
import { apiBase } from "@/stores/url";
|
import { apiBase } from '@/stores/url';
|
||||||
import { sesionStore } from "@/stores/usuario";
|
import { sesionStore } from '@/stores/usuario';
|
||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from '@sveltejs/kit';
|
||||||
import { get } from "svelte/store";
|
import { get } from 'svelte/store';
|
||||||
import type { UserResponseDto } from "../../types";
|
import type { UserResponseDto } from '../../types';
|
||||||
|
|
||||||
export async function fetchUsuariosAdmin(page: number, limit: number) {
|
export async function fetchUsuariosAdmin(page: number, limit: number) {
|
||||||
let response = await fetch(get(apiBase) + `/api/admin/users?page=${page}&pageSize=${limit}`,{
|
let response = await fetch(get(apiBase) + `/api/admin/users?page=${page}&pageSize=${limit}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
throw redirect(302, '/');
|
throw redirect(302, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return { error: true };
|
return { error: true };
|
||||||
}
|
}
|
||||||
const usuarios: UserResponseDto[] = await response.json();
|
const ret: { usuarios: UserResponseDto[]; hayMas: boolean } = await response.json();
|
||||||
return { usuarios, error: false };
|
return { ret, error: false };
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,19 @@ import { apiBase } from '@/stores/url';
|
|||||||
import { sesionStore } from '@/stores/usuario';
|
import { sesionStore } from '@/stores/usuario';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
export async function busquedaAdminUsuarios(q: string) {
|
export async function busquedaAdminUsuarios(q: string, limit = 5, page = 1) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(get(apiBase) + '/api/admin/users?q=' + q, {
|
const response = await fetch(
|
||||||
method: 'GET',
|
get(apiBase) +
|
||||||
headers: {
|
`/api/admin/users${q ? `?q=${q}` : ''}${q ? '&' : '?'}page=${page}&pageSize=${limit}`,
|
||||||
'Content-Type': 'application/json',
|
{
|
||||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
interface Prop {
|
interface Prop {
|
||||||
data: {
|
data: {
|
||||||
usuarios?: UserResponseDto[];
|
usuarios?: UserResponseDto[];
|
||||||
|
hayMas: boolean;
|
||||||
error: boolean;
|
error: boolean;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -31,7 +32,7 @@
|
|||||||
{#if data.usuarios?.length === 0}
|
{#if data.usuarios?.length === 0}
|
||||||
<CardDescription>No hay usuarios que mostar</CardDescription>
|
<CardDescription>No hay usuarios que mostar</CardDescription>
|
||||||
{:else}
|
{:else}
|
||||||
<TablaUsuarios usuarios={data.usuarios || []}></TablaUsuarios>
|
<TablaUsuarios usuarios={data.usuarios || []} hayMas={data.hayMas}></TablaUsuarios>
|
||||||
{/if}
|
{/if}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@@ -3,16 +3,17 @@ import { fetchUsuariosAdmin } from '@/hooks/UsuariosAdmin.js';
|
|||||||
|
|
||||||
export const ssr = false;
|
export const ssr = false;
|
||||||
|
|
||||||
export const load: PageLoad = async ({ depends}) => {
|
export const load: PageLoad = async ({ depends }) => {
|
||||||
depends('admin:load');
|
depends('admin:load');
|
||||||
const result = await fetchUsuariosAdmin(1, 10);
|
const result = await fetchUsuariosAdmin(1, 5);
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
return { error: true };
|
return { error: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
usuarios: result.usuarios,
|
usuarios: result.ret?.usuarios,
|
||||||
|
hayMas: result.ret?.hayMas,
|
||||||
error: false
|
error: false
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user