fix pagination in profile

This commit is contained in:
Fran
2026-01-24 01:43:34 -03:00
parent 1c79c5fcda
commit 9eb92b0c06
3 changed files with 47 additions and 40 deletions

View File

@@ -38,6 +38,8 @@
$inspect(data); $inspect(data);
let fetching = false; let fetching = false;
// svelte-ignore state_referenced_locally
let currentProfile = $state(params.perfil);
async function obtenerPosts() { async function obtenerPosts() {
if (fetching || finished) return; if (fetching || finished) return;
@@ -46,15 +48,14 @@
cargando = true; cargando = true;
try { try {
const res = await fetch( const res = await fetch(
`${$apiBase}/api/posts/user/${params.perfil}?page=${pageNumber}&pageSize=20`, `${$apiBase}/api/posts/user/${params.perfil}?page=${pageNumber}&pageSize=20`,
{ {
headers: { headers: {
Authorization: `Bearer ${$sesionStore?.accessToken}` Authorization: `Bearer ${$sesionStore?.accessToken}`
}
} }
); }
);
const nuevosPosts: Post[] = await res.json(); const nuevosPosts: Post[] = await res.json();
if (nuevosPosts.length === 0) { if (nuevosPosts.length === 0) {
@@ -69,6 +70,8 @@
if (nuevosPosts.length < 20) { if (nuevosPosts.length < 20) {
finished = true; finished = true;
} }
} catch (error) {
mensajeError = 'Error al cargar los posts';
} finally { } finally {
fetching = false; fetching = false;
cargando = false; cargando = false;
@@ -76,24 +79,24 @@
} }
$effect(() => { $effect(() => {
params.perfil; if (currentProfile !== params.perfil) {
currentProfile = params.perfil;
setPosts([]);
pageNumber = 1;
finished = false;
mensajeError = '';
fetching = false;
setPosts([]); obtenerPosts();
pageNumber = 1; }
finished = false;
mensajeError = '';
obtenerPosts();
}); });
$effect(() => { $effect(() => {
if (!sentinel) return; if (!sentinel || finished) return;
if (finished) return;
const observer = new IntersectionObserver( const observer = new IntersectionObserver(
([entry]) => { ([entry]) => {
if (entry.isIntersecting && !fetching) { if (entry.isIntersecting && !fetching && !finished) {
obtenerPosts(); obtenerPosts();
} }
}, },
@@ -147,25 +150,27 @@
</h1> </h1>
<hr class="mb-8" /> <hr class="mb-8" />
{#if cargando && !finished} {#if mensajeError !== ''}
<CardCargando /> <CardError {mensajeError} />
{:else if mensajeError !== ''} {:else}
<CardError {mensajeError} /> <div class="flex flex-col gap-2">
{:else} {#each $posts as post (post.id)}
<div class="flex flex-col gap-2"> <div transition:slide>
{#each $posts as post (post.id)} <PostCard {post} bind:postAModificar />
<div transition:slide> </div>
<PostCard {post} bind:postAModificar /> {/each}
</div>
{/each}
<div bind:this={sentinel} class="h-1"></div> <div bind:this={sentinel} class="h-1"></div>
{#if cargando && !finished} {#if cargando && !finished}
<CardCargando /> <CardCargando />
{/if} {/if}
</div>
{/if} {#if finished && $posts.length === 0}
<p class="text-center text-muted-foreground">No hay posts para mostrar</p>
{/if}
</div>
{/if}
</div> </div>
</div> </div>
{#if postAModificar} {#if postAModificar}

View File

@@ -1,12 +1,13 @@
import { obtenerUsuarioPorUsername } from '@/hooks/obtenerUsuario.js'; import { obtenerUsuarioPorUsername } from '@/hooks/obtenerUsuario.js';
import type { User, UserResponseDto } from '../../types.js'; import type {UserResponseDto } from '../../types.js';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import { obtenerSeguidosPorUsuario } from '@/hooks/obtenerSeguidosPorUsuario.js'; import { obtenerSeguidosPorUsuario } from '@/hooks/obtenerSeguidosPorUsuario.js';
import { obtenerSeguidoresPorUsuario } from '@/hooks/obtenerSeguidoresPorUsuario.js'; import { obtenerSeguidoresPorUsuario } from '@/hooks/obtenerSeguidoresPorUsuario.js';
import { obtenerCantidadDeSeguidores } from '@/hooks/obtenerCantidadDeSeguidores.js'; import { obtenerCantidadDeSeguidores } from '@/hooks/obtenerCantidadDeSeguidores.js';
import { obtenerCantidadDeSeguidos } from '@/hooks/obtenerCantidadDeSeguidos.js'; import { obtenerCantidadDeSeguidos } from '@/hooks/obtenerCantidadDeSeguidos.js';
export async function load({ params, depends, fetch }) { export const load: PageLoad = async ({ params, depends, fetch }) => {
depends('perfil:general'); depends('perfil:general');
const usuario: UserResponseDto | null = await obtenerUsuarioPorUsername(params.perfil, fetch); const usuario: UserResponseDto | null = await obtenerUsuarioPorUsername(params.perfil, fetch);
if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil); if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil);
@@ -25,4 +26,4 @@ export async function load({ params, depends, fetch }) {
countSeguidores: countSeguidores.count, countSeguidores: countSeguidores.count,
countSeguidos: countSeguidos.count countSeguidos: countSeguidos.count
}; };
} };

View File

@@ -1,9 +1,10 @@
import { obtenerUsuarioPorUsername } from '@/hooks/obtenerUsuario'; import { obtenerUsuarioPorUsername } from '@/hooks/obtenerUsuario';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import type { UserResponseDto, UsersResponseDto } from '../../../types'; import type { UserResponseDto, UsersResponseDto } from '../../../types';
import { obtenerSeguidosPorUsuario } from '@/hooks/obtenerSeguidosPorUsuario'; import { obtenerSeguidosPorUsuario } from '@/hooks/obtenerSeguidosPorUsuario';
export async function load({ params, fetch }) { export const load: PageLoad = async ({ params, fetch }) => {
const usuario: UserResponseDto | null = await obtenerUsuarioPorUsername(params.perfil, fetch); const usuario: UserResponseDto | null = await obtenerUsuarioPorUsername(params.perfil, fetch);
if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil); if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil);
@@ -17,4 +18,4 @@ export async function load({ params, fetch }) {
usuario, usuario,
seguidos: seguidosResponse?.response || [] seguidos: seguidosResponse?.response || []
}; };
} };