feat: improve loading state for creation and fix types for adding image (#196)

This commit is contained in:
Tonya
2024-09-05 19:40:57 +00:00
committed by GitHub
parent 67c50068d9
commit 1e81b4bab4
3 changed files with 42 additions and 39 deletions

View File

@@ -7,12 +7,11 @@
<FormTextArea v-model="form.description" label="Item Description" /> <FormTextArea v-model="form.description" label="Item Description" />
<FormMultiselect v-model="form.labels" label="Labels" :items="labels ?? []" /> <FormMultiselect v-model="form.labels" label="Labels" :items="labels ?? []" />
<div class="modal-action"> <div class="modal-action">
<div class="flex justify-center"> <div class="flex justify-center">
<div> <div>
<label for="photo" class="btn">{{ $t("components.item.create_modal.photo_button") }}</label> <label for="photo" class="btn">{{ $t("components.item.create_modal.photo_button") }}</label>
<input type="file" accept="image/*" @change="previewImage" style="visibility:hidden;" id="photo"> <input id="photo" type="file" accept="image/*" style="visibility: hidden" @change="previewImage" />
</div> </div>
<BaseButton class="rounded-r-none" :loading="loading" type="submit"> <BaseButton class="rounded-r-none" :loading="loading" type="submit">
<template #icon> <template #icon>
@@ -34,16 +33,17 @@
</div> </div>
</div> </div>
<!-- photo preview area is AFTER the create button, to avoid pushing the button below the screen on small displays --> <!-- photo preview area is AFTER the create button, to avoid pushing the button below the screen on small displays -->
<div class="border-t border-gray-300 p-4"> <div class="border-t border-gray-300 p-4">
<template v-if="form.preview"> <template v-if="form.preview">
<p class="mb-0">file name: {{ form.photo.name }}</p> <p class="mb-0">File name: {{ form.photo?.name }}</p>
<img :src="form.preview" class="h-[100px] w-full object-cover rounded-t shadow-sm border-gray-300" /> <img
</template> :src="form.preview"
class="h-[100px] w-full object-cover rounded-t shadow-sm border-gray-300"
alt="Uploaded Photo"
/>
</template>
</div> </div>
</form> </form>
<p class="text-sm text-center mt-4"> <p class="text-sm text-center mt-4">
use <kbd class="kbd kbd-xs">Shift</kbd> + <kbd class="kbd kbd-xs"> Enter </kbd> to create and add another use <kbd class="kbd kbd-xs">Shift</kbd> + <kbd class="kbd kbd-xs"> Enter </kbd> to create and add another
@@ -103,40 +103,25 @@
description: "", description: "",
color: "", // Future! color: "", // Future!
labels: [] as LabelOut[], labels: [] as LabelOut[],
preview: null, preview: null as string | null,
photo: null photo: null as File | null,
}); });
const { shift } = useMagicKeys(); const { shift } = useMagicKeys();
function previewImage(event) { function previewImage(event: Event) {
var input = event.target; const input = event.target as HTMLInputElement;
if (input.files) { if (input.files && input.files.length > 0) {
var reader = new FileReader(); const reader = new FileReader();
reader.onload = (e) => { reader.onload = e => {
form.preview = e.target.result; form.preview = e.target?.result as string;
} };
form.photo=input.files[0]; const file = input.files[0];
reader.readAsDataURL(input.files[0]); form.photo = file;
reader.readAsDataURL(file);
} }
} }
function uploadImage(e: Event) {
const files = (e.target as HTMLInputElement).files;
if (!files || !files.item(0)) {
return;
}
const first = files.item(0);
if (!first) {
return;
}
uploadAttachment([first], null);
}
whenever( whenever(
() => modal.value, () => modal.value,
() => { () => {
@@ -160,6 +145,13 @@
return; return;
} }
if (loading.value) {
toast.error("Already creating an item");
return;
}
loading.value = true;
if (shift.value) { if (shift.value) {
close = false; close = false;
} }
@@ -175,6 +167,7 @@
const { error, data } = await api.items.create(out); const { error, data } = await api.items.create(out);
loading.value = false; loading.value = false;
if (error) { if (error) {
loading.value = false;
toast.error("Couldn't create item"); toast.error("Couldn't create item");
return; return;
} }
@@ -182,10 +175,11 @@
toast.success("Item created"); toast.success("Item created");
// if the photo was provided, upload it // if the photo was provided, upload it
if(form.photo){ if (form.photo) {
const { data2, error } = await api.items.attachments.add(data.id, form.photo, form.photo.name, AttachmentTypes.Photo); const { error } = await api.items.attachments.add(data.id, form.photo, form.photo.name, AttachmentTypes.Photo);
if (error) { if (error) {
loading.value = false;
toast.error("Failed to upload Photo"); toast.error("Failed to upload Photo");
return; return;
} }
@@ -193,7 +187,6 @@
toast.success("Photo uploaded"); toast.success("Photo uploaded");
} }
// Reset // Reset
form.name = ""; form.name = "";
form.description = ""; form.description = "";

View File

@@ -71,6 +71,12 @@
const { shift } = useMagicKeys(); const { shift } = useMagicKeys();
async function create(close = true) { async function create(close = true) {
if (loading.value) {
toast.error("Already creating a label");
return;
}
loading.value = true;
if (shift.value) { if (shift.value) {
close = false; close = false;
} }

View File

@@ -73,6 +73,10 @@
const { shift } = useMagicKeys(); const { shift } = useMagicKeys();
async function create(close = true) { async function create(close = true) {
if (loading.value) {
toast.error("Already creating a location");
return;
}
loading.value = true; loading.value = true;
if (shift.value) { if (shift.value) {