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 {schemRepo} from "@repo/schem.ts";
import SWModal from "@components/styled/SWModal.svelte";
import {t} from "astro-i18n"
import {t} from "astro-i18n";
const dispatch = createEventDispatcher();
interface Props {
open?: boolean;
}
interface Props {
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) {
return
error = "dashboard.schematic.errors.noFile";
return;
}
let file = uploadFile[0];
@@ -42,33 +44,46 @@
let type = name.split(".").pop();
if (type !== "schem" && type !== "schematic") {
return
error = "dashboard.schematic.errors.invalidEnding";
return;
}
let content = await file.arrayBuffer();
// @ts-ignore
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 = "";
dispatch("reset")
}
let uploadFile: FileList | null = $state(null);
let value = $state("");
let error = $state(null)
</script>
<SWModal title={t("dashboard.schematic.title")} bind:open>
<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>
{#snippet footer()}
<button class="btn !ml-auto" onclick={upload}>{t("dashboard.schematic.upload")}</button>
<button class="btn btn-gray" onclick={() => open = false}>{t("dashboard.schematic.cancel")}</button>
{/snippet}
<button class="btn" onclick={upload}>{t("dashboard.schematic.upload")}</button>
<button class="btn btn-gray" onclick={reset}>{t("dashboard.schematic.cancel")}</button>
{/snippet}
</SWModal>