Skip to content

Commit

Permalink
feat: filter details for zero values (#364)
Browse files Browse the repository at this point in the history
* filter details for zero values

* ensure exhaustive checks

* update event listener to only bind when collapsable
  • Loading branch information
hay-kot committed Mar 23, 2023
1 parent 840d220 commit 40e76ba
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 20 deletions.
50 changes: 40 additions & 10 deletions frontend/components/Base/Card.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
<template>
<div class="card bg-base-100 shadow-xl sm:rounded-lg">
<div v-if="$slots.title" class="px-4 py-5 sm:px-6">
<h3 class="text-lg font-medium leading-6">
<slot name="title"></slot>
</h3>
<p v-if="$slots.subtitle" class="mt-1 max-w-2xl text-sm text-gray-500">
<slot name="subtitle"></slot>
</p>
<template v-if="$slots['title-actions']">
<slot name="title-actions"></slot>
</template>
<component :is="collapsable ? 'button' : 'div'" v-on="collapsable ? { click: toggle } : {}">
<h3 class="text-lg font-medium leading-6 flex items-center">
<slot name="title"></slot>
<template v-if="collapsable">
<span class="ml-2 swap swap-rotate" :class="`${collapsed ? 'swap-active' : ''}`">
<Icon class="h-6 w-6 swap-on" name="mdi-chevron-right" />
<Icon class="h-6 w-6 swap-off" name="mdi-chevron-down" />
</span>
</template>
</h3>
</component>
<div>
<p v-if="$slots.subtitle" class="mt-1 max-w-2xl text-sm text-gray-500">
<slot name="subtitle"></slot>
</p>
<template v-if="$slots['title-actions']">
<slot name="title-actions"></slot>
</template>
</div>
</div>
<div
:class="{
'max-h-screen': !collapsed,
'max-h-0': collapsed,
}"
class="transition-[max-height] duration-200 overflow-hidden"
>
<slot />
</div>
<slot />
</div>
</template>

<script setup lang="ts">
defineProps<{
collapsable?: boolean;
}>();
function toggle() {
collapsed.value = !collapsed.value;
}
const collapsed = ref(false);
</script>
20 changes: 20 additions & 0 deletions frontend/components/global/DetailsSection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ export type Detail = BaseDetail & {
export type AnyDetail = DateDetail | CurrencyDetail | LinkDetail | MarkdownDetail | Detail;

export type Details = Array<Detail | AnyDetail>;

export function filterZeroValues(details: Details): Details {
return details.filter(detail => {
switch (detail.type) {
case "date":
return validDate(detail.text);
case "currency":
return !!detail.text;
case "link":
return !!detail.text && !!detail.href;
case undefined:
case "text":
case "markdown":
return detail.text !== null && detail.text !== "" && detail.text !== undefined;
default:
console.warn("Unknown detail type (this should never happen)", detail);
return false;
}
});
}
45 changes: 35 additions & 10 deletions frontend/pages/item/[id]/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { AnyDetail, Detail, Details } from "~~/components/global/DetailsSection/types";
import { AnyDetail, Detail, Details, filterZeroValues } from "~~/components/global/DetailsSection/types";
import { ItemAttachment } from "~~/lib/api/types/data-contracts";
definePageMeta({
Expand Down Expand Up @@ -121,7 +121,7 @@
return [];
}
return [
const ret: Details = [
{
name: "Description",
type: "markdown",
Expand Down Expand Up @@ -176,6 +176,12 @@
};
}),
];
if (!preferences.value.showEmpty) {
return filterZeroValues(ret);
}
return ret;
});
const showAttachments = computed(() => {
Expand Down Expand Up @@ -256,6 +262,10 @@
text: item.value?.warrantyDetails || "",
});
if (!preferences.value.showEmpty) {
return filterZeroValues(details);
}
return details;
});
Expand All @@ -267,7 +277,7 @@
});
const purchaseDetails = computed<Details>(() => {
return [
const v: Details = [
{
name: "Purchased From",
text: item.value?.purchaseFrom || "",
Expand All @@ -284,6 +294,12 @@
date: true,
},
];
if (!preferences.value.showEmpty) {
return filterZeroValues(v);
}
return v;
});
const showSold = computed(() => {
Expand All @@ -294,7 +310,7 @@
});
const soldDetails = computed<Details>(() => {
return [
const v: Details = [
{
name: "Sold To",
text: item.value?.soldTo || "",
Expand All @@ -311,6 +327,12 @@
date: true,
},
];
if (!preferences.value.showEmpty) {
return filterZeroValues(v);
}
return v;
});
const confirm = useConfirm();
Expand Down Expand Up @@ -442,7 +464,7 @@

<section>
<div class="space-y-6">
<BaseCard v-if="!hasNested">
<BaseCard v-if="!hasNested" collapsable>
<template #title> Details </template>
<template #title-actions>
<div class="flex flex-wrap justify-between items-center mt-2 gap-4">
Expand All @@ -469,9 +491,9 @@
</div>
</BaseCard>

<BaseCard v-if="showAttachments">
<BaseCard v-if="showAttachments" collapsable>
<template #title> Attachments </template>
<DetailsSection :details="attachmentDetails">
<DetailsSection v-if="attachmentDetails.length > 0" :details="attachmentDetails">
<template #manuals>
<ItemAttachmentsList
v-if="attachments.manuals.length > 0"
Expand Down Expand Up @@ -501,19 +523,22 @@
/>
</template>
</DetailsSection>
<div v-else>
<p class="text-base-content/70 px-6 pb-4">No attachments found</p>
</div>
</BaseCard>

<BaseCard v-if="showPurchase">
<BaseCard v-if="showPurchase" collapsable>
<template #title> Purchase Details </template>
<DetailsSection :details="purchaseDetails" />
</BaseCard>

<BaseCard v-if="showWarranty">
<BaseCard v-if="showWarranty" collapsable>
<template #title> Warranty Details </template>
<DetailsSection :details="warrantyDetails" />
</BaseCard>

<BaseCard v-if="showSold">
<BaseCard v-if="showSold" collapsable>
<template #title> Sold Details </template>
<DetailsSection :details="soldDetails" />
</BaseCard>
Expand Down

0 comments on commit 40e76ba

Please sign in to comment.