Replaced if/else by functional matcher

This commit is contained in:
Alejandro Celaya 2020-05-31 20:16:15 +02:00
parent 32cc1cc580
commit 73e55cc742

View file

@ -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) => {