2022-09-02 17:46:20 +00:00
|
|
|
<template>
|
|
|
|
<BaseModal v-model="modal">
|
|
|
|
<template #title> Create Item </template>
|
2023-08-02 21:00:57 +00:00
|
|
|
<form @submit.prevent="create()">
|
2023-01-29 22:20:18 +00:00
|
|
|
<LocationSelector v-model="form.location" />
|
2023-07-31 17:53:26 +00:00
|
|
|
<FormTextField ref="nameInput" v-model="form.name" :trigger-focus="focused" :autofocus="true" label="Item Name" />
|
2022-11-01 03:07:01 +00:00
|
|
|
<FormTextArea v-model="form.description" label="Item Description" />
|
2022-09-09 22:46:53 +00:00
|
|
|
<FormMultiselect v-model="form.labels" label="Labels" :items="labels ?? []" />
|
2022-09-02 17:46:20 +00:00
|
|
|
<div class="modal-action">
|
2023-01-22 06:15:23 +00:00
|
|
|
<div class="flex justify-center">
|
2023-08-02 21:00:57 +00:00
|
|
|
<BaseButton class="rounded-r-none" :loading="loading" type="submit">
|
2023-01-22 06:15:23 +00:00
|
|
|
<template #icon>
|
2024-03-01 01:20:18 +00:00
|
|
|
<MdiPackageVariant class="swap-off h-5 w-5" />
|
|
|
|
<MdiPackageVariantClosed class="swap-on h-5 w-5" />
|
2023-01-22 06:15:23 +00:00
|
|
|
</template>
|
|
|
|
Create
|
|
|
|
</BaseButton>
|
|
|
|
<div class="dropdown dropdown-top">
|
|
|
|
<label tabindex="0" class="btn rounded-l-none rounded-r-xl">
|
2024-03-01 01:20:18 +00:00
|
|
|
<MdiChevronDown class="h-5 w-5" name="mdi-chevron-down" />
|
2023-01-22 06:15:23 +00:00
|
|
|
</label>
|
2023-08-31 17:07:45 +00:00
|
|
|
<ul tabindex="0" class="dropdown-content menu p-2 shadow bg-base-100 rounded-box w-64 right-0">
|
2023-07-31 17:53:26 +00:00
|
|
|
<li>
|
2023-08-02 21:00:57 +00:00
|
|
|
<button type="button" @click="create(false)">Create and Add Another</button>
|
2023-07-31 17:53:26 +00:00
|
|
|
</li>
|
2023-01-22 06:15:23 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-02 17:46:20 +00:00
|
|
|
</div>
|
2023-08-02 21:00:57 +00:00
|
|
|
</form>
|
2023-07-31 17:53:26 +00:00
|
|
|
<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">
|
2024-03-01 18:08:14 +00:00
|
|
|
import type { ItemCreate, LabelOut, LocationOut } from "~~/lib/api/types/data-contracts";
|
2022-09-13 03:36:22 +00:00
|
|
|
import { useLabelStore } from "~~/stores/labels";
|
|
|
|
import { useLocationStore } from "~~/stores/locations";
|
2024-03-01 01:20:18 +00:00
|
|
|
import MdiPackageVariant from "~icons/mdi/package-variant";
|
|
|
|
import MdiPackageVariantClosed from "~icons/mdi/package-variant-closed";
|
|
|
|
import MdiChevronDown from "~icons/mdi/chevron-down";
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-02 17:46:20 +00:00
|
|
|
const props = defineProps({
|
|
|
|
modelValue: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-31 17:53:26 +00:00
|
|
|
const api = useUserApi();
|
|
|
|
const toast = useNotifier();
|
|
|
|
|
|
|
|
const locationsStore = useLocationStore();
|
|
|
|
const locations = computed(() => locationsStore.allLocations);
|
|
|
|
|
|
|
|
const labelStore = useLabelStore();
|
|
|
|
const labels = computed(() => labelStore.labels);
|
|
|
|
|
2022-12-02 03:21:49 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2023-07-31 17:53:26 +00:00
|
|
|
const nameInput = ref<HTMLInputElement | null>(null);
|
2022-09-02 17:46:20 +00:00
|
|
|
|
2022-09-09 22:46:53 +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({
|
2022-10-09 13:03:24 +00:00
|
|
|
location: locations.value && locations.value.length > 0 ? locations.value[0] : ({} as LocationOut),
|
2022-09-09 22:46:53 +00:00
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
color: "", // Future!
|
2023-02-18 06:41:01 +00:00
|
|
|
labels: [] as LabelOut[],
|
2022-09-02 17:46:20 +00:00
|
|
|
});
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
const { shift } = useMagicKeys();
|
|
|
|
|
2022-09-02 17:46:20 +00:00
|
|
|
whenever(
|
|
|
|
() => modal.value,
|
|
|
|
() => {
|
|
|
|
focused.value = true;
|
2022-12-02 03:21:49 +00:00
|
|
|
|
|
|
|
if (locationId.value) {
|
2023-02-18 06:41:01 +00:00
|
|
|
const found = locations.value.find(l => l.id === locationId.value);
|
|
|
|
if (found) {
|
|
|
|
form.location = found;
|
|
|
|
}
|
2022-12-02 03:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (labelId.value) {
|
|
|
|
form.labels = labels.value.filter(l => l.id === labelId.value);
|
|
|
|
}
|
2022-09-02 17:46:20 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
async function create(close = true) {
|
2022-09-03 09:17:57 +00:00
|
|
|
if (!form.location) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
if (shift.value) {
|
|
|
|
close = false;
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
const out: ItemCreate = {
|
2023-02-18 06:41:01 +00:00
|
|
|
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[],
|
|
|
|
};
|
|
|
|
|
2022-11-01 03:07:01 +00:00
|
|
|
const { error, data } = await api.items.create(out);
|
2023-10-06 21:22:16 +00:00
|
|
|
loading.value = false;
|
2022-09-02 17:46:20 +00:00
|
|
|
if (error) {
|
2022-10-09 13:03:24 +00:00
|
|
|
toast.error("Couldn't create item");
|
2022-09-02 17:46:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.success("Item created");
|
2023-01-22 06:15:23 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
</script>
|