2022-09-02 01:52:40 +00:00
|
|
|
<template>
|
|
|
|
<BaseModal v-model="modal">
|
|
|
|
<template #title> Create Location </template>
|
|
|
|
<form @submit.prevent="create">
|
|
|
|
<FormTextField
|
|
|
|
ref="locationNameRef"
|
2022-09-09 22:46:53 +00:00
|
|
|
v-model="form.name"
|
|
|
|
:trigger-focus="focused"
|
2022-09-02 01:52:40 +00:00
|
|
|
:autofocus="true"
|
|
|
|
label="Location Name"
|
|
|
|
/>
|
2022-09-25 22:33:13 +00:00
|
|
|
<FormTextArea v-model="form.description" label="Location Description" />
|
2023-01-28 20:53:00 +00:00
|
|
|
<LocationSelector v-model="form.parent" />
|
2022-09-02 01:52:40 +00:00
|
|
|
<div class="modal-action">
|
|
|
|
<BaseButton type="submit" :loading="loading"> Create </BaseButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</BaseModal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2023-01-28 20:53:00 +00:00
|
|
|
import { LocationSummary } from "~~/lib/api/types/data-contracts";
|
2022-09-02 01:52:40 +00:00
|
|
|
const props = defineProps({
|
|
|
|
modelValue: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
const modal = useVModel(props, "modelValue");
|
2022-09-02 01:52:40 +00:00
|
|
|
const loading = ref(false);
|
|
|
|
const focused = ref(false);
|
|
|
|
const form = reactive({
|
2022-09-09 22:46:53 +00:00
|
|
|
name: "",
|
|
|
|
description: "",
|
2023-01-28 20:53:00 +00:00
|
|
|
parent: null as LocationSummary | null,
|
2022-09-02 01:52:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
whenever(
|
|
|
|
() => modal.value,
|
|
|
|
() => {
|
|
|
|
focused.value = true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function reset() {
|
2022-09-09 22:46:53 +00:00
|
|
|
form.name = "";
|
|
|
|
form.description = "";
|
2023-01-28 20:53:00 +00:00
|
|
|
form.parent = null;
|
2022-09-02 01:52:40 +00:00
|
|
|
focused.value = false;
|
|
|
|
modal.value = false;
|
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const api = useUserApi();
|
|
|
|
const toast = useNotifier();
|
|
|
|
|
|
|
|
async function create() {
|
|
|
|
loading.value = true;
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
const { data, error } = await api.locations.create({
|
|
|
|
name: form.name,
|
|
|
|
description: form.description,
|
|
|
|
parentId: form.parent ? form.parent.id : null,
|
|
|
|
});
|
2022-09-02 01:52:40 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
toast.error("Couldn't create location");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.success("Location created");
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
reset();
|
2022-11-01 03:07:01 +00:00
|
|
|
navigateTo(`/location/${data.id}`);
|
2022-09-02 01:52:40 +00:00
|
|
|
}
|
|
|
|
</script>
|