From f1055bb0d5ae8fd67a0786c52f13e20a4f923503 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Wed, 27 Mar 2024 10:41:10 -0400 Subject: [PATCH] Immutable data is better --- src/routes/app/+page.svelte | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/routes/app/+page.svelte b/src/routes/app/+page.svelte index ba6f80756..b0cf355f4 100644 --- a/src/routes/app/+page.svelte +++ b/src/routes/app/+page.svelte @@ -13,29 +13,31 @@ export let data; - let page_number = 1; - $: searchResults = data.searchResults; $: query = data.query; $: per_page = data.per_page; $: page_number = data.page; // update the cursor URL param, forcing refresh of search results - function setCursor(url: URL) { + async function setCursor(url: URL) { const cursor = url.searchParams.get("cursor"); const page_number = url.searchParams.get("page"); + // snapshot the current URL + const u = new URL($page.url); + // handle different environments that paginate differently - if (cursor) $page.url.searchParams.set("cursor", cursor); - if (per_page) $page.url.searchParams.set("page", page_number); + if (cursor) u.searchParams.set("cursor", cursor); + if (per_page) u.searchParams.set("page", page_number); - return goto($page.url); + return goto(u); } // update the per_page query param - function setPerPage(e) { - $page.url.searchParams.set("per_page", e.target.value); - return goto($page.url); + async function setPerPage(e) { + const u = new URL($page.url); + u.searchParams.set("per_page", e.target.value); + return goto(u); } @@ -67,13 +69,13 @@ totalPages={total_pages} has_next={Boolean(next)} has_previous={Boolean(previous)} - on:next={(e) => { - if (next) setCursor(new URL(next)); - page_number = Math.min(total_pages, page_number + 1); + on:next={async (e) => { + await setCursor(new URL(next)); + // page_number = Math.min(total_pages, page_number + 1); }} - on:previous={(e) => { - if (previous) setCursor(new URL(previous)); - page_number = Math.max(1, page_number - 1); + on:previous={async (e) => { + await setCursor(new URL(previous)); + // page_number = Math.max(1, page_number - 1); }} /> {/await}