Skip to content

Commit

Permalink
Prefill add-on fields from query params
Browse files Browse the repository at this point in the history
Fixes #965
  • Loading branch information
allanlasser committed Dec 10, 2024
1 parent 678f802 commit 3beefa5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/lib/components/forms/AddOnDispatch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
} from "$lib/api/types";
import { enhance } from "$app/forms";
import { page } from "$app/stores";
import { afterNavigate } from "$app/navigation";
import Ajv from "ajv";
Expand Down Expand Up @@ -51,6 +52,12 @@
$: validator = ajv.compile({ type: "object", properties, required });
$: hasEvents = eventOptions && eventOptions.events.length > 0;
$: hasFields = Object.keys(properties).length > 0;
// prefill values from search params
$: new URLSearchParams($page.url.searchParams).forEach((v, k) => {
if (k in properties) {
$values[k] = v;
}
});
afterNavigate(() => {
// set initial values
Expand Down
20 changes: 19 additions & 1 deletion src/lib/components/forms/stories/AddOnDispatch.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Flex from "$lib/components/common/Flex.svelte";
import { addonsList } from "@/test/fixtures/addons";
import { APP_URL } from "@/config/config";
const addons = addonsList.results;
Expand All @@ -26,7 +27,7 @@
required={addon.parameters.required}
eventOptions={addon.parameters.eventOptions}
/>
<div class="values" style="max-width: 88ch">
<div class="values" style="max-width: 88ch" data-chromatic="ignore">
<h2><code>$values</code></h2>
<pre><code>{JSON.stringify($values, null, 2)}</code></pre>
</div>
Expand All @@ -46,3 +47,20 @@
<Story name="Bad Redactions" args={addons[5]} />

<Story name="Site Snapshot" args={addons[6]} />

<Story
name="Prefill Fields (Scraper)"
args={addons[1]}
parameters={{
sveltekit_experimental: {
stores: {
page: {
url: new URL(
"/?site=https://example.com&project=123&keywords=foo,bar&notAfield=hahagotya",
APP_URL,
),
},
},
},
}}
/>

0 comments on commit 3beefa5

Please sign in to comment.