Skip to content

Commit

Permalink
Fix insights aggregation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ystxn committed Oct 3, 2023
1 parent bb0e235 commit 974ceac
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/dashboard/insights.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ 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;
}, Object.create(null));
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);
Expand Down

0 comments on commit 974ceac

Please sign in to comment.