Skip to content

Commit 1b2d433

Browse files
committed
chore: test html form submission
1 parent 43f06aa commit 1b2d433

File tree

2 files changed

+111
-14
lines changed

2 files changed

+111
-14
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<template>
2+
<UPage>
3+
<UPageHeader
4+
:title="title"
5+
headline="Vertalen"
6+
:description="description"
7+
/>
8+
9+
<UPageBody>
10+
<form method="post" action="/api/outlines" enctype="multipart/form-data">
11+
<!-- <UFileUpload
12+
v-model="jwpubFile"
13+
name="file"
14+
:file-delete="false"
15+
accept=".jwpub,.JWPUB"
16+
class="w-full min-h-48"
17+
:disabled="!!jwpubFile"
18+
label="Importeer vanuit S-34 formulier."
19+
/> -->
20+
<input name="file" type="file" accept=".jwpub,.JWPUB" />
21+
<UButton class="mr-2" type="reset" color="error" label="Reset" />
22+
<UButton type="submit" label="Importeren" />
23+
</form>
24+
<template v-if="loading">
25+
<UPageGrid>
26+
<div v-for="i in 12" :key="i" class="grid gap-2">
27+
<USkeleton class="h-4 w-3xs" />
28+
<USkeleton class="h-4 w-20" />
29+
<USkeleton class="h-4 w-full" />
30+
</div>
31+
</UPageGrid>
32+
</template>
33+
<template v-else>
34+
<UPageGrid>
35+
<OutlineForm
36+
v-for="outline in jsonStore.originals.outlines"
37+
:id="`outline-${outline.number}`"
38+
:key="outline.number"
39+
:outline="outline"
40+
/>
41+
</UPageGrid>
42+
</template>
43+
</UPageBody>
44+
</UPage>
45+
</template>
46+
<script setup lang="ts">
47+
const jsonStore = useJsonStore();
48+
49+
const loading = ref(false);
50+
const jwpubFile = ref<File | null>(null);
51+
52+
const { showError } = useFlash();
53+
54+
const _importOutlines = async () => {
55+
if (!jwpubFile.value) return;
56+
57+
loading.value = true;
58+
try {
59+
const body = new FormData();
60+
body.append("file", jwpubFile.value);
61+
const parsedOutlines = await $fetch<Outline[]>("/api/outlines", {
62+
body,
63+
method: "POST",
64+
});
65+
66+
const outlines =
67+
jsonStore.outlines.translations?.map((outline) => ({
68+
...outline,
69+
...(parsedOutlines.find((o) => o.number === outline.number) ?? {}),
70+
})) ?? [];
71+
72+
jsonStore.setTranslations({ outlines }, "outlines");
73+
} catch {
74+
showError({
75+
description:
76+
"Er is een fout opgetreden bij het importeren van de lezingen.",
77+
id: "import-outlines-error",
78+
});
79+
}
80+
loading.value = false;
81+
jwpubFile.value = null;
82+
};
83+
84+
const title = "Lezingen";
85+
const description = "Vertaal op deze pagina de lezingen.";
86+
87+
useSeoMeta({
88+
description,
89+
ogDescription: description,
90+
ogTitle: title,
91+
title,
92+
});
93+
</script>

server/utils/blob.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,30 @@ export async function receiveFiles(
2626
multiple: true,
2727
} satisfies BlobUploadOptions);
2828

29-
const form = await readFormData(event);
30-
const files = form.getAll(options.formKey!) as File[];
29+
try {
30+
const form = await readFormData(event);
31+
const files = form.getAll(options.formKey!) as File[];
3132

32-
if (!files?.length) throw new Error("No files received");
33+
if (!files?.length) throw new Error("No files received");
3334

34-
if (!options.multiple && files.length > 1)
35-
throw new Error("Multiple files are not allowed");
35+
if (!options.multiple && files.length > 1)
36+
throw new Error("Multiple files are not allowed");
3637

37-
if (typeof options.multiple === "number" && files.length > options.multiple)
38-
throw new Error(
39-
`Number of files exceeded. Maximum allowed: ${options.multiple}`,
40-
);
38+
if (typeof options.multiple === "number" && files.length > options.multiple)
39+
throw new Error(
40+
`Number of files exceeded. Maximum allowed: ${options.multiple}`,
41+
);
4142

42-
if (options.ensure?.maxSize || options.ensure?.types?.length) {
43-
for (const file of files) {
44-
ensureBlob(file, options.ensure);
43+
if (options.ensure?.maxSize || options.ensure?.types?.length) {
44+
for (const file of files) {
45+
ensureBlob(file, options.ensure);
46+
}
4547
}
48+
return files;
49+
} catch (e) {
50+
console.error(e);
51+
throw new Error("Error receiving files", { cause: e });
4652
}
47-
48-
return files;
4953
}
5054

5155
/**

0 commit comments

Comments
 (0)