From 6c750748ba31bf2424319154a12f6ff80375f1f4 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Thu, 11 Apr 2024 16:03:17 -0400 Subject: [PATCH] Loading state --- src/lib/components/forms/DocumentUpload.svelte | 18 ++++++++++++++---- src/routes/app/upload/+page.svelte | 12 +++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lib/components/forms/DocumentUpload.svelte b/src/lib/components/forms/DocumentUpload.svelte index 66802eefa..84ab2ca9d 100644 --- a/src/lib/components/forms/DocumentUpload.svelte +++ b/src/lib/components/forms/DocumentUpload.svelte @@ -128,6 +128,7 @@ let files: File[] = []; let projects: Project[] = []; + let loading = false; let uploader: HTMLInputElement; let fileDropActive: boolean; @@ -171,22 +172,26 @@ function filenameToTitle(filename: string): string { const [name, ...ext] = filename.split("."); - return name.replace(/_/g, " ").replace(/([A-Z])/g, " $1"); + return name.replace(/_/g, " "); } // handle uploads client side instead of going through the server async function onSubmit(e: SubmitEvent) { + loading = true; const form = e.target as HTMLFormElement; const fd = new FormData(form); const result = await upload(fd, csrf_token); // send data up - applyAction(result); + await applyAction(result); if (result.type === "success") { form.reset(); + files = []; } + + loading = false; } afterUpdate(() => { @@ -240,7 +245,11 @@
- +

Drag and drop files here

@@ -310,7 +319,8 @@ - Begin Upload - import type { ActionData } from "./$types"; + import { page } from "$app/stores"; + import Flex from "$lib/components/common/Flex.svelte"; import DocumentUpload from "$lib/components/forms/DocumentUpload.svelte"; - export let form: ActionData; + // using $page.form captures the correct type from applyAction + $: form = $page.form; @@ -14,11 +16,11 @@

Upload documents

- {#if form?.type === "success"} + {#if form?.success}

- {form.data?.message} + {form?.message}

- {:else if form?.type === "error"} + {:else if form?.error}

{form?.error}