Skip to content
Snippets Groups Projects
Commit a2bad358 authored by Trirst's avatar Trirst
Browse files

Image metadata fields in upload page

parent 54b8b3dd
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
let imageUpload; let imageUpload;
let previews = []; let previews = [];
let forms = [];
const acceptedTypes = ["image/png", "image/jpeg", "image/gif"]; const acceptedTypes = ["image/png", "image/jpeg", "image/gif"];
function validImageTypes(file) { function validImageTypes(file) {
...@@ -21,11 +21,17 @@ ...@@ -21,11 +21,17 @@
} }
} }
function postImages() { async function postImages() {
let formData = new FormData(); let formData = new FormData();
let i = 0;
for (const file of imageUpload.files) { for (const file of imageUpload.files) {
formData.set("image", file); formData.set("image", file);
post1Image(formData); const imageMetadata = await post1Image(formData);
updateImageMetadata(
imageMetadata.snowflake,
serializeForm(forms[i])
);
i++;
} }
imageUpload.value = null; imageUpload.value = null;
previews = []; previews = [];
...@@ -42,6 +48,22 @@ ...@@ -42,6 +48,22 @@
const imageMetadata = await fetchAPI(paths.Image, options); const imageMetadata = await fetchAPI(paths.Image, options);
return imageMetadata; return imageMetadata;
} }
// Serialize image metada fields into JSON string that can be sent to API endpoints
function serializeForm(form) {
return JSON.stringify(Object.fromEntries(new FormData(form)));
}
function updateImageMetadata(snowflake, metadata) {
const options = {
method: "PATCH",
headers: {
secret: $user.secret,
},
body: metadata,
};
fetchAPI(paths.ImageField(snowflake), options);
}
</script> </script>
<main class="flex flex-col items-center mx-auto max-w-4xl"> <main class="flex flex-col items-center mx-auto max-w-4xl">
...@@ -55,8 +77,38 @@ ...@@ -55,8 +77,38 @@
/> />
<button on:click={postImages}>Submit</button> <button on:click={postImages}>Submit</button>
</div> </div>
<!-- {(console.log(forms), "")} -->
{#each previews as preview} {#each previews as preview, i}
<img src={preview} alt="" class="max-w-md" /> <div class="flex flex-row space-x-6 m-4">
<form class="flex flex-col flex-1 space-y-6" bind:this={forms[i]}>
<input
type="text"
placeholder="Source"
name="source"
autocomplete="off"
/>
<input
type="text"
placeholder="Parent"
name="parent"
autocomplete="off"
/>
<textarea
type="text"
placeholder="Commentary"
name="commentary"
autocomplete="off"
class="max-h-full resize-none flex-1"
/>
<textarea
type="text"
placeholder="Commentary translation"
name="commentary_translation"
autocomplete="off"
class="max-h-full resize-none flex-1"
/>
</form>
<img src={preview} alt="" class="max-w-xs" />
</div>
{/each} {/each}
</main> </main>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment