From 974ceac3be4e22b46c4f5f11e3c4ddb522702cb0 Mon Sep 17 00:00:00 2001 From: Yong Sheng Tan Date: Tue, 3 Oct 2023 14:04:31 +0800 Subject: [PATCH] Fix insights aggregation bug --- src/dashboard/insights.jsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/dashboard/insights.jsx b/src/dashboard/insights.jsx index c8ba862..21e4374 100644 --- a/src/dashboard/insights.jsx +++ b/src/dashboard/insights.jsx @@ -47,10 +47,8 @@ const Insights = () => { const [ breakdown, setBreakdown ] = useState(false); useEffect(() => getInsights((response) => { - const keys = [ 'sum', 'transactions' ]; - const summary = response.summary - .map((s) => ({ ...s, sum: s.average * s.transactions })) - .reduce((acc, curr) => { + const keys = [ 'average', 'transactions' ]; + const summary = response.summary.reduce((acc, curr) => { acc[curr.category] = acc[curr.category] || Object.assign(...keys.map(k => ({ [k]: 0 }))); keys.forEach(k => acc[curr.category][k] += curr[k]); return acc; @@ -58,7 +56,7 @@ const Insights = () => { const summaryList = Object.keys(summary).map((category) => ({ category, transactions: summary[category].transactions, - average: summary[category].sum / summary[category].transactions, + average: summary[category].average, })); setCategorySummary(summaryList); setInsights(response);