Debian Codegraph @

frontend/src/reports/import/ImportFileUpload.svelte

raw
1 <script lang="ts">
2 import { put } from "../../api";
3 import { _ } from "../../i18n";
4 import { notify, notify_err } from "../../notifications";
5 import router from "../../router";
6
7 let input: HTMLInputElement | undefined = $state.raw();
8
9 /** Upload the selected files and reload. */
10 async function uploadImports(event: SubmitEvent) {
11 event.preventDefault();
12 if (input?.files == null) {
13 return;
14 }
15 await Promise.all(
16 Array.from(input.files).map(async (file) => {
17 const formData = new FormData();
18 formData.append("file", file, file.name);
19 return put("upload_import_file", formData).then(
20 notify,
21 (error: unknown) => {
22 notify_err(error, (err) => `Upload error: ${err.message}`);
23 },
24 );
25 }),
26 );
27 input.value = "";
28 router.reload();
29 }
30 </script>
31
32 <form onsubmit={uploadImports}>
33 <h2>{_("Upload files for import")}</h2>
34 <input bind:this={input} multiple type="file" />
35 <button type="submit">{_("Upload")}</button>
36 </form>