mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-19 16:07:32 -03:00
Compare commits
5 Commits
77aee661a8
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b4800d0d9 | |||
| 022623e22c | |||
| c96df5af92 | |||
|
|
d278c75688 | ||
| 3e07252c6f |
@@ -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,6 +25,7 @@
|
||||
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[];
|
||||
@@ -38,7 +34,17 @@
|
||||
|
||||
let { usuarios = $bindable(), hayMas }: Props = $props();
|
||||
|
||||
let hayMass = $state(hayMas);
|
||||
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);
|
||||
let openDarAdmin = $state(false);
|
||||
@@ -50,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) {
|
||||
@@ -92,26 +96,6 @@
|
||||
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() {
|
||||
@@ -120,11 +104,17 @@
|
||||
}
|
||||
|
||||
timeoutId = setTimeout(async () => {
|
||||
if (search === '') {
|
||||
search = '';
|
||||
const url = new URL(window.location.href);
|
||||
if (!search.trim()) {
|
||||
url.searchParams.delete('q');
|
||||
} else {
|
||||
url.searchParams.set('q', search);
|
||||
}
|
||||
replaceState(url, {});
|
||||
|
||||
let ret = await busquedaAdminUsuarios(search, ITEMS_POR_PAGINA, paginaActual);
|
||||
usuariosFiltrados = ret.usuarios;
|
||||
// invalidate('admin:load');
|
||||
hayMass = ret.hayMas;
|
||||
}, 200);
|
||||
|
||||
@@ -134,8 +124,6 @@
|
||||
}
|
||||
const ITEMS_POR_PAGINA = 5;
|
||||
|
||||
let paginaActual = $state(1);
|
||||
|
||||
// const usuariosPaginados = $derived(
|
||||
// usuariosFiltrados.slice((paginaActual - 1) * ITEMS_POR_PAGINA, paginaActual * ITEMS_POR_PAGINA)
|
||||
// );
|
||||
@@ -196,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>
|
||||
@@ -205,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>
|
||||
@@ -215,7 +212,10 @@
|
||||
<TooltipTrigger>
|
||||
<Button
|
||||
disabled={usuario.isAdmin}
|
||||
onclick={() => handleBorrar(usuario)}
|
||||
onclick={() => {
|
||||
openBorrar = true;
|
||||
usuarioBorrar = usuario;
|
||||
}}
|
||||
variant="destructive"><Trash_2 /></Button
|
||||
>
|
||||
</TooltipTrigger>
|
||||
@@ -231,7 +231,10 @@
|
||||
<Tooltip>
|
||||
<TooltipTrigger>
|
||||
<Button
|
||||
onclick={() => handleDarAdmin(usuario)}
|
||||
onclick={() => {
|
||||
openDarAdmin = true;
|
||||
usuarioDarAdmin = usuario;
|
||||
}}
|
||||
variant={usuario.isAdmin ? 'destructive' : 'default'}
|
||||
>
|
||||
<Shield />
|
||||
@@ -255,7 +258,9 @@
|
||||
<Button
|
||||
disabled={paginaActual === 1}
|
||||
onclick={() => {
|
||||
paginaActual--;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('p', String(--paginaActual));
|
||||
replaceState(url, {});
|
||||
buscarUsuarios();
|
||||
}}
|
||||
variant="secondary"
|
||||
@@ -266,7 +271,9 @@
|
||||
<Button
|
||||
disabled={!hayMass}
|
||||
onclick={() => {
|
||||
paginaActual++;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set('p', String(++paginaActual));
|
||||
replaceState(url, {});
|
||||
buscarUsuarios();
|
||||
}}
|
||||
variant="secondary">Siguiente</Button
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -2,9 +2,10 @@ import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
export async function busquedaAdminUsuarios(q: string, limit = 5, page = 1) {
|
||||
export async function busquedaAdminUsuarios(q: string, limit = 5, page = 1, fetch2?: Function) {
|
||||
try {
|
||||
const response = await fetch(
|
||||
const fetchFn = fetch2 ? fetch2 : fetch;
|
||||
const response = await fetchFn(
|
||||
get(apiBase) +
|
||||
`/api/admin/users${q ? `?q=${q}` : ''}${q ? '&' : '?'}page=${page}&pageSize=${limit}`,
|
||||
{
|
||||
|
||||
@@ -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';
|
||||
|
||||
134
src/lib/stores/cacheSeguidos.js
Normal file
134
src/lib/stores/cacheSeguidos.js
Normal file
@@ -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,7 @@
|
||||
|
||||
interface Prop {
|
||||
data: {
|
||||
usuarios?: UserResponseDto[];
|
||||
usuarios: UserResponseDto[];
|
||||
hayMas: boolean;
|
||||
error: boolean;
|
||||
};
|
||||
@@ -29,11 +28,7 @@
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{#if data.usuarios?.length === 0}
|
||||
<CardDescription>No hay usuarios que mostar</CardDescription>
|
||||
{:else}
|
||||
<TablaUsuarios usuarios={data.usuarios || []} hayMas={data.hayMas}></TablaUsuarios>
|
||||
{/if}
|
||||
<TablaUsuarios usuarios={data.usuarios} hayMas={data.hayMas}></TablaUsuarios>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
import { busquedaAdminUsuarios } from '@/hooks/busquedaAdminUsuarios.js';
|
||||
import type { PageLoad } from './$types.js';
|
||||
import { fetchUsuariosAdmin } from '@/hooks/UsuariosAdmin.js';
|
||||
|
||||
export const ssr = false;
|
||||
|
||||
export const load: PageLoad = async ({ depends }) => {
|
||||
export const load: PageLoad = async ({ depends, fetch }) => {
|
||||
depends('admin:load');
|
||||
const result = await fetchUsuariosAdmin(1, 5);
|
||||
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;
|
||||
}
|
||||
|
||||
const result = await busquedaAdminUsuarios(query, 5, page, fetch);
|
||||
|
||||
if (result.error) {
|
||||
return { error: true };
|
||||
}
|
||||
|
||||
return {
|
||||
usuarios: result.ret?.usuarios,
|
||||
hayMas: result.ret?.hayMas,
|
||||
usuarios: result.usuarios,
|
||||
hayMas: result.hayMas,
|
||||
error: false
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user