Ensured highlighted stats are properly sorted and paginated on charts that support that

This commit is contained in:
Alejandro Celaya 2020-04-10 14:38:31 +02:00
parent 9d1e48ee90
commit fafe920b7b

View file

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { fromPairs, head, keys, pipe, prop, reverse, sortBy, splitEvery, toLower, toPairs, type } from 'ramda'; import { fromPairs, head, keys, pipe, prop, reverse, sortBy, splitEvery, toLower, toPairs, type, zipObj } from 'ramda';
import SortingDropdown from '../utils/SortingDropdown'; import SortingDropdown from '../utils/SortingDropdown';
import PaginationDropdown from '../utils/PaginationDropdown'; import PaginationDropdown from '../utils/PaginationDropdown';
import { rangeOf } from '../utils/utils'; import { rangeOf } from '../utils/utils';
@ -10,6 +10,7 @@ import GraphCard from './GraphCard';
const { max } = Math; const { max } = Math;
const toLowerIfString = (value) => type(value) === 'String' ? toLower(value) : value; const toLowerIfString = (value) => type(value) === 'String' ? toLower(value) : value;
const pickKeyFromPair = ([ key ]) => key;
const pickValueFromPair = ([ , value ]) => value; const pickValueFromPair = ([ , value ]) => value;
export default class SortableBarGraph extends React.Component { export default class SortableBarGraph extends React.Component {
@ -30,7 +31,7 @@ export default class SortableBarGraph extends React.Component {
itemsPerPage: Infinity, itemsPerPage: Infinity,
}; };
determineStats(stats, sortingItems) { getSortedPairsForStats(stats, sortingItems) {
const pairs = toPairs(stats); const pairs = toPairs(stats);
const sortedPairs = !this.state.orderField ? pairs : sortBy( const sortedPairs = !this.state.orderField ? pairs : sortBy(
pipe( pipe(
@ -39,18 +40,33 @@ export default class SortableBarGraph extends React.Component {
), ),
pairs pairs
); );
const directionalPairs = !this.state.orderDir || this.state.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs);
if (directionalPairs.length <= this.state.itemsPerPage) { return !this.state.orderDir || this.state.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs);
return { currentPageStats: fromPairs(directionalPairs) }; }
determineStats(stats, highlightedStats, sortingItems) {
const sortedPairs = this.getSortedPairsForStats(stats, sortingItems);
const sortedKeys = sortedPairs.map(pickKeyFromPair);
// The highlighted stats have to be ordered based on the regular stats, not on its own values
const sortedHighlightedPairs = highlightedStats && toPairs(
{ ...zipObj(sortedKeys, sortedKeys.map(() => 0)), ...highlightedStats }
);
if (sortedPairs.length <= this.state.itemsPerPage) {
return {
currentPageStats: fromPairs(sortedPairs),
currentPageHighlightedStats: sortedHighlightedPairs && fromPairs(sortedHighlightedPairs),
};
} }
const pages = splitEvery(this.state.itemsPerPage, directionalPairs); const pages = splitEvery(this.state.itemsPerPage, sortedPairs);
const highlightedPages = sortedHighlightedPairs && splitEvery(this.state.itemsPerPage, sortedHighlightedPairs);
return { return {
currentPageStats: fromPairs(this.determineCurrentPagePairs(pages)), currentPageStats: fromPairs(this.determineCurrentPagePairs(pages)),
currentPageHighlightedStats: highlightedPages && fromPairs(this.determineCurrentPagePairs(highlightedPages)),
pagination: this.renderPagination(pages.length), pagination: this.renderPagination(pages.length),
max: roundTen(max(...directionalPairs.map(pickValueFromPair))), max: roundTen(max(...sortedPairs.map(pickValueFromPair))),
}; };
} }
@ -75,8 +91,20 @@ export default class SortableBarGraph extends React.Component {
} }
render() { render() {
const { stats, sortingItems, title, extraHeaderContent, withPagination = true, ...rest } = this.props; const {
const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems); stats,
highlightedStats,
sortingItems,
title,
extraHeaderContent,
withPagination = true,
...rest
} = this.props;
const { currentPageStats, currentPageHighlightedStats, pagination, max } = this.determineStats(
stats,
highlightedStats && keys(highlightedStats).length > 0 ? highlightedStats : undefined,
sortingItems
);
const activeCities = keys(currentPageStats); const activeCities = keys(currentPageStats);
const computeTitle = () => ( const computeTitle = () => (
<React.Fragment> <React.Fragment>
@ -114,6 +142,7 @@ export default class SortableBarGraph extends React.Component {
isBarChart isBarChart
title={computeTitle} title={computeTitle}
stats={currentPageStats} stats={currentPageStats}
highlightedStats={currentPageHighlightedStats}
footer={pagination} footer={pagination}
max={max} max={max}
{...rest} {...rest}