diff --git a/src/visits/helpers/LineChartCard.js b/src/visits/helpers/LineChartCard.js index 85c26999..d3f3593e 100644 --- a/src/visits/helpers/LineChartCard.js +++ b/src/visits/helpers/LineChartCard.js @@ -10,7 +10,7 @@ import { DropdownItem, } from 'reactstrap'; import { Line } from 'react-chartjs-2'; -import { reverse } from 'ramda'; +import { always, cond, reverse } from 'ramda'; import moment from 'moment'; import { VisitType } from '../types'; import { fillTheGaps } from '../../utils/helpers/visits'; @@ -55,17 +55,13 @@ const STEP_TO_DATE_FORMAT = { const determineInitialStep = (oldestVisitDate) => { const now = moment(); const oldestDate = moment(oldestVisitDate); + const matcher = cond([ + [ () => now.diff(oldestDate, 'day') <= 2, always('hourly') ], // Less than 2 days + [ () => now.diff(oldestDate, 'month') <= 1, always('daily') ], // Between 2 days and 1 month + [ () => now.diff(oldestDate, 'month') <= 6, always('weekly') ], // Between 1 and 6 months + ]); - if (now.diff(oldestDate, 'day') <= 2) { // Less than 2 days - return 'hourly'; - } else if (now.diff(oldestDate, 'month') <= 1) { // Between 2 days and 1 month - return 'daily'; - } else if (now.diff(oldestDate, 'month') <= 6) { // Between 1 and 6 months - return 'weekly'; - } - - // Older than 6 months - return 'monthly'; + return matcher() || 'monthly'; }; const groupVisitsByStep = (step, visits) => visits.reduce((acc, visit) => {