2022-09-01 22:32:03 +00:00
|
|
|
<script setup lang="ts">
|
2022-09-09 22:46:53 +00:00
|
|
|
import ActionsDivider from "../../components/Base/ActionsDivider.vue";
|
2022-09-01 22:32:03 +00:00
|
|
|
|
|
|
|
definePageMeta({
|
2022-09-09 22:46:53 +00:00
|
|
|
layout: "home",
|
2022-09-01 22:32:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const route = useRoute();
|
|
|
|
const api = useUserApi();
|
|
|
|
const toast = useNotifier();
|
|
|
|
|
2022-09-02 17:46:20 +00:00
|
|
|
const preferences = useViewPreferences();
|
2022-09-01 22:32:03 +00:00
|
|
|
|
|
|
|
const locationId = computed<string>(() => route.params.id as string);
|
|
|
|
|
2022-09-02 01:52:40 +00:00
|
|
|
const { data: location } = useAsyncData(locationId.value, async () => {
|
|
|
|
const { data, error } = await api.locations.get(locationId.value);
|
|
|
|
if (error) {
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.error("Failed to load location");
|
|
|
|
navigateTo("/home");
|
2022-09-02 01:52:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
|
2022-09-01 22:32:03 +00:00
|
|
|
function maybeTimeAgo(date?: string): string {
|
|
|
|
if (!date) {
|
2022-09-09 22:46:53 +00:00
|
|
|
return "??";
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const time = new Date(date);
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
return `${useTimeAgo(time).value} (${useDateFormat(time, "MM-DD-YYYY").value})`;
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const details = computed(() => {
|
|
|
|
const dt = {
|
2022-09-09 22:46:53 +00:00
|
|
|
Name: location.value?.name || "",
|
|
|
|
Description: location.value?.description || "",
|
2022-09-01 22:32:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (preferences.value.showDetails) {
|
2022-09-09 22:46:53 +00:00
|
|
|
dt["Created At"] = maybeTimeAgo(location.value?.createdAt);
|
|
|
|
dt["Updated At"] = maybeTimeAgo(location.value?.updatedAt);
|
|
|
|
dt["Database ID"] = location.value?.id || "";
|
|
|
|
dt["Group Id"] = location.value?.groupId || "";
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dt;
|
|
|
|
});
|
|
|
|
|
|
|
|
const { reveal } = useConfirm();
|
|
|
|
|
|
|
|
async function confirmDelete() {
|
2022-09-09 22:46:53 +00:00
|
|
|
const { isCanceled } = await reveal("Are you sure you want to delete this location? This action cannot be undone.");
|
2022-09-01 22:32:03 +00:00
|
|
|
|
|
|
|
if (isCanceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { error } = await api.locations.delete(locationId.value);
|
|
|
|
|
|
|
|
if (error) {
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.error("Failed to delete location");
|
2022-09-01 22:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.success("Location deleted");
|
|
|
|
navigateTo("/home");
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateModal = ref(false);
|
|
|
|
const updating = ref(false);
|
|
|
|
const updateData = reactive({
|
2022-09-09 22:46:53 +00:00
|
|
|
name: "",
|
|
|
|
description: "",
|
2022-09-01 22:32:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function openUpdate() {
|
2022-09-09 22:46:53 +00:00
|
|
|
updateData.name = location.value?.name || "";
|
|
|
|
updateData.description = location.value?.description || "";
|
2022-09-01 22:32:03 +00:00
|
|
|
updateModal.value = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function update() {
|
|
|
|
updating.value = true;
|
|
|
|
const { error, data } = await api.locations.update(locationId.value, updateData);
|
|
|
|
|
|
|
|
if (error) {
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.error("Failed to update location");
|
2022-09-01 22:32:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
toast.success("Location updated");
|
2022-09-01 22:32:03 +00:00
|
|
|
location.value = data;
|
|
|
|
updateModal.value = false;
|
|
|
|
updating.value = false;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<BaseContainer>
|
|
|
|
<BaseModal v-model="updateModal">
|
|
|
|
<template #title> Update Location </template>
|
|
|
|
<form v-if="location" @submit.prevent="update">
|
2022-09-09 22:46:53 +00:00
|
|
|
<FormTextField v-model="updateData.name" :autofocus="true" label="Location Name" />
|
|
|
|
<FormTextField v-model="updateData.description" label="Location Description" />
|
2022-09-01 22:32:03 +00:00
|
|
|
<div class="modal-action">
|
|
|
|
<BaseButton type="submit" :loading="updating"> Update </BaseButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</BaseModal>
|
|
|
|
<section>
|
2022-09-03 09:17:57 +00:00
|
|
|
<BaseSectionHeader class="mb-5" dark>
|
2022-09-09 22:46:53 +00:00
|
|
|
{{ location ? location.name : "" }}
|
2022-09-01 22:32:03 +00:00
|
|
|
</BaseSectionHeader>
|
|
|
|
<BaseDetails class="mb-2" :details="details">
|
|
|
|
<template #title> Location Details </template>
|
|
|
|
</BaseDetails>
|
|
|
|
<div class="form-control ml-auto mr-2 max-w-[130px]">
|
|
|
|
<label class="label cursor-pointer">
|
2022-09-09 22:46:53 +00:00
|
|
|
<input v-model="preferences.showDetails" type="checkbox" class="toggle" />
|
2022-09-01 22:32:03 +00:00
|
|
|
<span class="label-text"> Detailed View </span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<ActionsDivider @delete="confirmDelete" @edit="openUpdate" />
|
|
|
|
</section>
|
|
|
|
|
2022-09-03 09:17:57 +00:00
|
|
|
<section v-if="location">
|
|
|
|
<BaseSectionHeader class="mb-5"> Items </BaseSectionHeader>
|
|
|
|
<div class="grid gap-2 grid-cols-2">
|
2022-09-09 22:46:53 +00:00
|
|
|
<ItemCard v-for="item in location.items" :key="item.id" :item="item" />
|
2022-09-03 09:17:57 +00:00
|
|
|
</div>
|
|
|
|
</section>
|
2022-09-01 22:32:03 +00:00
|
|
|
</BaseContainer>
|
|
|
|
</template>
|