mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
add pagination in following and followers
This commit is contained in:
@@ -35,8 +35,5 @@
|
|||||||
<BotonSeguir post={{ authorId: usu.id }} />
|
<BotonSeguir post={{ authorId: usu.id }} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{#if usu.bio}
|
|
||||||
<div class="mt-4 rounded-full bg-accent p-4 text-muted-foreground">{usu.bio}</div>
|
|
||||||
{/if}
|
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
import { sesionStore } from '@/stores/usuario';
|
import { sesionStore } from '@/stores/usuario';
|
||||||
import type { UsersResponseDto } from '../../types';
|
import type { UsersResponseDto } from '../../types';
|
||||||
import { get } from 'svelte/store';
|
|
||||||
import { apiBase } from '@/stores/url';
|
import { apiBase } from '@/stores/url';
|
||||||
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
export async function obtenerSeguidoresPorUsuario(
|
export async function obtenerSeguidoresPorUsuario(
|
||||||
id: string,
|
id: string,
|
||||||
|
page: number = 1,
|
||||||
limit: number = 20,
|
limit: number = 20,
|
||||||
fetch2: Function
|
fetch2?: Function
|
||||||
): Promise<UsersResponseDto | null> {
|
): Promise<UsersResponseDto | null> {
|
||||||
try {
|
try {
|
||||||
const fetchFunc = fetch2 || fetch;
|
const fetchFunc = fetch2 || fetch;
|
||||||
const response = await fetchFunc(`${get(apiBase)}/api/users/${id}/followers?limit=${limit}`, {
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
const response = await fetchFunc(`${get(apiBase)}/api/users/${id}/followers?skip=${skip}&limit=${limit}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ import { get } from 'svelte/store';
|
|||||||
|
|
||||||
export async function obtenerSeguidosPorUsuario(
|
export async function obtenerSeguidosPorUsuario(
|
||||||
id: string,
|
id: string,
|
||||||
|
page: number = 1,
|
||||||
limit: number = 20,
|
limit: number = 20,
|
||||||
fetch2?: Function
|
fetch2?: Function
|
||||||
): Promise<UsersResponseDto | null> {
|
): Promise<UsersResponseDto | null> {
|
||||||
try {
|
try {
|
||||||
const fetchFunc = fetch2 || fetch;
|
const fetchFunc = fetch2 || fetch;
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
const response = await fetchFunc(`${get(apiBase)}/api/users/${id}/following?limit=${limit}`, {
|
const response = await fetchFunc(`${get(apiBase)}/api/users/${id}/following?skip=${skip}&limit=${limit}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -23,8 +25,8 @@ export async function obtenerSeguidosPorUsuario(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const users: UsersResponseDto = await response.json();
|
const data: UsersResponseDto = await response.json();
|
||||||
return users;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ export const load: PageLoad = async ({ params, depends, fetch }) => {
|
|||||||
if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil);
|
if (!usuario) error(404, 'No se encontro el usuario, ' + params.perfil);
|
||||||
|
|
||||||
const [seguidos, seguidores, countSeguidores, countSeguidos] = await Promise.all([
|
const [seguidos, seguidores, countSeguidores, countSeguidos] = await Promise.all([
|
||||||
obtenerSeguidosPorUsuario(usuario.id, 5, fetch),
|
obtenerSeguidosPorUsuario(usuario.id, 1, 5, fetch),
|
||||||
obtenerSeguidoresPorUsuario(usuario.id, 5, fetch),
|
obtenerSeguidoresPorUsuario(usuario.id, 1, 5, fetch),
|
||||||
obtenerCantidadDeSeguidores(usuario.id, fetch),
|
obtenerCantidadDeSeguidores(usuario.id, fetch),
|
||||||
obtenerCantidadDeSeguidos(usuario.id, fetch)
|
obtenerCantidadDeSeguidos(usuario.id, fetch)
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -1,15 +1,41 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import ArrowLeft from '@lucide/svelte/icons/chevron-left';
|
import ArrowLeft from '@lucide/svelte/icons/chevron-left';
|
||||||
|
import ChevronLeft from '@lucide/svelte/icons/chevron-left';
|
||||||
|
import ChevronRight from '@lucide/svelte/icons/chevron-right';
|
||||||
import type { UserResponseDto } from '../../../types';
|
import type { UserResponseDto } from '../../../types';
|
||||||
import UserCard from '@/components/UserCard.svelte';
|
import UserCard from '@/components/UserCard.svelte';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
|
import { obtenerSeguidoresPorUsuario } from '@/hooks/obtenerSeguidoresPorUsuario';
|
||||||
|
|
||||||
type Data = {
|
type Data = {
|
||||||
usuario: UserResponseDto;
|
usuario: UserResponseDto;
|
||||||
seguidores: UserResponseDto[];
|
seguidores: UserResponseDto[];
|
||||||
|
totalCount: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
let { data }: { data: Data } = $props();
|
let { data }: { data: Data } = $props();
|
||||||
|
|
||||||
|
let currentPage = $state(1);
|
||||||
|
let isLoading = $state(false);
|
||||||
|
const limit = 100;
|
||||||
|
|
||||||
|
let totalPages = $derived(Math.ceil(data.totalCount / limit));
|
||||||
|
|
||||||
|
async function loadPage(page: number) {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
isLoading = true;
|
||||||
|
const response = await obtenerSeguidoresPorUsuario(data.usuario.id, page, limit);
|
||||||
|
|
||||||
|
if (response) {
|
||||||
|
data.seguidores = response.response as UserResponseDto[];
|
||||||
|
data.totalCount = response.totalCount;
|
||||||
|
currentPage = page;
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoading = false;
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex min-h-fit w-full items-center justify-center p-6 md:p-10">
|
<div class="flex min-h-fit w-full items-center justify-center p-6 md:p-10">
|
||||||
@@ -25,9 +51,14 @@
|
|||||||
<ArrowLeft />
|
<ArrowLeft />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{#if data.seguidores.length === 0}
|
|
||||||
|
{#if isLoading}
|
||||||
<div class="py-8 text-center text-muted-foreground">
|
<div class="py-8 text-center text-muted-foreground">
|
||||||
<p>No hay seguidores para mostrar.</p>
|
<p>Cargando...</p>
|
||||||
|
</div>
|
||||||
|
{:else if data.seguidores.length === 0}
|
||||||
|
<div class="py-8 text-center text-muted-foreground">
|
||||||
|
<p>No hay seguidos para mostrar.</p>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<div class="flex flex-col sm:grid" style="grid-template-columns: repeat(2, 1fr); gap: 1rem;">
|
<div class="flex flex-col sm:grid" style="grid-template-columns: repeat(2, 1fr); gap: 1rem;">
|
||||||
@@ -38,5 +69,29 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if totalPages > 1}
|
||||||
|
<div class="mt-6 flex items-center justify-center gap-2">
|
||||||
|
<button
|
||||||
|
class="rounded-md border bg-card p-2 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
onclick={() => loadPage(currentPage - 1)}
|
||||||
|
disabled={currentPage === 1 || isLoading}
|
||||||
|
>
|
||||||
|
<ChevronLeft class="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<span class="px-4 text-sm text-muted-foreground">
|
||||||
|
Página {currentPage} de {totalPages}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="rounded-md border bg-card p-2 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
onclick={() => loadPage(currentPage + 1)}
|
||||||
|
disabled={currentPage === totalPages || isLoading}
|
||||||
|
>
|
||||||
|
<ChevronRight class="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
import { obtenerSeguidoresPorUsuario } from '@/hooks/obtenerSeguidoresPorUsuario';
|
import { obtenerSeguidoresPorUsuario } from '@/hooks/obtenerSeguidoresPorUsuario';
|
||||||
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';
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
const seguidoresResponse: UsersResponseDto | null = await obtenerSeguidoresPorUsuario(
|
const seguidoresResponse: UsersResponseDto | null = await obtenerSeguidoresPorUsuario(
|
||||||
usuario.id,
|
usuario.id,
|
||||||
|
1,
|
||||||
100,
|
100,
|
||||||
fetch
|
fetch
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,15 +1,41 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import ArrowLeft from '@lucide/svelte/icons/chevron-left';
|
import ArrowLeft from '@lucide/svelte/icons/chevron-left';
|
||||||
|
import ChevronLeft from '@lucide/svelte/icons/chevron-left';
|
||||||
|
import ChevronRight from '@lucide/svelte/icons/chevron-right';
|
||||||
import type { UserResponseDto } from '../../../types';
|
import type { UserResponseDto } from '../../../types';
|
||||||
import UserCard from '@/components/UserCard.svelte';
|
import UserCard from '@/components/UserCard.svelte';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
|
import { obtenerSeguidosPorUsuario } from '@/hooks/obtenerSeguidosPorUsuario';
|
||||||
|
|
||||||
type Data = {
|
type Data = {
|
||||||
usuario: UserResponseDto;
|
usuario: UserResponseDto;
|
||||||
seguidos: UserResponseDto[];
|
seguidos: UserResponseDto[];
|
||||||
|
totalCount: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
let { data }: { data: Data } = $props();
|
let { data }: { data: Data } = $props();
|
||||||
|
|
||||||
|
let currentPage = $state(1);
|
||||||
|
let isLoading = $state(false);
|
||||||
|
const limit = 100;
|
||||||
|
|
||||||
|
let totalPages = $derived(Math.ceil(data.totalCount / limit));
|
||||||
|
|
||||||
|
async function loadPage(page: number) {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
isLoading = true;
|
||||||
|
const response = await obtenerSeguidosPorUsuario(data.usuario.id, page, limit);
|
||||||
|
|
||||||
|
if (response) {
|
||||||
|
data.seguidos = response.response as UserResponseDto[];
|
||||||
|
data.totalCount = response.totalCount;
|
||||||
|
currentPage = page;
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoading = false;
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex min-h-fit w-full items-center justify-center p-6 md:p-10">
|
<div class="flex min-h-fit w-full items-center justify-center p-6 md:p-10">
|
||||||
@@ -25,7 +51,12 @@
|
|||||||
<ArrowLeft />
|
<ArrowLeft />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{#if data.seguidos.length === 0}
|
|
||||||
|
{#if isLoading}
|
||||||
|
<div class="py-8 text-center text-muted-foreground">
|
||||||
|
<p>Cargando...</p>
|
||||||
|
</div>
|
||||||
|
{:else if data.seguidos.length === 0}
|
||||||
<div class="py-8 text-center text-muted-foreground">
|
<div class="py-8 text-center text-muted-foreground">
|
||||||
<p>No hay seguidos para mostrar.</p>
|
<p>No hay seguidos para mostrar.</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -38,5 +69,29 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if totalPages > 1}
|
||||||
|
<div class="mt-6 flex items-center justify-center gap-2">
|
||||||
|
<button
|
||||||
|
class="rounded-md border bg-card p-2 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
onclick={() => loadPage(currentPage - 1)}
|
||||||
|
disabled={currentPage === 1 || isLoading}
|
||||||
|
>
|
||||||
|
<ChevronLeft class="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<span class="px-4 text-sm text-muted-foreground">
|
||||||
|
Página {currentPage} de {totalPages}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="rounded-md border bg-card p-2 hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
onclick={() => loadPage(currentPage + 1)}
|
||||||
|
disabled={currentPage === totalPages || isLoading}
|
||||||
|
>
|
||||||
|
<ChevronRight class="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -10,12 +10,14 @@ export const load: PageLoad = async ({ params, fetch }) => {
|
|||||||
|
|
||||||
const seguidosResponse: UsersResponseDto | null = await obtenerSeguidosPorUsuario(
|
const seguidosResponse: UsersResponseDto | null = await obtenerSeguidosPorUsuario(
|
||||||
usuario.id,
|
usuario.id,
|
||||||
|
1,
|
||||||
100,
|
100,
|
||||||
fetch
|
fetch
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
usuario,
|
usuario,
|
||||||
seguidos: seguidosResponse?.response || []
|
seguidos: seguidosResponse?.response || [],
|
||||||
|
totalCount: seguidosResponse?.totalCount ?? 0
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user