homebox/frontend/components/Item/CreateModal.vue

102 lines
2.6 KiB
Vue
Raw Normal View History

2022-09-02 17:46:20 +00:00
<template>
<BaseModal v-model="modal">
<template #title> Create Item </template>
<form @submit.prevent="create">
2022-10-14 00:45:18 +00:00
<FormSelect v-model="form.location" label="Location" :items="locations ?? []" />
2022-09-02 17:46:20 +00:00
<FormTextField
ref="locationNameRef"
v-model="form.name"
:trigger-focus="focused"
2022-09-02 17:46:20 +00:00
: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">
<BaseButton ref="submitBtn" type="submit" :loading="loading">
<template #icon>
<Icon name="mdi-package-variant" class="swap-off" />
<Icon name="mdi-package-variant-closed" class="swap-on" />
</template>
Create
</BaseButton>
</div>
</form>
</BaseModal>
</template>
<script setup lang="ts">
import { ItemCreate, 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);
2022-09-02 17:46:20 +00:00
const submitBtn = ref(null);
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!
2022-09-02 17:46:20 +00:00
labels: [],
});
function reset() {
form.name = "";
form.description = "";
form.color = "";
2022-09-02 17:46:20 +00:00
focused.value = false;
modal.value = false;
loading.value = false;
}
whenever(
() => modal.value,
() => {
focused.value = true;
}
);
async function create() {
2022-09-03 09:17:57 +00:00
if (!form.location) {
return;
}
const out: ItemCreate = {
parentId: undefined,
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");
2022-09-02 17:46:20 +00:00
reset();
navigateTo(`/item/${data.id}`);
2022-09-02 17:46:20 +00:00
}
</script>