diff --git a/src/lib/hooks/getPosts.ts b/src/lib/hooks/getPosts.ts index e95c52e..040b95b 100644 --- a/src/lib/hooks/getPosts.ts +++ b/src/lib/hooks/getPosts.ts @@ -1,17 +1,21 @@ import { apiBase } from "@/stores/url"; import { sesionStore } from "@/stores/usuario"; import { get } from "svelte/store"; +import type { Post } from '../../types'; +import { PAGE_SIZE } from '../stores/posts'; -export async function getPosts() { +export async function getPosts(page: number = 1): Promise { + const token = get(sesionStore)?.accessToken; + const headers: HeadersInit = {}; + if (token) headers.Authorization = `Bearer ${token}`; - const req = await fetch(`${get(apiBase)}/timeline?pageSize=20`,{ - headers: { - Authorization: `Bearer ${get(sesionStore)?.accessToken}` + const res = await fetch( + `${get(apiBase)}/timeline?page=${page}&pageSize=${PAGE_SIZE}`, + { headers } + ); - } - }); - if (req.ok) { - return await req.json(); - } -} + if (!res.ok) throw new Error('Error cargando posts'); + + return res.json(); +} \ No newline at end of file diff --git a/src/lib/hooks/loadMorePosts.ts b/src/lib/hooks/loadMorePosts.ts new file mode 100644 index 0000000..e126152 --- /dev/null +++ b/src/lib/hooks/loadMorePosts.ts @@ -0,0 +1,32 @@ +import { get } from 'svelte/store'; +import { page, loadingPosts, PAGE_SIZE } from '@/stores/posts'; +import { appendPosts } from '@/stores/posts'; +import { getPosts } from './getPosts'; + +let finished = false; + +export async function loadMorePosts() { + if (get(loadingPosts) || finished) return; + + loadingPosts.set(true); + + try { + const currentPage = get(page); + const newPosts = await getPosts(currentPage); + + if (newPosts.length === 0) { + finished = true; + return; + } + + appendPosts(newPosts); + + if (newPosts.length < PAGE_SIZE) { + finished = true; + } else { + page.update(p => p + 1); + } + } finally { + loadingPosts.set(false); + } +} diff --git a/src/lib/stores/posts.ts b/src/lib/stores/posts.ts index 85fcbca..22823bf 100644 --- a/src/lib/stores/posts.ts +++ b/src/lib/stores/posts.ts @@ -1,29 +1,42 @@ import { writable } from 'svelte/store'; import type { Post } from '../../types'; -export const posts = writable(undefined); +export const posts = writable([]); +export const loadingPosts = writable(false); +export const page = writable(1); + +export const PAGE_SIZE = 20; export const setPosts = (newPosts: Post[]) => { posts.set(newPosts); }; -export const addPost = (post: Post) => { - posts.update((currentPosts) => [post, ...currentPosts]); +export const appendPosts = (newPosts: Post[]) => { + posts.update((current) => [...current, ...newPosts]); }; -export const updatePostStore = (postId: string, updatedData: Partial) => { - posts.update((currentPosts) => - currentPosts.map((post) => (post.id === postId ? { ...post, ...updatedData } : post)) +export const addPost = (post: Post) => { + posts.update((current) => [post, ...current]); +}; + +export const updatePostStore = ( + postId: string, + updatedData: Partial +) => { + posts.update((current) => + current.map((post) => + post.id === postId ? { ...post, ...updatedData } : post + ) ); }; export const removePost = (postId: string) => { - posts.update((currentPosts) => { - const a = currentPosts.filter((post) => post.id !== postId); - return a; - }); + posts.update((current) => + current.filter((post) => post.id !== postId) + ); }; export const resetPosts = () => { - posts.set(undefined); + posts.set([]); + page.set(1); }; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 20d7631..17c26e4 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,56 +1,71 @@ -{#if from == 'cambio_contraseña'} - { - from = ''; - }} - > - Se cambio la contraseña del usuario exitosamente +{#if from === 'cambio_contraseña'} + + + Se cambió la contraseña del usuario exitosamente + {/if} @@ -65,22 +80,25 @@
- {#if $sesionStore !== null} + {#if $sesionStore} {/if}
- {#if $posts === undefined} + {#if $posts.length === 0 && $loadingPosts}

Cargando

- {:else if $posts.length <= 0} + + {:else if $posts.length === 0} -

No hay Posts que mostrar

+

+ No hay Posts que mostrar +

{:else} @@ -91,10 +109,20 @@ {/each} {/if}
+
+ + {#if $loadingPosts && $posts.length > 0} +
+ +
+ {/if}
{#if postAModificar}
- +
-{/if} +{/if} \ No newline at end of file