Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Add total price indicator to labels and locations #638

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/lib/api/types/data-contracts.ts
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is auto-generated you shouldn't manually update it

Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export interface LabelOut {
id: string;
name: string;
updatedAt: Date | string;
totalPrice: number | undefined;
}

export interface LabelSummary {
Expand All @@ -217,6 +218,7 @@ export interface LocationOut {
name: string;
parent: LocationSummary;
updatedAt: Date | string;
totalPrice: number | undefined;
}

export interface LocationOutCount {
Expand Down
15 changes: 14 additions & 1 deletion frontend/pages/label/[id].vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
import Currency from "~~/components/global/Currency.vue";

definePageMeta({
middleware: ["auth"],
});
Expand Down Expand Up @@ -84,6 +86,8 @@
return [];
}

label.value.totalPrice = resp.data.items.map(item => Number(item.purchasePrice)).reduce((a, b) => a + b, 0);

return resp.data.items;
});
</script>
Expand Down Expand Up @@ -111,8 +115,17 @@
</div>
</div>
<div>
<h1 class="text-2xl pb-1">
<h1 class="text-2xl pb-1 flex items-center gap-3">
{{ label ? label.name : "" }}

<div
v-if="label && label.totalPrice"
class="text-xs bg-secondary text-secondary-content rounded-full px-2 py-1"
>
<div>
<Currency :amount="label.totalPrice" />
</div>
</div>
</h1>
<div class="flex gap-1 flex-wrap text-xs">
<div>
Expand Down
66 changes: 62 additions & 4 deletions frontend/pages/location/[id].vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { LocationSummary, LocationUpdate } from "~~/lib/api/types/data-contracts";
import { LocationSummary, LocationUpdate, TreeItem } from "~~/lib/api/types/data-contracts";
import { useLocationStore } from "~~/stores/locations";
import Currency from "~~/components/global/Currency.vue";

definePageMeta({
middleware: ["auth"],
Expand Down Expand Up @@ -89,16 +90,64 @@
return [];
}

const locationTreeResp = await api.locations.getTree();

if (locationTreeResp.error) {
toast.error("Failed to load items");
return [];
}

const fullLocationTree: TreeItem[] = locationTreeResp.data;

function flattenLocation(location: TreeItem): string[] {
return location.children.reduce((acc, child) => [...acc, ...flattenLocation(child)], [location.id]);
}

function findObjectById(locations: TreeItem[], targetId: string): TreeItem | undefined {
if (!locations || !Array.isArray(locations)) {
return undefined;
}

const target = locations.find(location => location.id === targetId);
if (target) return target;

for (let i = 0; i < locations.length; i++) {
const result = findObjectById(locations[i].children, targetId);
if (result) return result;
}

return undefined;
}

const locationTree: TreeItem | undefined = findObjectById(fullLocationTree, location.value.id);

if (!locationTree) {
toast.error("Failed to get tree for current location");

return [];
}

const allChildLocations = flattenLocation(locationTree);

const resp = await api.items.getAll({
locations: [location.value.id],
locations: allChildLocations,
});

if (resp.error) {
toast.error("Failed to load items");
return [];
}

return resp.data.items;
const locationItems = resp.data.items.filter(item => {
if (item.location && location.value) return item?.location.id === location?.value.id;
return false;
});

const totalPrice = resp.data.items.map(item => Number(item.purchasePrice)).reduce((a, b) => a + b, 0);

location.value.totalPrice = totalPrice;

return locationItems;
});
</script>

Expand Down Expand Up @@ -135,8 +184,17 @@
<li>{{ location.name }}</li>
</ul>
</div>
<h1 class="text-2xl pb-1">
<h1 class="text-2xl pb-1 flex items-center gap-3">
{{ location ? location.name : "" }}

<div
v-if="location && location.totalPrice"
class="text-xs bg-secondary text-secondary-content rounded-full px-2 py-1"
>
<div>
<Currency :amount="location.totalPrice" />
</div>
</div>
</h1>
<div class="flex gap-1 flex-wrap text-xs">
<div>
Expand Down