diff --git a/src/components/GenerationList.svelte b/src/components/GenerationList.svelte index ce3d6bf..e1ebe60 100644 --- a/src/components/GenerationList.svelte +++ b/src/components/GenerationList.svelte @@ -7,26 +7,26 @@ import type { ListLoadResponse } from '../routes/api/get-generation-list/+server' import GenerationalListItemSkeleton from './GenerationalListItemSkeleton.svelte' import { browser } from '$app/environment' + import { generations, nextPageToken } from '$lib/stores' export let additionalGenerations: Models['TextToCad_type'][] = [] - let generations: Models['TextToCad_type'][] = [] const filterFailures = (item: Models['TextToCad_type']) => item.status !== 'failed' let PAGE_SIZE = 2 let isFetching = false - let nextPageToken: string | null = '' async function fetchData() { + if ($nextPageToken === null) return isFetching = true const response = await fetch(endpoints.localList, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', - body: JSON.stringify({ page_token: nextPageToken }) + body: JSON.stringify({ page_token: $nextPageToken }) }) const nextBatchPayload = (await response.json()) as ListLoadResponse - nextPageToken = nextBatchPayload?.body?.next_page ?? null + $nextPageToken = nextBatchPayload?.body?.next_page ?? null isFetching = false updateGenerations(nextBatchPayload) @@ -34,10 +34,12 @@ function updateGenerations(payload: ListLoadResponse) { const nextBatch = payload?.body?.items ?? [] - const newGenerations = [...generations, ...nextBatch] - generations = Array.from(new Set(newGenerations.map((item) => item.id))).map((id) => - newGenerations.find((item) => item.id === id) - ) as Models['TextToCad_type'][] + generations.update((g) => { + const newGenerations = [...g, ...nextBatch] + return Array.from(new Set(newGenerations.map((item) => item.id))).map((id) => + newGenerations.find((item) => item.id === id) + ) as Models['TextToCad_type'][] + }) } onMount(() => { @@ -45,7 +47,7 @@ fetchData() }) - $: console.log('generations', generations) + $: console.log('generations', $generations)
@@ -54,14 +56,14 @@ >generations - {#if generations.length > 0} + {#if $generations.length > 0}
([]) +export const nextPageToken = writable(undefined)