diff options
Diffstat (limited to 'src/lib/form.ts')
-rw-r--r-- | src/lib/form.ts | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/lib/form.ts b/src/lib/form.ts index 787a397..844ca2d 100644 --- a/src/lib/form.ts +++ b/src/lib/form.ts @@ -1,4 +1,4 @@ -import { invalidate } from '$app/navigation'; +import { invalidate } from '$app/navigation' // this action (https://svelte.dev/tutorial/actions) allows us to // progressively enhance a <form> that already works without JS @@ -32,16 +32,16 @@ export function enhance( }) => void; } = {} ): { destroy: () => void } { - let current_token: unknown; + let current_token: unknown async function handle_submit(e: Event) { - const token = (current_token = {}); + const token = (current_token = {}) - e.preventDefault(); + e.preventDefault() - const data = new FormData(form); + const data = new FormData(form) - if (pending) pending({ data, form }); + if (pending) pending({ data, form }) try { const response = await fetch(form.action, { @@ -52,33 +52,33 @@ export function enhance( body: data }); - if (token !== current_token) return; + if (token !== current_token) return if (response.ok) { - if (result) result({ data, form, response }); + if (result) result({ data, form, response }) - const url = new URL(form.action); - url.search = url.hash = ''; - invalidate(url.href); + const url = new URL(form.action) + url.search = url.hash = '' + invalidate(url.href) } else if (error) { - error({ data, form, error: null, response }); + error({ data, form, error: null, response }) } else { - console.error(await response.text()); + console.error(await response.text()) } } catch (e: any) { if (error) { - error({ data, form, error: e, response: null }); + error({ data, form, error: e, response: null }) } else { - throw e; + throw e } } } - form.addEventListener('submit', handle_submit); + form.addEventListener('submit', handle_submit) return { destroy() { - form.removeEventListener('submit', handle_submit); + form.removeEventListener('submit', handle_submit) } - }; + } } |