Replaced custom reducers with ramda's countBy

This commit is contained in:
Alejandro Celaya 2021-03-28 16:27:31 +02:00
parent 859cd9e5e3
commit 6d887ec4a8
2 changed files with 7 additions and 22 deletions

View file

@ -1,4 +1,4 @@
import { isEmpty, propEq, values } from 'ramda';
import { countBy, isEmpty, prop, propEq, values } from 'ramda';
import { useState, useEffect, useMemo, FC } from 'react';
import { Button, Card, Nav, NavLink, Progress, Row } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
@ -51,18 +51,9 @@ const sections: Record<Section, VisitsNavLinkProps> = {
list: { title: 'List', subPath: '/list', icon: faList },
};
const highlightedVisitsToStats = (
highlightedVisits: NormalizedVisit[],
prop: HighlightableProps,
): Stats => highlightedVisits.reduce<Stats>((acc, highlightedVisit) => {
if (!acc[highlightedVisit[prop]]) {
acc[highlightedVisit[prop]] = 0;
}
const highlightedVisitsToStats = (highlightedVisits: NormalizedVisit[], property: HighlightableProps): Stats =>
countBy(prop(property), highlightedVisits);
acc[highlightedVisit[prop]] += 1;
return acc;
}, {});
let selectedBar: string | undefined;
const VisitsNavLink: FC<VisitsNavLinkProps & { to: string }> = ({ subPath, title, icon, to }) => (

View file

@ -9,7 +9,7 @@ import {
DropdownItem,
} from 'reactstrap';
import { Line } from 'react-chartjs-2';
import { always, cond, reverse } from 'ramda';
import { always, cond, countBy, reverse } from 'ramda';
import moment from 'moment';
import Chart, { ChartData, ChartDataSets, ChartOptions } from 'chart.js';
import { NormalizedVisit, Stats } from '../types';
@ -70,15 +70,9 @@ const determineInitialStep = (oldestVisitDate: string): Step => {
return matcher() ?? 'monthly';
};
const groupVisitsByStep = (step: Step, visits: NormalizedVisit[]): Stats => visits.reduce<Stats>(
(acc, visit) => {
const key = STEP_TO_DATE_FORMAT[step](visit.date);
acc[key] = (acc[key] || 0) + 1;
return acc;
},
{},
const groupVisitsByStep = (step: Step, visits: NormalizedVisit[]): Stats => countBy(
(visit) => STEP_TO_DATE_FORMAT[step](visit.date),
visits,
);
const visitsToDatasetGroups = (step: Step, visits: NormalizedVisit[]) =>