aboutsummaryrefslogtreecommitdiff
path: root/src/lib/form.ts
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-02-15 01:33:08 +0000
committermat <github@matdoes.dev>2022-02-15 01:33:08 +0000
commitffe5eea0ce73cae8657c547f881b6f41270fef37 (patch)
treeeff4b6c4e4b9a155e8c6b68fd1843db20d8b4aa5 /src/lib/form.ts
parent9f28b6d9160fee5eff92d1d9849191f2f12faeab (diff)
downloadskyblock-stats-ffe5eea0ce73cae8657c547f881b6f41270fef37.tar.gz
skyblock-stats-ffe5eea0ce73cae8657c547f881b6f41270fef37.tar.bz2
skyblock-stats-ffe5eea0ce73cae8657c547f881b6f41270fef37.zip
start adding stuff
Diffstat (limited to 'src/lib/form.ts')
-rw-r--r--src/lib/form.ts36
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)
}
- };
+ }
}