update useBill reset value and calculator sum generic function

This commit is contained in:
2026-05-09 16:19:40 +05:30
parent 62e6759047
commit f99685d69a
2 changed files with 9 additions and 5 deletions
+2 -2
View File
@@ -58,8 +58,8 @@ export function useBill() {
title: "New Bill", title: "New Bill",
currency: Currency.INR, currency: Currency.INR,
createdAt: new Date(), createdAt: new Date(),
taxPercent: 18, taxPercent: 0,
taxType: TaxType.GST, taxType: TaxType.None,
items: [], items: [],
}); });
}, []); }, []);
+7 -3
View File
@@ -14,12 +14,16 @@ export function computeItem(item: BillItem): ComputedBillItem {
}; };
} }
export function sumBy<T>(items: T[], getValue: (item: T) => number): number {
return items.reduce((sum, currentItem) => sum + getValue(currentItem), 0);
}
export function computeBill(bill: Bill): ComputedBill { export function computeBill(bill: Bill): ComputedBill {
const computedItems = bill.items.map(computeItem); const computedItems = bill.items.map(computeItem);
const subtotal = computedItems.reduce( const subtotal = sumBy(
(sum, item) => sum + item.finalPrice, computedItems,
0, (computedItem) => computedItem.finalPrice,
); );
const taxAmount = (subtotal * bill.taxPercent) / 100; const taxAmount = (subtotal * bill.taxPercent) / 100;