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,7 +48,6 @@
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`,
{ {
@@ -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([]); setPosts([]);
pageNumber = 1; pageNumber = 1;
finished = false; finished = false;
mensajeError = ''; mensajeError = '';
fetching = false;
obtenerPosts(); 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,9 +150,7 @@
</h1> </h1>
<hr class="mb-8" /> <hr class="mb-8" />
{#if cargando && !finished} {#if mensajeError !== ''}
<CardCargando />
{:else if mensajeError !== ''}
<CardError {mensajeError} /> <CardError {mensajeError} />
{:else} {:else}
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
@@ -164,6 +165,10 @@
{#if cargando && !finished} {#if cargando && !finished}
<CardCargando /> <CardCargando />
{/if} {/if}
{#if finished && $posts.length === 0}
<p class="text-center text-muted-foreground">No hay posts para mostrar</p>
{/if}
</div> </div>
{/if} {/if}
</div> </div>

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 || []
}; };
} };