homebox/frontend/components/Item/CreateModal.vue

153 lines
4.2 KiB
Vue
Raw Normal View History

2022-09-02 17:46:20 +00:00
<template>
<BaseModal v-model="modal">
<template #title> Create Item </template>
<div @keyup="keySubmit">
<LocationSelector v-model="form.location" />
<FormTextField ref="nameInput" v-model="form.name" :trigger-focus="focused" :autofocus="true" label="Item Name" />
<FormTextArea v-model="form.description" label="Item Description" />
<FormMultiselect v-model="form.labels" label="Labels" :items="labels ?? []" />
2022-09-02 17:46:20 +00:00
<div class="modal-action">
<div class="flex justify-center">
<BaseButton class="rounded-r-none" :loading="loading" @click="create(true)">
<template #icon>
<Icon name="mdi-package-variant" class="swap-off h-5 w-5" />
<Icon name="mdi-package-variant-closed" class="swap-on h-5 w-5" />
</template>
Create
</BaseButton>
<div class="dropdown dropdown-top">
<label tabindex="0" class="btn rounded-l-none rounded-r-xl">
<Icon class="h-5 w-5" name="mdi-chevron-down" />
</label>
<ul tabindex="0" class="dropdown-content menu p-2 shadow bg-base-100 rounded-box w-64">
<li>
<button type="button" @click.prevent="create(false)">Create and Add Another</button>
</li>
</ul>
</div>
</div>
2022-09-02 17:46:20 +00:00
</div>
</div>
<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
</p>
2022-09-02 17:46:20 +00:00
</BaseModal>
</template>
<script setup lang="ts">
import { ItemCreate, LabelOut, LocationOut } from "~~/lib/api/types/data-contracts";
import { useLabelStore } from "~~/stores/labels";
import { useLocationStore } from "~~/stores/locations";
2022-09-02 17:46:20 +00:00
const props = defineProps({
modelValue: {
type: Boolean,
required: true,
},
});
const api = useUserApi();
const toast = useNotifier();
const locationsStore = useLocationStore();
const locations = computed(() => locationsStore.allLocations);
const labelStore = useLabelStore();
const labels = computed(() => labelStore.labels);
const route = useRoute();
const labelId = computed(() => {
if (route.fullPath.includes("/label/")) {
return route.params.id;
}
return null;
});
const locationId = computed(() => {
if (route.fullPath.includes("/location/")) {
return route.params.id;
}
return null;
});
const nameInput = ref<HTMLInputElement | null>(null);
2022-09-02 17:46:20 +00:00
const modal = useVModel(props, "modelValue");
2022-09-02 17:46:20 +00:00
const loading = ref(false);
const focused = ref(false);
const form = reactive({
location: locations.value && locations.value.length > 0 ? locations.value[0] : ({} as LocationOut),
name: "",
description: "",
color: "", // Future!
labels: [] as LabelOut[],
2022-09-02 17:46:20 +00:00
});
whenever(
() => modal.value,
() => {
focused.value = true;
if (locationId.value) {
const found = locations.value.find(l => l.id === locationId.value);
if (found) {
form.location = found;
}
}
if (labelId.value) {
form.labels = labels.value.filter(l => l.id === labelId.value);
}
2022-09-02 17:46:20 +00:00
}
);
async function create(close = false) {
2022-09-03 09:17:57 +00:00
if (!form.location) {
return;
}
const out: ItemCreate = {
parentId: null,
2022-09-03 09:17:57 +00:00
name: form.name,
description: form.description,
locationId: form.location.id as string,
labelIds: form.labels.map(l => l.id) as string[],
};
const { error, data } = await api.items.create(out);
2022-09-02 17:46:20 +00:00
if (error) {
toast.error("Couldn't create item");
2022-09-02 17:46:20 +00:00
return;
}
toast.success("Item created");
// Reset
form.name = "";
form.description = "";
form.color = "";
focused.value = false;
loading.value = false;
if (close) {
modal.value = false;
navigateTo(`/item/${data.id}`);
}
2022-09-02 17:46:20 +00:00
}
async function keySubmit(e: KeyboardEvent) {
// Shift + Enter
if (e.shiftKey && e.key === "Enter") {
console.log("Shift + Enter");
e.preventDefault();
await create(false);
focused.value = true;
} else if (e.key === "Enter") {
e.preventDefault();
console.log("Enter");
await create(true);
}
}
2022-09-02 17:46:20 +00:00
</script>