mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-14 15:20:45 -03:00
add infinite scroll
This commit is contained in:
32
src/lib/hooks/loadMorePosts.ts
Normal file
32
src/lib/hooks/loadMorePosts.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user