Skip to content

Commit

Permalink
Add total row
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsingh-007 committed Nov 9, 2024
1 parent 9692d72 commit 751cfc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/app/dashboard-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DahsboardTable = () => {
{ enabled: !!user, refetchOnMount: true }
);

const monthCardsData = useMemo(
const { monthCardsData, total } = useMemo(
() => getMergedCardsData(cardsData, cardTransactions),
[cardsData, cardTransactions]
);
Expand Down Expand Up @@ -107,7 +107,7 @@ const DahsboardTable = () => {
<TableBody>
<TableRow>
<TableCell colSpan={4} className="p-0">
<Skeleton className="rounded-none h-32 sm:h-96" />
<Skeleton className="rounded-none h-60 sm:h-96" />
</TableCell>
</TableRow>
</TableBody>
Expand Down Expand Up @@ -152,6 +152,12 @@ const DahsboardTable = () => {
</TableRow>
);
})}
<TableRow className="bg-muted/50">
<TableCell className="font-bold py-3 pl-9" colSpan={2}>
Total
</TableCell>
<TableCell colSpan={2}> {getFormattedPrice(total)}</TableCell>
</TableRow>
</TableBody>
)}
</Table>
Expand Down
7 changes: 5 additions & 2 deletions src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getMergedCardsData = (
cardTransactions: Record<string, TCardTransaction> | undefined
) => {
if (!cardsData) {
return [];
return { monthCardsData: [], total: 0 };
}
const cardTransactionMap = cardTransactions
? Object.entries(cardTransactions).reduce<Map<string, TCardTransaction>>(
Expand All @@ -18,9 +18,11 @@ export const getMergedCardsData = (
)
: new Map<string, TCardTransaction>();

return Object.entries(cardsData)
let total = 0;
const monthCardsData = Object.entries(cardsData)
.map(([cardId, cardDetails]) => {
const transaction = cardTransactionMap.get(cardId);
total += transaction?.amount || 0;

return {
cardId: cardId,
Expand All @@ -31,6 +33,7 @@ export const getMergedCardsData = (
.sort(
(a, b) => a.cardDetails.cardBillingDate - b.cardDetails.cardBillingDate
);
return { monthCardsData, total };
};

export const getMergedTxnData = (
Expand Down

0 comments on commit 751cfc4

Please sign in to comment.