homebox/frontend/pages/item/[id]/index.vue

345 lines
9 KiB
Vue
Raw Permalink Normal View History

2022-09-06 18:32:13 +00:00
<script setup lang="ts">
import { DateDetail, Detail } from "~~/components/global/DetailsSection/types";
import { ItemAttachment } from "~~/lib/api/types/data-contracts";
2022-09-06 18:32:13 +00:00
definePageMeta({
layout: "home",
2022-09-06 18:32:13 +00:00
});
const route = useRoute();
const api = useUserApi();
const toast = useNotifier();
const itemId = computed<string>(() => route.params.id as string);
const preferences = useViewPreferences();
const { data: item, refresh } = useAsyncData(itemId.value, async () => {
2022-09-06 18:32:13 +00:00
const { data, error } = await api.items.get(itemId.value);
if (error) {
toast.error("Failed to load item");
navigateTo("/home");
2022-09-06 18:32:13 +00:00
return;
}
return data;
});
onMounted(() => {
refresh();
});
type FilteredAttachments = {
photos: ItemAttachment[];
attachments: ItemAttachment[];
warranty: ItemAttachment[];
manuals: ItemAttachment[];
};
const attachments = computed<FilteredAttachments>(() => {
if (!item.value) {
return {
photos: [],
attachments: [],
manuals: [],
warranty: [],
};
}
return item.value.attachments.reduce(
(acc, attachment) => {
if (attachment.type === "photo") {
acc.photos.push(attachment);
} else if (attachment.type === "warranty") {
acc.warranty.push(attachment);
} else if (attachment.type === "manual") {
acc.manuals.push(attachment);
} else {
acc.attachments.push(attachment);
}
return acc;
},
{
photos: [] as ItemAttachment[],
attachments: [] as ItemAttachment[],
warranty: [] as ItemAttachment[],
manuals: [] as ItemAttachment[],
}
);
});
const itemDetails = computed(() => {
return [
{
name: "Description",
text: item.value?.description,
},
{
name: "Serial Number",
text: item.value?.serialNumber,
},
{
name: "Mode Number",
text: item.value?.modelNumber,
},
{
name: "Manufacturer",
text: item.value?.manufacturer,
},
{
name: "Insured",
text: item.value?.insured ? "Yes" : "No",
},
{
name: "Notes",
text: item.value?.notes,
},
];
2022-09-06 18:32:13 +00:00
});
const showAttachments = computed(() => {
if (preferences.value?.showEmpty) {
return true;
}
return (
attachments.value.photos.length > 0 ||
attachments.value.attachments.length > 0 ||
attachments.value.warranty.length > 0 ||
attachments.value.manuals.length > 0
);
});
const attachmentDetails = computed(() => {
const details: Detail[] = [];
const push = (name: string) => {
details.push({
name,
text: "",
slot: name.toLowerCase(),
});
};
if (attachments.value.photos.length > 0) {
push("Photos");
}
if (attachments.value.attachments.length > 0) {
push("Attachments");
}
if (attachments.value.warranty.length > 0) {
push("Warranty");
}
if (attachments.value.manuals.length > 0) {
push("Manuals");
}
return details;
});
2022-09-09 18:20:38 +00:00
const showWarranty = computed(() => {
if (preferences.value.showEmpty) {
return true;
}
return validDate(item.value?.warrantyExpires);
2022-09-09 18:20:38 +00:00
});
const warrantyDetails = computed(() => {
const details: (Detail | DateDetail)[] = [
{
name: "Lifetime Warranty",
text: item.value?.lifetimeWarranty ? "Yes" : "No",
},
];
if (item.value?.lifetimeWarranty) {
details.push({
name: "Warranty Expires",
text: "N/A",
});
} else {
details.push({
name: "Warranty Expires",
text: item.value?.warrantyExpires,
type: "date",
});
2022-09-09 18:20:38 +00:00
}
details.push({
name: "Warranty Details",
text: item.value?.warrantyDetails || "",
});
2022-09-09 18:20:38 +00:00
return details;
2022-09-09 18:20:38 +00:00
});
2022-09-06 18:32:13 +00:00
const showPurchase = computed(() => {
if (preferences.value.showEmpty) {
return true;
}
return item.value?.purchaseFrom || item.value?.purchasePrice !== "0";
2022-09-06 18:32:13 +00:00
});
const purchaseDetails = computed<Array<Detail | DateDetail>>(() => {
return [
{
name: "Purchase From",
text: item.value?.purchaseFrom || "",
},
{
name: "Purchase Price",
text: item.value?.purchasePrice ? fmtCurrency(item.value.purchasePrice) : "",
},
{
name: "Purchase Date",
text: item.value.purchaseTime,
type: "date",
},
];
2022-09-06 18:32:13 +00:00
});
const showSold = computed(() => {
if (preferences.value.showEmpty) {
return true;
}
return item.value?.soldTo || item.value?.soldPrice !== "0";
2022-09-06 18:32:13 +00:00
});
const soldDetails = computed<Array<Detail | DateDetail>>(() => {
return [
{
name: "Sold To",
text: item.value?.soldTo || "",
},
{
name: "Sold Price",
text: item.value?.soldPrice ? fmtCurrency(item.value.soldPrice) : "",
},
{
name: "Sold At",
text: item.value?.soldTime || "",
type: "date",
},
];
2022-09-06 18:32:13 +00:00
});
2022-09-09 06:05:23 +00:00
const confirm = useConfirm();
async function deleteItem() {
const confirmed = await confirm.open("Are you sure you want to delete this item?");
2022-09-09 06:05:23 +00:00
if (!confirmed.data) {
return;
}
const { error } = await api.items.delete(itemId.value);
if (error) {
toast.error("Failed to delete item");
2022-09-09 06:05:23 +00:00
return;
}
toast.success("Item deleted");
navigateTo("/home");
2022-09-09 06:05:23 +00:00
}
2022-09-06 18:32:13 +00:00
</script>
2022-09-09 06:05:23 +00:00
<template>
<BaseContainer v-if="item" class="pb-8">
2022-09-09 06:05:23 +00:00
<section class="px-3">
<div class="flex justify-between items-center">
<div class="form-control"></div>
</div>
<div class="grid grid-cols-1 gap-3">
<BaseCard>
2022-09-09 06:05:23 +00:00
<template #title>
<BaseSectionHeader>
<Icon name="mdi-package-variant" class="mr-2 -mt-1 text-base-content" />
<span class="text-base-content">
{{ item ? item.name : "" }}
2022-09-09 06:05:23 +00:00
</span>
<template #description>
<p class="text-sm text-base-content font-bold pb-0 mb-0">
{{ item.location.name }} - Quantity {{ item.quantity }}
</p>
<div v-if="item.labels && item.labels.length > 0" class="flex flex-wrap gap-3 mt-3">
<LabelChip v-for="label in item.labels" :key="label.id" class="badge-primary" :label="label" />
2022-09-09 06:05:23 +00:00
</div>
</template>
</BaseSectionHeader>
</template>
<template #title-actions>
<div class="modal-action mt-0">
<label class="label cursor-pointer mr-auto">
<input v-model="preferences.showEmpty" type="checkbox" class="toggle toggle-primary" />
<span class="label-text ml-4"> Show Empty </span>
</label>
<BaseButton size="sm" :to="`/item/${itemId}/edit`">
<template #icon>
<Icon name="mdi-pencil" />
</template>
Edit
</BaseButton>
<BaseButton size="sm" @click="deleteItem">
<template #icon>
<Icon name="mdi-delete" />
</template>
Delete
</BaseButton>
</div>
</template>
<DetailsSection :details="itemDetails" />
</BaseCard>
<BaseCard v-if="showAttachments">
<template #title> Attachments </template>
<DetailsSection :details="attachmentDetails">
<template #manuals>
<ItemAttachmentsList
v-if="attachments.manuals.length > 0"
:attachments="attachments.manuals"
:item-id="item.id"
/>
</template>
<template #attachments>
<ItemAttachmentsList
v-if="attachments.attachments.length > 0"
:attachments="attachments.attachments"
:item-id="item.id"
/>
</template>
<template #warranty>
<ItemAttachmentsList
v-if="attachments.warranty.length > 0"
:attachments="attachments.warranty"
:item-id="item.id"
/>
</template>
<template #photos>
<ItemAttachmentsList
v-if="attachments.photos.length > 0"
:attachments="attachments.photos"
:item-id="item.id"
/>
</template>
</DetailsSection>
</BaseCard>
<BaseCard v-if="showPurchase">
<template #title> Purchase </template>
<DetailsSection :details="purchaseDetails" />
</BaseCard>
<BaseCard v-if="showWarranty">
2022-09-09 18:20:38 +00:00
<template #title> Warranty </template>
<DetailsSection :details="warrantyDetails" />
</BaseCard>
<BaseCard v-if="showSold">
2022-09-09 18:20:38 +00:00
<template #title> Sold </template>
<DetailsSection :details="soldDetails" />
</BaseCard>
2022-09-09 06:05:23 +00:00
</div>
</section>
</BaseContainer>
</template>