mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-17 15:47:31 -03:00
add infinite scroll
This commit is contained in:
@@ -1,56 +1,71 @@
|
||||
<script lang="ts">
|
||||
import { goto, replaceState } from '$app/navigation';
|
||||
import { replaceState } from '$app/navigation';
|
||||
import { page } from '$app/state';
|
||||
import { onMount } from 'svelte';
|
||||
import Card from '@/components/ui/card/card.svelte';
|
||||
import { Content } from '@/components/ui/card';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import CrearPost from '@/components/crear-post.svelte';
|
||||
import { posts, resetPosts, setPosts, updatePostStore } from '@/stores/posts';
|
||||
import PostCard from '@/components/PostCard.svelte';
|
||||
import type { Post } from '../types';
|
||||
import ModalEditar from './[perfil]/modalEditar.svelte';
|
||||
import { updatePost } from '@/hooks/updatePost';
|
||||
import { fade, slide } from 'svelte/transition';
|
||||
import { getPosts } from '@/hooks/getPosts';
|
||||
import Spinner from '@/components/ui/spinner/spinner.svelte';
|
||||
import { page } from '$app/state';
|
||||
import Dialog from '@/components/ui/dialog/dialog.svelte';
|
||||
import DialogContent from '@/components/ui/dialog/dialog-content.svelte';
|
||||
import CrearPost from '@/components/crear-post.svelte';
|
||||
import PostCard from '@/components/PostCard.svelte';
|
||||
import ModalEditar from './[perfil]/modalEditar.svelte';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import {
|
||||
posts,
|
||||
updatePostStore,
|
||||
loadingPosts
|
||||
} from '@/stores/posts';
|
||||
import { updatePost } from '@/hooks/updatePost';
|
||||
import { loadMorePosts } from '@/hooks/loadMorePosts';
|
||||
import type { Post } from '../types';
|
||||
import { fade, slide } from 'svelte/transition';
|
||||
|
||||
$effect(() => {
|
||||
resetPosts();
|
||||
(async () => {
|
||||
setPosts(await getPosts());
|
||||
})();
|
||||
let postAModificar: Post | null = null;
|
||||
let mensajeError = '';
|
||||
let sentinel: HTMLDivElement;
|
||||
|
||||
onMount(() => {
|
||||
loadMorePosts();
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
if (entry.isIntersecting) {
|
||||
loadMorePosts();
|
||||
}
|
||||
},
|
||||
{ rootMargin: '200px' }
|
||||
);
|
||||
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
});
|
||||
|
||||
let postAModificar: Post | null = $state(null);
|
||||
let mensajeError = $state('');
|
||||
|
||||
async function handleEditar(e: SubmitEvent) {
|
||||
e.preventDefault();
|
||||
if (postAModificar == null) return;
|
||||
if (!postAModificar) return;
|
||||
|
||||
await updatePost(
|
||||
postAModificar,
|
||||
(postnuevo: Post) => updatePostStore(postAModificar!.id, postnuevo),
|
||||
|
||||
(postNuevo: Post) =>
|
||||
updatePostStore(postAModificar!.id, postNuevo),
|
||||
mensajeError
|
||||
);
|
||||
postAModificar = null;
|
||||
}
|
||||
let from = $state(page.url.searchParams.get('from'));
|
||||
$effect(() => {
|
||||
goto('/', { replaceState: true });
|
||||
});
|
||||
|
||||
let from = page.url.searchParams.get('from');
|
||||
|
||||
if (from) {
|
||||
replaceState('/', {});
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if from == 'cambio_contraseña'}
|
||||
<Dialog
|
||||
open={true}
|
||||
onOpenChange={() => {
|
||||
from = '';
|
||||
}}
|
||||
>
|
||||
<DialogContent>Se cambio la contraseña del usuario exitosamente</DialogContent>
|
||||
{#if from === 'cambio_contraseña'}
|
||||
<Dialog open>
|
||||
<DialogContent>
|
||||
Se cambió la contraseña del usuario exitosamente
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
{/if}
|
||||
|
||||
@@ -65,22 +80,25 @@
|
||||
<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="flex flex-col gap-2">
|
||||
{#if $sesionStore !== null}
|
||||
{#if $sesionStore}
|
||||
<CrearPost />
|
||||
{/if}
|
||||
<hr />
|
||||
|
||||
{#if $posts === undefined}
|
||||
{#if $posts.length === 0 && $loadingPosts}
|
||||
<Card>
|
||||
<Content class="flex items-center justify-center gap-2">
|
||||
<Spinner class="h-10 w-10" />
|
||||
<p>Cargando</p>
|
||||
</Content>
|
||||
</Card>
|
||||
{:else if $posts.length <= 0}
|
||||
|
||||
{:else if $posts.length === 0}
|
||||
<Card>
|
||||
<Content>
|
||||
<p class=" text-center leading-7 not-first:mt-6">No hay Posts que mostrar</p>
|
||||
<p class="text-center leading-7">
|
||||
No hay Posts que mostrar
|
||||
</p>
|
||||
</Content>
|
||||
</Card>
|
||||
{:else}
|
||||
@@ -91,10 +109,20 @@
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
<div bind:this={sentinel} class="h-1"></div>
|
||||
|
||||
{#if $loadingPosts && $posts.length > 0}
|
||||
<div class="flex justify-center py-4">
|
||||
<Spinner />
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{#if postAModificar}
|
||||
<div in:fade>
|
||||
<ModalEditar callbackfn={handleEditar} bind:post={postAModificar} />
|
||||
<ModalEditar
|
||||
callbackfn={handleEditar}
|
||||
bind:post={postAModificar}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
Reference in New Issue
Block a user