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

View file

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