Skip to content

Commit

Permalink
fix(monthStats): fix week aggregation computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Clm-Roig committed Apr 8, 2022
1 parent af330b5 commit fa2d999
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/components/charts/MonthChart/formatMonthData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { addWeeks, getDaysInMonth, getWeeksInMonth, isSameWeek, startOfMonth } from 'date-fns';
import {
addWeeks,
getDay,
getDaysInMonth,
getWeeksInMonth,
isSameWeek,
startOfMonth
} from 'date-fns';
import TrackerEntry from '../../../models/TrackerEntry';
import { getAggregatedCompletions } from '../../../store/trackers/utils';
import { DataType } from './types';
Expand All @@ -9,10 +16,14 @@ const formatData = (monthDate: Date, entries: TrackerEntry[]): DataType[] => {
const startDay = startOfMonth(monthDate);
for (let i = 0; i < nbOfWeeks; i += 1) {
const week = addWeeks(startDay, i);
const weekBeginDayNumber = i === 0 ? 1 : i * 7 + 1;
const weekEndDayNumber = Math.min(i * 7 + 7, getDaysInMonth(startDay));
const weekData: DataType = {
name: (i === 0 ? 1 : i * 7 + 1) + '-' + Math.min(i * 7 + 8, getDaysInMonth(startDay))
name: weekBeginDayNumber + '-' + weekEndDayNumber
};
const weekEntries = entries.filter((e) => isSameWeek(new Date(e.date), week));
const weekEntries = entries.filter((e) => isSameWeek(new Date(e.date), week), {
weekStartsOn: getDay(new Date(week))
});
const aggCompletions = getAggregatedCompletions(weekEntries);
aggCompletions.forEach((c) => {
weekData[c.unit] = c.quantity;
Expand Down

0 comments on commit fa2d999

Please sign in to comment.