Add infinite loading support to hashtags

This commit is contained in:
WardPearce
2024-10-30 01:48:55 +13:00
parent dd8fa93368
commit db2b9937d4
3 changed files with 25 additions and 3 deletions
+2 -2
View File
@@ -151,8 +151,8 @@ export async function getSearchSuggestions(search: string, fetchOptions?: Reques
return await resp.json();
}
export async function getHashtag(tag: string): Promise<{ results: Video[]; }> {
const resp = await fetchErrorHandle(await fetch(buildPath(`hashtag/${tag}`)));
export async function getHashtag(tag: string, page: number = 0): Promise<{ results: Video[]; }> {
const resp = await fetchErrorHandle(await fetch(buildPath(`hashtag/${tag}?page=${page}`)));
return await resp.json();
}
@@ -1,7 +1,28 @@
<script lang="ts">
import { getHashtag } from '$lib/api/index.js';
import { activePageStore } from '$lib/store.js';
import VideoList from '$lib/VideoList.svelte';
import InfiniteLoading, { type InfiniteEvent } from 'svelte-infinite-loading';
export let data;
let currentPage = 1;
$: videos = [...data.hashTagVideos.results];
activePageStore.set('hashtag');
async function loadMore(event: InfiniteEvent) {
currentPage++;
const hashtagVideos = (await getHashtag(data.hashtag, currentPage)).results;
if (hashtagVideos.length === 0) {
event.detail.complete();
} else {
videos = [...videos, ...hashtagVideos];
event.detail.loaded();
}
}
</script>
<VideoList videos={data.hashTagVideos.results} />
<VideoList {videos} />
<InfiniteLoading on:infinite={loadMore} />
@@ -2,6 +2,7 @@ import { getHashtag } from '$lib/api';
export async function load({ params }) {
return {
hashtag: params.slug,
hashTagVideos: await getHashtag(params.slug)
};
}