From 6d887ec4a8f6979ef6d7aa90860ef6cdd0e92848 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 28 Mar 2021 16:27:31 +0200 Subject: [PATCH] Replaced custom reducers with ramda's countBy --- src/visits/VisitsStats.tsx | 15 +++------------ src/visits/helpers/LineChartCard.tsx | 14 ++++---------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/visits/VisitsStats.tsx b/src/visits/VisitsStats.tsx index 73e6d533..57c10e4b 100644 --- a/src/visits/VisitsStats.tsx +++ b/src/visits/VisitsStats.tsx @@ -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 = { list: { title: 'List', subPath: '/list', icon: faList }, }; -const highlightedVisitsToStats = ( - highlightedVisits: NormalizedVisit[], - prop: HighlightableProps, -): Stats => highlightedVisits.reduce((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 = ({ subPath, title, icon, to }) => ( diff --git a/src/visits/helpers/LineChartCard.tsx b/src/visits/helpers/LineChartCard.tsx index 2190b4bf..cb180dca 100644 --- a/src/visits/helpers/LineChartCard.tsx +++ b/src/visits/helpers/LineChartCard.tsx @@ -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( - (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[]) =>