mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-23 16:34:28 -03:00
Compare commits
35 Commits
1c4140e0a1
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8683996d32 | |||
| 3b4800d0d9 | |||
| bb06dade59 | |||
| 022623e22c | |||
| 7d0e76eff4 | |||
| c96df5af92 | |||
| d278c75688 | |||
| 3e07252c6f | |||
| 77aee661a8 | |||
| ec9ec1f58a | |||
| 4ca434da9e | |||
| 398592ea1d | |||
| 4d99695a59 | |||
| 1f2f26f780 | |||
| 3dc0cfc8a4 | |||
| 22ecdb6e9d | |||
| e785b60935 | |||
| c47b9956b9 | |||
| 61232ada05 | |||
| fae9a676e2 | |||
| 9b0937b731 | |||
| 77f3901cb3 | |||
| bd09ae005b | |||
| d1a26cc132 | |||
| 670f8ae3e2 | |||
| b4382b361a | |||
| 17f7bed1d9 | |||
| 29b7effd57 | |||
| f425dd13a7 | |||
| 09ddb0800c | |||
| d60daa624c | |||
| ee5535dbc6 | |||
| 8c0da761e6 | |||
| ef16f649dd | |||
| 8decf85d3b |
@@ -11,7 +11,7 @@
|
||||
import { seguirUsuario } from '@/hooks/seguirUsuario';
|
||||
import type { Post } from '../../types';
|
||||
import CardError from './CardError.svelte';
|
||||
import { cacheSeguidos } from '@/stores/cacheSeguidos.svelte';
|
||||
import { cacheSeguidos } from '@/stores/cacheSeguidos.js';
|
||||
|
||||
let {
|
||||
post,
|
||||
@@ -41,16 +41,13 @@
|
||||
});
|
||||
|
||||
async function cargarSeguido() {
|
||||
let a = cacheSeguidos.get(post.authorId);
|
||||
if (a === undefined) {
|
||||
const seguidoStatus = await esSeguido(post as Post);
|
||||
if (seguidoStatus) {
|
||||
cacheSeguidos.set(post.authorId, seguidoStatus.isFollowing || false);
|
||||
seguido = seguidoStatus.isFollowing || false;
|
||||
seguido = await cacheSeguidos.getOrFetch(
|
||||
post.authorId,
|
||||
async () => {
|
||||
const seguidoStatus = await esSeguido(post as Post);
|
||||
return seguidoStatus?.isFollowing || false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
seguido = a;
|
||||
);
|
||||
}
|
||||
|
||||
let mensajeError: string | null = $state(null);
|
||||
|
||||
@@ -16,12 +16,7 @@
|
||||
import TooltipTrigger from './ui/tooltip/tooltip-trigger.svelte';
|
||||
import TooltipContent from './ui/tooltip/tooltip-content.svelte';
|
||||
import RecuperarContraseña from './admin/RecuperarContraseña.svelte';
|
||||
import { Dialog } from './ui/dialog';
|
||||
import DialogContent from './ui/dialog/dialog-content.svelte';
|
||||
import ModificarUsuario from './admin/ModificarUsuario.svelte';
|
||||
import { fade } from 'svelte/transition';
|
||||
import type { Unsubscriber } from 'svelte/store';
|
||||
import Input from './ui/input/input.svelte';
|
||||
import Trash_2 from '@lucide/svelte/icons/trash-2';
|
||||
import BorrarUsuario from './BorrarUsuario.svelte';
|
||||
import InputGroup from './ui/input-group/input-group.svelte';
|
||||
@@ -30,12 +25,25 @@
|
||||
import AgregarUsuario from './admin/AgregarUsuario.svelte';
|
||||
import DarAdmin from './admin/DarAdmin.svelte';
|
||||
import { busquedaAdminUsuarios } from '@/hooks/busquedaAdminUsuarios';
|
||||
import { invalidate, replaceState } from '$app/navigation';
|
||||
|
||||
interface Props {
|
||||
usuarios: UserResponseDto[];
|
||||
hayMas: boolean;
|
||||
}
|
||||
|
||||
let { usuarios = $bindable() }: Props = $props();
|
||||
let { usuarios = $bindable(), hayMas }: Props = $props();
|
||||
|
||||
let paginaActual = $derived.by(() => {
|
||||
const url = new URL(window.location.href);
|
||||
return Number(url.searchParams.get('p')) || 1;
|
||||
});
|
||||
let search = $derived.by(() => {
|
||||
const url = new URL(window.location.href);
|
||||
let ret = url.searchParams.get('q') || '';
|
||||
return ret;
|
||||
});
|
||||
let hayMass = $derived(hayMas);
|
||||
|
||||
let open = $state(false);
|
||||
let openModificarUsuario = $state(false);
|
||||
@@ -48,13 +56,11 @@
|
||||
let usuarioModificar: UserResponseDto | null = $state(null);
|
||||
let usuarioDarAdmin: UserResponseDto | null = $state(null);
|
||||
|
||||
let search = $state('');
|
||||
|
||||
type SortKey = 'username' | 'displayName' | 'postsCount' | 'createdAt';
|
||||
let sortBy = $state<SortKey | null>(null);
|
||||
let sortDirection = $state<'asc' | 'desc'>('asc');
|
||||
|
||||
let usuariosFiltrados = $state(usuarios);
|
||||
let usuariosFiltrados = $derived(usuarios);
|
||||
|
||||
function ordenarPor(campo: SortKey) {
|
||||
if (sortBy === campo) {
|
||||
@@ -90,41 +96,26 @@
|
||||
return sortDirection === 'asc' ? '↑' : '↓';
|
||||
}
|
||||
|
||||
function handleCambiarContraseña(usuario: UserResponseDto) {
|
||||
open = true;
|
||||
usuarioCambioPass = usuario;
|
||||
}
|
||||
|
||||
function handleModificar(usuario: UserResponseDto) {
|
||||
openModificarUsuario = true;
|
||||
usuarioModificar = usuario;
|
||||
}
|
||||
|
||||
function handleBorrar(usuario: UserResponseDto) {
|
||||
openBorrar = true;
|
||||
usuarioBorrar = usuario;
|
||||
}
|
||||
|
||||
function handleDarAdmin(usuario: UserResponseDto) {
|
||||
openDarAdmin = true;
|
||||
usuarioDarAdmin = usuario;
|
||||
}
|
||||
|
||||
// $inspect(usuarios);
|
||||
let timeoutId: ReturnType<typeof setTimeout> | number | undefined;
|
||||
|
||||
function buscarUsuarios() {
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
timeoutId = setTimeout(async () => {
|
||||
paginaActual = 1;
|
||||
if (search === '') {
|
||||
usuariosFiltrados = usuarios;
|
||||
return;
|
||||
const url = new URL(window.location.href);
|
||||
if (!search.trim()) {
|
||||
url.searchParams.delete('q');
|
||||
} else {
|
||||
url.searchParams.set('q', search);
|
||||
}
|
||||
usuariosFiltrados = await busquedaAdminUsuarios(search);
|
||||
replaceState(url, {});
|
||||
|
||||
let ret = await busquedaAdminUsuarios(search, ITEMS_POR_PAGINA, paginaActual);
|
||||
usuariosFiltrados = ret.usuarios;
|
||||
// invalidate('admin:load');
|
||||
hayMass = ret.hayMas;
|
||||
}, 200);
|
||||
|
||||
return () => {
|
||||
@@ -133,13 +124,9 @@
|
||||
}
|
||||
const ITEMS_POR_PAGINA = 5;
|
||||
|
||||
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>
|
||||
|
||||
<div class="mb-4 flex gap-2">
|
||||
@@ -184,7 +171,7 @@
|
||||
<p class="text-center">No hay usuarios por el nombre de: {search}</p>
|
||||
</TableCell>
|
||||
</TableRow>{:else}
|
||||
{#each usuariosPaginados as usuario}
|
||||
{#each usuariosFiltrados as usuario}
|
||||
<TableRow>
|
||||
<TableCell
|
||||
>@<a href={'/' + usuario.username}>
|
||||
@@ -197,7 +184,11 @@
|
||||
<TableCell class="flex gap-2">
|
||||
<Tooltip>
|
||||
<TooltipTrigger>
|
||||
<Button onclick={() => handleCambiarContraseña(usuario)}><KeyIcon></KeyIcon></Button
|
||||
<Button
|
||||
onclick={() => {
|
||||
open = true;
|
||||
usuarioCambioPass = usuario;
|
||||
}}><KeyIcon></KeyIcon></Button
|
||||
>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
@@ -206,7 +197,12 @@
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger>
|
||||
<Button onclick={() => handleModificar(usuario)}><UserPen /></Button>
|
||||
<Button
|
||||
onclick={() => {
|
||||
openModificarUsuario = true;
|
||||
usuarioModificar = usuario;
|
||||
}}><UserPen /></Button
|
||||
>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Modificar Usuario</p>
|
||||
@@ -216,7 +212,10 @@
|
||||
<TooltipTrigger>
|
||||
<Button
|
||||
disabled={usuario.isAdmin}
|
||||
onclick={() => handleBorrar(usuario)}
|
||||
onclick={() => {
|
||||
openBorrar = true;
|
||||
usuarioBorrar = usuario;
|
||||
}}
|
||||
variant="destructive"><Trash_2 /></Button
|
||||
>
|
||||
</TooltipTrigger>
|
||||
@@ -232,7 +231,10 @@
|
||||
<Tooltip>
|
||||
<TooltipTrigger>
|
||||
<Button
|
||||
onclick={() => handleDarAdmin(usuario)}
|
||||
onclick={() => {
|
||||
openDarAdmin = true;
|
||||
usuarioDarAdmin = usuario;
|
||||
}}
|
||||
variant={usuario.isAdmin ? 'destructive' : 'default'}
|
||||
>
|
||||
<Shield />
|
||||
@@ -253,23 +255,29 @@
|
||||
</TableBody>
|
||||
</Table>
|
||||
<div class="mt-4 flex items-center justify-between">
|
||||
<p class="text-sm text-muted-foreground">
|
||||
Página {paginaActual} de {totalPaginas}
|
||||
</p>
|
||||
<Button
|
||||
disabled={paginaActual === 1}
|
||||
onclick={() => {
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('p', String(--paginaActual));
|
||||
replaceState(url, {});
|
||||
buscarUsuarios();
|
||||
}}
|
||||
variant="secondary"
|
||||
>
|
||||
Anterior
|
||||
</Button>
|
||||
|
||||
<div class="flex gap-2">
|
||||
<Button disabled={paginaActual === 1} onclick={() => paginaActual--} variant="secondary">
|
||||
Anterior
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
disabled={paginaActual === totalPaginas || totalPaginas === 0}
|
||||
onclick={() => paginaActual++}
|
||||
variant="secondary"
|
||||
>
|
||||
Siguiente
|
||||
</Button>
|
||||
</div>
|
||||
<Button
|
||||
disabled={!hayMass}
|
||||
onclick={() => {
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('p', String(++paginaActual));
|
||||
replaceState(url, {});
|
||||
buscarUsuarios();
|
||||
}}
|
||||
variant="secondary">Siguiente</Button
|
||||
>
|
||||
</div>
|
||||
<BorrarUsuario bind:open={openBorrar} usuario={usuarioBorrar} />
|
||||
<RecuperarContraseña bind:open usuario={usuarioCambioPass} />
|
||||
|
||||
@@ -30,13 +30,7 @@
|
||||
}}
|
||||
>
|
||||
<DialogContent>
|
||||
<div
|
||||
class={esExitoso
|
||||
? 'rounded border border-green-400 bg-green-100/10 px-4 py-3 text-green-700'
|
||||
: 'rounded border border-red-400 bg-red-100/10 px-4 py-3 text-red-700'}
|
||||
>
|
||||
{mensajeResultado}
|
||||
</div>
|
||||
{mensajeResultado}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
{/if}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
import Label from '../ui/label/label.svelte';
|
||||
import Spinner from '../ui/spinner/spinner.svelte';
|
||||
import { updateUsuario } from '@/hooks/updateUsuario';
|
||||
import { invalidate } from '$app/navigation';
|
||||
|
||||
interface Prop {
|
||||
open: boolean;
|
||||
@@ -38,6 +39,7 @@
|
||||
error = ret;
|
||||
} else {
|
||||
usuario!.displayName = ret.displayName;
|
||||
invalidate('admin:load');
|
||||
open = false;
|
||||
}
|
||||
cargando = false;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { get } from 'svelte/store';
|
||||
import type { UserResponseDto } from '../../types';
|
||||
|
||||
export async function fetchUsuariosAdmin(page: number, limit: number) {
|
||||
let response = await fetch(get(apiBase) + `/api/admin/users?page=${page}&pageSize=${limit}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
throw redirect(302, '/');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return { error: true };
|
||||
}
|
||||
const ret: { usuarios: UserResponseDto[]; hayMas: boolean } = await response.json();
|
||||
return { ret, error: false };
|
||||
}
|
||||
@@ -2,15 +2,20 @@ import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
export async function busquedaAdminUsuarios(q: string) {
|
||||
export async function busquedaAdminUsuarios(q: string, limit = 5, page = 1, fetch2?: Function) {
|
||||
try {
|
||||
const response = await fetch(get(apiBase) + '/api/admin/users?q=' + q, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
const fetchFn = fetch2 ? fetch2 : fetch;
|
||||
const response = await fetchFn(
|
||||
get(apiBase) +
|
||||
`/api/admin/users${q ? `?q=${q}` : ''}${q ? '&' : '?'}page=${page}&pageSize=${limit}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import { cacheSeguidos } from '@/stores/cacheSeguidos.svelte';
|
||||
import { cacheSeguidos } from '@/stores/cacheSeguidos';
|
||||
import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
class FollowCache {
|
||||
constructor() {
|
||||
if (browser) {
|
||||
this.loadFromStorage();
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Map<string, boolean | Promise<boolean>>} */
|
||||
#cache = new Map();
|
||||
|
||||
/** @type {import('svelte/store').Writable<Map<string, boolean>>} */
|
||||
store = writable(new Map());
|
||||
|
||||
/** @param {string} userId */
|
||||
get(userId) {
|
||||
const value = this.#cache.get(userId);
|
||||
return value instanceof Promise ? undefined : value;
|
||||
}
|
||||
|
||||
/** @param {string} userId */
|
||||
has(userId) {
|
||||
return this.#cache.has(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @param {() => Promise<boolean>} fetchFn
|
||||
*/
|
||||
async getOrFetch(userId, fetchFn) {
|
||||
const existing = this.#cache.get(userId);
|
||||
|
||||
if (existing !== undefined) {
|
||||
if (existing instanceof Promise) {
|
||||
return existing;
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
const promise = fetchFn()
|
||||
.then((result) => {
|
||||
this.#setFinal(userId, result);
|
||||
return result;
|
||||
})
|
||||
.catch((err) => {
|
||||
this.#cache.delete(userId);
|
||||
this.#updateStore();
|
||||
throw err;
|
||||
});
|
||||
|
||||
this.#cache.set(userId, promise);
|
||||
this.#updateStore();
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @param {boolean} isFollowed
|
||||
*/
|
||||
set(userId, isFollowed) {
|
||||
this.#setFinal(userId, isFollowed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @param {boolean} value
|
||||
*/
|
||||
#setFinal(userId, value) {
|
||||
this.#cache.set(userId, value);
|
||||
this.#updateStore();
|
||||
this.saveToStorage();
|
||||
|
||||
if (browser) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('followCacheUpdated', {
|
||||
detail: { userId, isFollowed: value }
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#updateStore() {
|
||||
const filtered = Array.from(this.#cache.entries())
|
||||
.filter(([_, v]) => typeof v === 'boolean');
|
||||
|
||||
this.store.set(
|
||||
/** @type {Map<string, boolean>} */
|
||||
(new Map(filtered))
|
||||
);
|
||||
}
|
||||
|
||||
/** @param {string} userId */
|
||||
delete(userId) {
|
||||
this.#cache.delete(userId);
|
||||
this.#updateStore();
|
||||
this.saveToStorage();
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.#cache.clear();
|
||||
this.store.set(new Map());
|
||||
this.saveToStorage();
|
||||
}
|
||||
|
||||
saveToStorage() {
|
||||
if (!browser) return;
|
||||
const filtered = Array.from(this.#cache.entries())
|
||||
.filter(([_, v]) => typeof v === 'boolean');
|
||||
|
||||
const data = Object.fromEntries(filtered);
|
||||
sessionStorage.setItem('follow-cache', JSON.stringify(data));
|
||||
}
|
||||
|
||||
loadFromStorage() {
|
||||
if (!browser) return;
|
||||
|
||||
try {
|
||||
const stored = sessionStorage.getItem('follow-cache');
|
||||
if (!stored) return;
|
||||
const data = JSON.parse(stored);
|
||||
|
||||
this.#cache = new Map(Object.entries(data));
|
||||
this.#updateStore();
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error cargando follow-cache:', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const cacheSeguidos = new FollowCache();
|
||||
@@ -1,105 +0,0 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
class FollowCache {
|
||||
constructor() {
|
||||
if (browser) {
|
||||
this.loadFromStorage();
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Map<string, boolean>} */
|
||||
#cache = new Map();
|
||||
|
||||
/** @type {import('svelte/store').Writable<Map<string, boolean>>} */
|
||||
store = writable(this.#cache);
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @returns {boolean | undefined}
|
||||
*/
|
||||
get(userId) {
|
||||
return this.#cache.get(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @param {boolean} isFollowed
|
||||
*/
|
||||
set(userId, isFollowed) {
|
||||
this.#cache.set(userId, isFollowed);
|
||||
this.store.set(this.#cache);
|
||||
this.saveToStorage();
|
||||
|
||||
if (browser) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('followCacheUpdated', {
|
||||
detail: { userId, isFollowed }
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
* @returns {boolean}
|
||||
*/
|
||||
has(userId) {
|
||||
return this.#cache.has(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} userId
|
||||
*/
|
||||
delete(userId) {
|
||||
this.#cache.delete(userId);
|
||||
this.store.set(this.#cache);
|
||||
this.saveToStorage();
|
||||
|
||||
if (browser) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('followCacheUpdated', {
|
||||
detail: { userId, isFollowed: false }
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.#cache.clear();
|
||||
this.store.set(this.#cache);
|
||||
this.saveToStorage();
|
||||
|
||||
if (browser) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('followCacheUpdated', {
|
||||
detail: { clearAll: true }
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
saveToStorage() {
|
||||
if (browser) {
|
||||
const data = Object.fromEntries(this.#cache);
|
||||
sessionStorage.setItem('follow-cache', JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
loadFromStorage() {
|
||||
if (browser) {
|
||||
try {
|
||||
const stored = sessionStorage.getItem('follow-cache');
|
||||
if (stored) {
|
||||
const data = JSON.parse(stored);
|
||||
this.#cache = new Map(Object.entries(data));
|
||||
this.store.set(this.#cache);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error cargando desde sesion:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const cacheSeguidos = new FollowCache();
|
||||
@@ -1,7 +1,6 @@
|
||||
<script lang="ts">
|
||||
import CardContent from '@/components/ui/card/card-content.svelte';
|
||||
import Card from '@/components/ui/card/card.svelte';
|
||||
import CardDescription from '@/components/ui/card/card-description.svelte';
|
||||
import TablaUsuarios from '@/components/TablaUsuarios.svelte';
|
||||
import CardTitle from '@/components/ui/card/card-title.svelte';
|
||||
import CardHeader from '@/components/ui/card/card-header.svelte';
|
||||
@@ -9,7 +8,8 @@
|
||||
|
||||
interface Prop {
|
||||
data: {
|
||||
usuarios?: UserResponseDto[];
|
||||
usuarios: UserResponseDto[];
|
||||
hayMas: boolean;
|
||||
error: boolean;
|
||||
};
|
||||
}
|
||||
@@ -28,11 +28,7 @@
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{#if data.usuarios?.length === 0}
|
||||
<CardDescription>No hay usuarios que mostar</CardDescription>
|
||||
{:else}
|
||||
<TablaUsuarios usuarios={data.usuarios || []}></TablaUsuarios>
|
||||
{/if}
|
||||
<TablaUsuarios usuarios={data.usuarios} hayMas={data.hayMas}></TablaUsuarios>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
@@ -1,28 +1,27 @@
|
||||
import { apiBase } from '@/stores/url.js';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { get } from 'svelte/store';
|
||||
import type { UserResponseDto } from '../../../types.js';
|
||||
import { busquedaAdminUsuarios } from '@/hooks/busquedaAdminUsuarios.js';
|
||||
import type { PageLoad } from './$types.js';
|
||||
import { fetchUsuariosAdmin } from '@/hooks/UsuariosAdmin.js';
|
||||
|
||||
export const ssr = false;
|
||||
|
||||
export async function load({ depends, fetch }) {
|
||||
export const load: PageLoad = async ({ depends, fetch }) => {
|
||||
depends('admin:load');
|
||||
const response = await fetch(get(apiBase) + '/api/admin/users', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
if (response.status === 401) {
|
||||
throw redirect(302, '/');
|
||||
let url = new URL(location.href);
|
||||
let query = url.searchParams.get('q') ?? '';
|
||||
let page = Number(url.searchParams.get('p'));
|
||||
if (isNaN(page) || page < 1) {
|
||||
page = 1;
|
||||
}
|
||||
if (!response.ok) {
|
||||
|
||||
const result = await busquedaAdminUsuarios(query, 5, page, fetch);
|
||||
|
||||
if (result.error) {
|
||||
return { error: true };
|
||||
}
|
||||
|
||||
const usuarios: UserResponseDto[] = await response.json();
|
||||
|
||||
return { usuarios, error: false };
|
||||
}
|
||||
return {
|
||||
usuarios: result.usuarios,
|
||||
hayMas: result.hayMas,
|
||||
error: false
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user