mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
add pagination in profile
This commit is contained in:
@@ -16,9 +16,9 @@
|
|||||||
let {
|
let {
|
||||||
post,
|
post,
|
||||||
variant = 'icon-lg'
|
variant = 'icon-lg'
|
||||||
}: { post: Omit<Partial<Post>, 'authorId'> & { authorId: string }; variant?: string } = $props();
|
}: { post: Omit<Partial<Post>, 'authorId'> & { authorId: string; id: string }; variant?: 'icon-lg' | 'default' | 'sm' | 'lg' | 'icon' | 'icon-sm' } = $props();
|
||||||
|
|
||||||
let seguido: Boolean | null = $state(null);
|
let seguido: boolean | null = $state(null);
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
window.addEventListener('followCacheUpdated', ((
|
window.addEventListener('followCacheUpdated', ((
|
||||||
@@ -40,9 +40,11 @@
|
|||||||
async function cargarSeguido() {
|
async function cargarSeguido() {
|
||||||
let a = cacheSeguidos.get(post.authorId);
|
let a = cacheSeguidos.get(post.authorId);
|
||||||
if (a === undefined) {
|
if (a === undefined) {
|
||||||
const seguidoStatus = await esSeguido(post);
|
const seguidoStatus = await esSeguido(post as Post);
|
||||||
cacheSeguidos.set(post.authorId, seguidoStatus.isFollowing || false);
|
if (seguidoStatus) {
|
||||||
seguido = seguidoStatus.isFollowing || false;
|
cacheSeguidos.set(post.authorId, seguidoStatus.isFollowing || false);
|
||||||
|
seguido = seguidoStatus.isFollowing || false;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
seguido = a;
|
seguido = a;
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
</Card>
|
</Card>
|
||||||
{:else}
|
{:else}
|
||||||
{#each $posts as post (post.id)}
|
{#each $posts as post (post.id)}
|
||||||
<div animate:slide>
|
<div transition:slide>
|
||||||
<PostCard {post} bind:postAModificar />
|
<PostCard {post} bind:postAModificar />
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { apiBase } from '@/stores/url';
|
import { apiBase } from '@/stores/url';
|
||||||
import PenLine from '@lucide/svelte/icons/pen-line';
|
|
||||||
import type { Post } from '../../types.js';
|
import type { Post } from '../../types.js';
|
||||||
import { fade, slide } from 'svelte/transition';
|
import { fade, slide } from 'svelte/transition';
|
||||||
import PostCard from '@/components/PostCard.svelte';
|
import PostCard from '@/components/PostCard.svelte';
|
||||||
@@ -9,7 +8,7 @@
|
|||||||
import ModalEditar from './modalEditar.svelte';
|
import ModalEditar from './modalEditar.svelte';
|
||||||
import { page } from '$app/state';
|
import { page } from '$app/state';
|
||||||
import Button from '@/components/ui/button/button.svelte';
|
import Button from '@/components/ui/button/button.svelte';
|
||||||
import { Dialog } from '@/components/ui/dialog/index.js';
|
import { Dialog } from '@/components/ui/dialog';
|
||||||
import CrearPost from '@/components/crear-post.svelte';
|
import CrearPost from '@/components/crear-post.svelte';
|
||||||
import DialogContent from '@/components/ui/dialog/dialog-content.svelte';
|
import DialogContent from '@/components/ui/dialog/dialog-content.svelte';
|
||||||
import DialogTitle from '@/components/ui/dialog/dialog-title.svelte';
|
import DialogTitle from '@/components/ui/dialog/dialog-title.svelte';
|
||||||
@@ -19,88 +18,131 @@
|
|||||||
import CardPerfil from '@/components/CardPerfil.svelte';
|
import CardPerfil from '@/components/CardPerfil.svelte';
|
||||||
import DialogModificarUsuario from '@/components/DialogModificarUsuario.svelte';
|
import DialogModificarUsuario from '@/components/DialogModificarUsuario.svelte';
|
||||||
import BotonSeguir from '@/components/BotonSeguir.svelte';
|
import BotonSeguir from '@/components/BotonSeguir.svelte';
|
||||||
import UserPen from '@lucide/svelte/icons/user-pen';
|
|
||||||
import DialogResetPassword from '@/components/DialogResetPassword.svelte';
|
import DialogResetPassword from '@/components/DialogResetPassword.svelte';
|
||||||
import Key from '@lucide/svelte/icons/key';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
let { params } = $props();
|
let { params } = $props();
|
||||||
|
|
||||||
let cargando = $state(true);
|
let cargando = $state(false);
|
||||||
|
let finished = $state(false);
|
||||||
|
let pageNumber = $state(1);
|
||||||
|
let sentinel: HTMLDivElement;
|
||||||
|
|
||||||
let mensajeError = $state('');
|
let mensajeError = $state('');
|
||||||
let postAModificar: Post | null = $state(null);
|
let postAModificar: Post | null = $state(null);
|
||||||
|
|
||||||
let showCrearPost = $state(false);
|
let showCrearPost = $state(false);
|
||||||
|
|
||||||
let data = $derived(page.data);
|
let data = $derived(page.data);
|
||||||
|
|
||||||
$effect(() => {
|
let fetching = false;
|
||||||
obtenerPosts();
|
|
||||||
});
|
|
||||||
|
|
||||||
async function obtenerPosts() {
|
async function obtenerPosts() {
|
||||||
|
if (fetching || finished) return;
|
||||||
|
|
||||||
|
fetching = true;
|
||||||
|
cargando = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const req = await fetch($apiBase + '/api/posts/user/' + params.perfil, {
|
|
||||||
method: 'GET',
|
const res = await fetch(
|
||||||
headers: {
|
`${$apiBase}/api/posts/user/${params.perfil}?page=${pageNumber}&pageSize=20`,
|
||||||
Authorization: `Bearer ${$sesionStore?.accessToken}`
|
{
|
||||||
}
|
headers: {
|
||||||
});
|
Authorization: `Bearer ${$sesionStore?.accessToken}`
|
||||||
if (req.ok) {
|
}
|
||||||
setPosts(await req.json());
|
}
|
||||||
|
);
|
||||||
|
const nuevosPosts: Post[] = await res.json();
|
||||||
|
|
||||||
|
if (nuevosPosts.length === 0) {
|
||||||
|
finished = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mensajeError = 'Fallo al obtener los datos';
|
|
||||||
} catch {
|
posts.update((actuales = []) => [...actuales, ...nuevosPosts]);
|
||||||
mensajeError = 'No se alcanzo el servidor';
|
|
||||||
|
pageNumber++;
|
||||||
|
|
||||||
|
if (nuevosPosts.length < 20) {
|
||||||
|
finished = true;
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
fetching = false;
|
||||||
cargando = false;
|
cargando = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
params.perfil;
|
||||||
|
|
||||||
|
setPosts([]);
|
||||||
|
pageNumber = 1;
|
||||||
|
finished = false;
|
||||||
|
mensajeError = '';
|
||||||
|
|
||||||
|
obtenerPosts();
|
||||||
|
});
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
const observer = new IntersectionObserver(
|
||||||
|
([entry]) => {
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
obtenerPosts();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ rootMargin: '200px' }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (sentinel) observer.observe(sentinel);
|
||||||
|
|
||||||
|
return () => observer.disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
async function handleEditar(e: SubmitEvent) {
|
async function handleEditar(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (postAModificar == null) return;
|
if (!postAModificar) return;
|
||||||
|
|
||||||
await updatePost(
|
await updatePost(
|
||||||
postAModificar,
|
postAModificar,
|
||||||
(postnuevo: Post) => updatePostStore(postAModificar!.id, postnuevo),
|
(postNuevo: Post) =>
|
||||||
|
updatePostStore(postAModificar!.id, postNuevo),
|
||||||
mensajeError
|
mensajeError
|
||||||
);
|
);
|
||||||
|
|
||||||
postAModificar = null;
|
postAModificar = null;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- {$inspect(data)} -->
|
|
||||||
<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">
|
||||||
<div class="w-full max-w-6xl">
|
<div class="w-full max-w-6xl">
|
||||||
{#key data}
|
{#key data}
|
||||||
<CardPerfil bind:data />
|
<CardPerfil bind:data />
|
||||||
{/key}
|
{/key}
|
||||||
<h1
|
|
||||||
class="mt-10 flex scroll-m-20 justify-between text-3xl font-extrabold tracking-tight lg:text-3xl"
|
<h1 class="mt-10 flex justify-between text-3xl font-extrabold">
|
||||||
>
|
|
||||||
Posts:
|
Posts:
|
||||||
{#if params.perfil == $sesionStore?.username}
|
{#if params.perfil === $sesionStore?.username}
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon-sm"
|
size="icon-sm"
|
||||||
class="m-1 rounded-full bg-blue-600"
|
class="rounded-full bg-blue-600"
|
||||||
onclick={() => {
|
onclick={() => (showCrearPost = true)}
|
||||||
showCrearPost = true;
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<PenLine />
|
+
|
||||||
</Button>
|
</Button>
|
||||||
{:else if $posts?.length == 0}
|
{:else if $posts.length === 0}
|
||||||
<BotonSeguir post={{ authorId: data.id }} />
|
<BotonSeguir post={{ authorId: data.id, id: data.id }} />
|
||||||
{/if}
|
{/if}
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<hr class="mb-8" />
|
<hr class="mb-8" />
|
||||||
{#if cargando}
|
|
||||||
|
{#if cargando && $posts.length === 0}
|
||||||
<CardCargando />
|
<CardCargando />
|
||||||
{:else if mensajeError !== ''}
|
{:else if mensajeError}
|
||||||
<CardError {mensajeError} />
|
<CardError {mensajeError} />
|
||||||
|
{:else if $posts.length === 0}
|
||||||
|
<CardError mensajeError="Este usuario no tiene posts" />
|
||||||
{:else}
|
{:else}
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
{#each $posts as post (post.id)}
|
{#each $posts as post (post.id)}
|
||||||
@@ -110,40 +152,37 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
<div bind:this={sentinel} class="h-1"></div>
|
||||||
|
|
||||||
|
{#if cargando && $posts.length > 0}
|
||||||
|
<div class="flex justify-center py-4">
|
||||||
|
<CardCargando />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if postAModificar}
|
{#if postAModificar}
|
||||||
<div in:fade>
|
<div in:fade>
|
||||||
<ModalEditar callbackfn={handleEditar} bind:post={postAModificar} />
|
<ModalEditar callbackfn={handleEditar} bind:post={postAModificar} />
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<div transition:fade>
|
<Dialog open={showCrearPost} onOpenChange={() => (showCrearPost = false)}>
|
||||||
<Dialog open={showCrearPost} onOpenChange={() => (showCrearPost = false)}>
|
<DialogContent>
|
||||||
<DialogContent
|
<DialogTitle>Crear publicación</DialogTitle>
|
||||||
onkeydown={(e: KeyboardEvent) => {
|
<CrearPost />
|
||||||
if (e.ctrlKey && e.key === 'Enter') {
|
</DialogContent>
|
||||||
showCrearPost = false;
|
</Dialog>
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<DialogTitle>Crear Publicacion</DialogTitle>
|
|
||||||
<CrearPost />
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{#if $sesionStore?.isAdmin || $sesionStore?.username == params.perfil}
|
{#if $sesionStore?.isAdmin || $sesionStore?.username === params.perfil}
|
||||||
<div class="fixed right-8 bottom-8 flex flex-col gap-2">
|
<div class="fixed right-8 bottom-8 flex flex-col gap-2">
|
||||||
<DialogModificarUsuario bind:data>
|
<DialogModificarUsuario bind:data>
|
||||||
<Button variant="default" size="icon-lg">
|
<Button>Modificar Usuario</Button>
|
||||||
<UserPen />
|
|
||||||
</Button>
|
|
||||||
</DialogModificarUsuario>
|
</DialogModificarUsuario>
|
||||||
<DialogResetPassword bind:data>
|
<DialogResetPassword bind:data>
|
||||||
<Button variant="default" size="icon-lg">
|
<Button>Reset Password</Button>
|
||||||
<Key />
|
|
||||||
</Button>
|
|
||||||
</DialogResetPassword>
|
</DialogResetPassword>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
@@ -152,6 +191,5 @@
|
|||||||
<meta property="og:title" content="Mini-x" />
|
<meta property="og:title" content="Mini-x" />
|
||||||
<meta property="og:description" content={`viendo el perfil de @${data.username}`} />
|
<meta property="og:description" content={`viendo el perfil de @${data.username}`} />
|
||||||
<meta property="og:image" content={data.imageUrl} />
|
<meta property="og:image" content={data.imageUrl} />
|
||||||
<meta property="og:url" content="https://minix-front.vercel.app/" />
|
|
||||||
<meta property="og:type" content="website" />
|
<meta property="og:type" content="website" />
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
Reference in New Issue
Block a user