2020-05-31 12:36:56 +03:00
|
|
|
import React, { useState } from 'react';
|
2018-10-29 00:54:08 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-04-10 15:38:31 +03:00
|
|
|
import { fromPairs, head, keys, pipe, prop, reverse, sortBy, splitEvery, toLower, toPairs, type, zipObj } from 'ramda';
|
2020-05-31 18:51:52 +03:00
|
|
|
import SortingDropdown from '../../utils/SortingDropdown';
|
|
|
|
import PaginationDropdown from '../../utils/PaginationDropdown';
|
|
|
|
import { rangeOf } from '../../utils/utils';
|
|
|
|
import { roundTen } from '../../utils/helpers/numbers';
|
|
|
|
import SimplePaginator from '../../common/SimplePaginator';
|
2018-10-29 00:54:08 +03:00
|
|
|
import GraphCard from './GraphCard';
|
2019-01-07 15:35:14 +03:00
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
const propTypes = {
|
|
|
|
stats: PropTypes.object.isRequired,
|
|
|
|
highlightedStats: PropTypes.object,
|
|
|
|
highlightedLabel: PropTypes.string,
|
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
sortingItems: PropTypes.object.isRequired,
|
|
|
|
extraHeaderContent: PropTypes.func,
|
|
|
|
withPagination: PropTypes.bool,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2019-01-09 22:34:35 +03:00
|
|
|
const toLowerIfString = (value) => type(value) === 'String' ? toLower(value) : value;
|
2020-04-10 15:38:31 +03:00
|
|
|
const pickKeyFromPair = ([ key ]) => key;
|
2019-03-10 10:28:14 +03:00
|
|
|
const pickValueFromPair = ([ , value ]) => value;
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
const SortableBarGraph = ({
|
|
|
|
stats,
|
|
|
|
highlightedStats,
|
|
|
|
title,
|
|
|
|
sortingItems,
|
|
|
|
extraHeaderContent,
|
|
|
|
withPagination = true,
|
|
|
|
...rest
|
|
|
|
}) => {
|
|
|
|
const [ order, setOrder ] = useState({
|
2018-10-29 00:54:08 +03:00
|
|
|
orderField: undefined,
|
|
|
|
orderDir: undefined,
|
2020-05-31 12:36:56 +03:00
|
|
|
});
|
|
|
|
const [ currentPage, setCurrentPage ] = useState(1);
|
|
|
|
const [ itemsPerPage, setItemsPerPage ] = useState(50);
|
2019-03-10 10:28:14 +03:00
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
const getSortedPairsForStats = (stats, sortingItems) => {
|
2019-03-10 11:54:40 +03:00
|
|
|
const pairs = toPairs(stats);
|
2020-05-31 12:36:56 +03:00
|
|
|
const sortedPairs = !order.orderField ? pairs : sortBy(
|
2019-03-10 10:28:14 +03:00
|
|
|
pipe(
|
2020-05-31 12:36:56 +03:00
|
|
|
prop(order.orderField === head(keys(sortingItems)) ? 0 : 1),
|
2019-03-10 10:28:14 +03:00
|
|
|
toLowerIfString
|
|
|
|
),
|
2019-03-10 11:54:40 +03:00
|
|
|
pairs
|
2019-03-10 10:28:14 +03:00
|
|
|
);
|
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
return !order.orderDir || order.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs);
|
|
|
|
};
|
|
|
|
const determineStats = (stats, highlightedStats, sortingItems) => {
|
|
|
|
const sortedPairs = getSortedPairsForStats(stats, sortingItems);
|
2020-04-10 15:38:31 +03:00
|
|
|
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 }
|
|
|
|
);
|
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
if (sortedPairs.length <= itemsPerPage) {
|
2020-04-10 15:38:31 +03:00
|
|
|
return {
|
|
|
|
currentPageStats: fromPairs(sortedPairs),
|
|
|
|
currentPageHighlightedStats: sortedHighlightedPairs && fromPairs(sortedHighlightedPairs),
|
|
|
|
};
|
2019-03-10 10:28:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
const pages = splitEvery(itemsPerPage, sortedPairs);
|
|
|
|
const highlightedPages = sortedHighlightedPairs && splitEvery(itemsPerPage, sortedHighlightedPairs);
|
2019-03-10 10:28:14 +03:00
|
|
|
|
|
|
|
return {
|
2020-05-31 12:36:56 +03:00
|
|
|
currentPageStats: fromPairs(determineCurrentPagePairs(pages)),
|
|
|
|
currentPageHighlightedStats: highlightedPages && fromPairs(determineCurrentPagePairs(highlightedPages)),
|
|
|
|
pagination: renderPagination(pages.length),
|
|
|
|
max: roundTen(Math.max(...sortedPairs.map(pickValueFromPair))),
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
2020-05-31 12:36:56 +03:00
|
|
|
};
|
|
|
|
const determineCurrentPagePairs = (pages) => {
|
|
|
|
const page = pages[currentPage - 1];
|
2019-03-10 10:28:14 +03:00
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
if (currentPage < pages.length) {
|
2019-03-10 10:28:14 +03:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstPageLength = pages[0].length;
|
|
|
|
|
|
|
|
// Using the "hidden" key, the chart will just replace the label by an empty string
|
|
|
|
return [ ...page, ...rangeOf(firstPageLength - page.length, (i) => [ `hidden_${i}`, 0 ]) ];
|
2020-05-31 12:36:56 +03:00
|
|
|
};
|
|
|
|
const renderPagination = (pagesCount) =>
|
|
|
|
<SimplePaginator currentPage={currentPage} pagesCount={pagesCount} setCurrentPage={setCurrentPage} />;
|
|
|
|
|
|
|
|
const { currentPageStats, currentPageHighlightedStats, pagination, max } = determineStats(
|
|
|
|
stats,
|
|
|
|
highlightedStats && keys(highlightedStats).length > 0 ? highlightedStats : undefined,
|
|
|
|
sortingItems
|
|
|
|
);
|
|
|
|
const activeCities = keys(currentPageStats);
|
|
|
|
const computeTitle = () => (
|
|
|
|
<React.Fragment>
|
|
|
|
{title}
|
|
|
|
<div className="float-right">
|
|
|
|
<SortingDropdown
|
|
|
|
isButton={false}
|
|
|
|
right
|
|
|
|
items={sortingItems}
|
|
|
|
orderField={order.orderField}
|
|
|
|
orderDir={order.orderDir}
|
|
|
|
onChange={(orderField, orderDir) => setOrder({ orderField, orderDir }) || setCurrentPage(1)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{withPagination && keys(stats).length > 50 && (
|
2018-10-29 00:54:08 +03:00
|
|
|
<div className="float-right">
|
2020-05-31 12:36:56 +03:00
|
|
|
<PaginationDropdown
|
|
|
|
toggleClassName="btn-sm p-0 mr-3"
|
|
|
|
ranges={[ 50, 100, 200, 500 ]}
|
|
|
|
value={itemsPerPage}
|
|
|
|
setValue={(itemsPerPage) => setItemsPerPage(itemsPerPage) || setCurrentPage(1)}
|
2018-10-29 00:54:08 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2020-05-31 12:36:56 +03:00
|
|
|
)}
|
|
|
|
{extraHeaderContent && (
|
|
|
|
<div className="float-right">
|
|
|
|
{extraHeaderContent(pagination ? activeCities : undefined)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GraphCard
|
|
|
|
isBarChart
|
|
|
|
title={computeTitle}
|
|
|
|
stats={currentPageStats}
|
|
|
|
highlightedStats={currentPageHighlightedStats}
|
|
|
|
footer={pagination}
|
|
|
|
max={max}
|
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SortableBarGraph.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default SortableBarGraph;
|