Skip to content

Commit

Permalink
Merge pull request #461 from MuckRock/sveltekit-async-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast authored Mar 5, 2024
2 parents d354855 + 6fe0e4d commit 5f285a8
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
$: searchResults = data.searchResults;
$: query = data.query;
$: count = searchResults.count;
$: next = searchResults.next;
$: previous = searchResults.previous;
$: total_pages = Math.ceil(count / per_page);
async function load(url) {
const res = await fetch(url, { credentials: "include" }).catch((e) => {
Expand All @@ -52,17 +48,7 @@
error = { name: "Loading error", message: res.statusText };
}
data.searchResults = await res.json();
}
function load_next(e) {
page = Math.min(total_pages, page + 1);
load(next);
}
function load_previous(e) {
page = Math.max(0, page - 1);
load(previous);
data.searchResults = res.json();
}
</script>

Expand Down Expand Up @@ -116,15 +102,29 @@
<input type="checkbox" name="select_all" />
Select all
</label>
<Paginator
slot="center"
{page}
totalPages={total_pages}
has_next={Boolean(next)}
has_previous={Boolean(previous)}
on:next={load_next}
on:previous={load_previous}
/>

<svelte:fragment slot="center">
{#await searchResults then sr}
{@const count = sr.count}
{@const total_pages = Math.ceil(count / per_page)}
{@const next = sr.next}
{@const previous = sr.previous}
<Paginator
{page}
totalPages={total_pages}
has_next={Boolean(next)}
has_previous={Boolean(previous)}
on:next={(e) => {
page = Math.min(total_pages, page + 1);
load(next);
}}
on:previous={(e) => {
page = Math.max(1, page - 1);
load(previous);
}}
/>
{/await}
</svelte:fragment>

<label slot="right">
Per page
Expand Down

0 comments on commit 5f285a8

Please sign in to comment.