Add error handling and improve file upload UX
All checks were successful
SteamWarCI Build successful

This commit is contained in:
2025-04-02 09:52:37 +02:00
parent 7153cacbab
commit 48586f1a50
4 changed files with 50 additions and 23 deletions

View File

@ -21,19 +21,21 @@
import {createEventDispatcher} from "svelte"; import {createEventDispatcher} from "svelte";
import {schemRepo} from "@repo/schem.ts"; import {schemRepo} from "@repo/schem.ts";
import SWModal from "@components/styled/SWModal.svelte"; import SWModal from "@components/styled/SWModal.svelte";
import {t} from "astro-i18n" import {t} from "astro-i18n";
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
interface Props { interface Props {
open?: boolean; open?: boolean;
} }
let { open = $bindable(false) }: Props = $props(); let {open = $bindable(false)}: Props = $props();
async function upload() { async function upload(e: Event) {
e.stopPropagation();
if (uploadFile == null) { if (uploadFile == null) {
return error = "dashboard.schematic.errors.noFile";
return;
} }
let file = uploadFile[0]; let file = uploadFile[0];
@ -42,33 +44,46 @@
let type = name.split(".").pop(); let type = name.split(".").pop();
if (type !== "schem" && type !== "schematic") { if (type !== "schem" && type !== "schematic") {
return error = "dashboard.schematic.errors.invalidEnding";
return;
} }
let content = await file.arrayBuffer(); let content = await file.arrayBuffer();
// @ts-ignore
let b64 = btoa(String.fromCharCode.apply(null, new Uint8Array(content))); let b64 = btoa(String.fromCharCode.apply(null, new Uint8Array(content)));
let response = await $schemRepo.uploadSchematic(name, b64); try {
await $schemRepo.uploadSchematic(name, b64);
open = false; open = false;
value = "";
dispatch("reset");
} catch (e) {
error = "dashboard.schematic.errors.upload";
}
}
function reset(e: Event) {
e.stopPropagation();
open = false
value = ""; value = "";
dispatch("reset")
} }
let uploadFile: FileList | null = $state(null); let uploadFile: FileList | null = $state(null);
let value = $state(""); let value = $state("");
let error = $state(null)
</script> </script>
<SWModal title={t("dashboard.schematic.title")} bind:open> <SWModal title={t("dashboard.schematic.title")} bind:open>
<form> <form>
<input type="file" bind:files={uploadFile} bind:value /> <label for="schem-upload">{t("dashboard.schematic.title")}</label>
<input type="file" id="schem-upload" bind:files={uploadFile} class="overflow-ellipsis" bind:value accept=".schem, .schematic"/>
{#if error !== null}
<p class="text-red-400">{t(error)}</p>
{/if}
</form> </form>
{#snippet footer()} {#snippet footer()}
<button class="btn" onclick={upload}>{t("dashboard.schematic.upload")}</button>
<button class="btn !ml-auto" onclick={upload}>{t("dashboard.schematic.upload")}</button> <button class="btn btn-gray" onclick={reset}>{t("dashboard.schematic.cancel")}</button>
<button class="btn btn-gray" onclick={() => open = false}>{t("dashboard.schematic.cancel")}</button> {/snippet}
{/snippet}
</SWModal> </SWModal>

View File

@ -68,16 +68,18 @@
}) })
</script> </script>
<dialog bind:this={dialog} onclose={close} onclick={(e) => dialog.close()} aria-hidden="true" class="max-h-full max-w-md w-full rounded-lg shadow-lg dark:bg-neutral-800 dark:text-neutral-100"> <dialog bind:this={dialog} onclose={close} onclick={(e) => dialog.close()} aria-hidden="true" class="max-h-full min-w-md w-fit rounded-lg shadow-lg dark:bg-neutral-800 dark:text-neutral-100">
<div onclick={stopPropagation(onclick)} aria-hidden="true"> <div onclick={stopPropagation(onclick)} aria-hidden="true" class="w-fit">
<div class="p-6 border-b border-neutral-200 dark:border-neutral-700"> <div class="p-6 border-b border-neutral-200 dark:border-neutral-700">
<h1 class="text-4xl font-bold">{title}</h1> <h1 class="text-4xl font-bold">{title}</h1>
</div> </div>
<div class="p-6 main border-b border-neutral-200 dark:border-neutral-700"> <div class="p-6 main border-b border-neutral-200 dark:border-neutral-700">
{@render children?.()} {@render children?.()}
</div> </div>
<div class="flex mx-4 my-2 p-6" onclick={() => dialog.close()} aria-hidden="true"> <div class="mx-4 my-2 p-6">
{@render footer?.()} <div class="ml-auto flex justify-end" onclick={() => dialog.close()} aria-hidden="true">
{@render footer?.()}
</div>
</div> </div>
</div> </div>
</dialog> </dialog>

View File

@ -200,7 +200,12 @@
} }
}, },
"cancel": "Abbrechen", "cancel": "Abbrechen",
"title": "Schematic hochladen" "title": "Schematic hochladen",
"errors": {
"invalidEnding": "Diese Dateiendung kann nicht Hochgeladen werden.",
"noFile": "Keine Datei.",
"upload": "Fehler beim Hochladen, Überprüfe deine Schematic!"
}
} }
}, },
"login": { "login": {

View File

@ -137,6 +137,11 @@
"download": "Download", "download": "Download",
"close": "Close" "close": "Close"
} }
},
"errors": {
"invalidEnding": "This file extension cannot be uploaded.",
"noFile": "No file.",
"upload": "Error uploading, check your schematic!"
} }
} }
}, },