2018-10-29 00:54:08 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-03-10 10:28:14 +03:00
|
|
|
import { fromPairs, head, keys, pipe, prop, reverse, sortBy, splitEvery, toLower, toPairs, type } from 'ramda';
|
2018-10-29 00:54:08 +03:00
|
|
|
import SortingDropdown from '../utils/SortingDropdown';
|
2019-03-10 10:28:14 +03:00
|
|
|
import PaginationDropdown from '../utils/PaginationDropdown';
|
2020-04-04 11:36:34 +03:00
|
|
|
import { rangeOf } from '../utils/utils';
|
|
|
|
import { roundTen } from '../utils/helpers/numbers';
|
2019-09-21 19:29:58 +03:00
|
|
|
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
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
const { max } = Math;
|
2019-01-09 22:34:35 +03:00
|
|
|
const toLowerIfString = (value) => type(value) === 'String' ? toLower(value) : value;
|
2019-03-10 10:28:14 +03:00
|
|
|
const pickValueFromPair = ([ , value ]) => value;
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2018-10-29 01:04:52 +03:00
|
|
|
export default class SortableBarGraph extends React.Component {
|
2018-10-29 00:54:08 +03:00
|
|
|
static propTypes = {
|
2018-10-29 01:04:52 +03:00
|
|
|
stats: PropTypes.object.isRequired,
|
2020-04-04 21:16:20 +03:00
|
|
|
highlightedStats: PropTypes.object,
|
2018-10-29 01:04:52 +03:00
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
sortingItems: PropTypes.object.isRequired,
|
2019-03-10 12:56:36 +03:00
|
|
|
extraHeaderContent: PropTypes.func,
|
2019-03-10 11:54:40 +03:00
|
|
|
withPagination: PropTypes.bool,
|
2020-04-10 12:59:53 +03:00
|
|
|
onClick: PropTypes.func,
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
orderField: undefined,
|
|
|
|
orderDir: undefined,
|
2019-03-10 10:28:14 +03:00
|
|
|
currentPage: 1,
|
|
|
|
itemsPerPage: Infinity,
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
2019-03-10 10:28:14 +03:00
|
|
|
|
|
|
|
determineStats(stats, sortingItems) {
|
2019-03-10 11:54:40 +03:00
|
|
|
const pairs = toPairs(stats);
|
|
|
|
const sortedPairs = !this.state.orderField ? pairs : sortBy(
|
2019-03-10 10:28:14 +03:00
|
|
|
pipe(
|
|
|
|
prop(this.state.orderField === head(keys(sortingItems)) ? 0 : 1),
|
|
|
|
toLowerIfString
|
|
|
|
),
|
2019-03-10 11:54:40 +03:00
|
|
|
pairs
|
2019-03-10 10:28:14 +03:00
|
|
|
);
|
|
|
|
const directionalPairs = !this.state.orderDir || this.state.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs);
|
|
|
|
|
|
|
|
if (directionalPairs.length <= this.state.itemsPerPage) {
|
|
|
|
return { currentPageStats: fromPairs(directionalPairs) };
|
|
|
|
}
|
|
|
|
|
|
|
|
const pages = splitEvery(this.state.itemsPerPage, directionalPairs);
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentPageStats: fromPairs(this.determineCurrentPagePairs(pages)),
|
|
|
|
pagination: this.renderPagination(pages.length),
|
|
|
|
max: roundTen(max(...directionalPairs.map(pickValueFromPair))),
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
2019-03-10 10:28:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
determineCurrentPagePairs(pages) {
|
|
|
|
const page = pages[this.state.currentPage - 1];
|
|
|
|
|
|
|
|
if (this.state.currentPage < pages.length) {
|
|
|
|
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 ]) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPagination(pagesCount) {
|
|
|
|
const { currentPage } = this.state;
|
2019-09-22 11:41:31 +03:00
|
|
|
const setCurrentPage = (currentPage) => this.setState({ currentPage });
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2019-09-21 19:29:58 +03:00
|
|
|
return <SimplePaginator currentPage={currentPage} pagesCount={pagesCount} setCurrentPage={setCurrentPage} />;
|
2019-03-10 10:28:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-04-10 12:59:53 +03:00
|
|
|
const { stats, sortingItems, title, extraHeaderContent, withPagination = true, ...rest } = this.props;
|
2019-03-10 11:54:40 +03:00
|
|
|
const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems);
|
2019-03-10 14:09:54 +03:00
|
|
|
const activeCities = keys(currentPageStats);
|
2019-03-10 19:55:02 +03:00
|
|
|
const computeTitle = () => (
|
2019-03-10 10:28:14 +03:00
|
|
|
<React.Fragment>
|
2018-10-29 01:04:52 +03:00
|
|
|
{title}
|
2018-10-29 00:54:08 +03:00
|
|
|
<div className="float-right">
|
|
|
|
<SortingDropdown
|
|
|
|
isButton={false}
|
|
|
|
right
|
2019-03-10 10:28:14 +03:00
|
|
|
items={sortingItems}
|
2018-10-29 00:54:08 +03:00
|
|
|
orderField={this.state.orderField}
|
|
|
|
orderDir={this.state.orderDir}
|
2019-03-10 10:28:14 +03:00
|
|
|
onChange={(orderField, orderDir) => this.setState({ orderField, orderDir, currentPage: 1 })}
|
2018-10-29 00:54:08 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2019-03-10 11:54:40 +03:00
|
|
|
{withPagination && keys(stats).length > 50 && (
|
2019-03-10 10:28:14 +03:00
|
|
|
<div className="float-right">
|
|
|
|
<PaginationDropdown
|
|
|
|
toggleClassName="btn-sm paddingless mr-3"
|
|
|
|
ranges={[ 50, 100, 200, 500 ]}
|
|
|
|
value={this.state.itemsPerPage}
|
2019-03-16 11:02:10 +03:00
|
|
|
setValue={(itemsPerPage) => this.setState({ itemsPerPage, currentPage: 1 })}
|
2019-03-10 10:28:14 +03:00
|
|
|
/>
|
2019-01-07 21:43:25 +03:00
|
|
|
</div>
|
2019-03-10 10:28:14 +03:00
|
|
|
)}
|
2019-03-10 12:56:36 +03:00
|
|
|
{extraHeaderContent && (
|
|
|
|
<div className="float-right">
|
2019-03-10 14:09:54 +03:00
|
|
|
{extraHeaderContent(pagination ? activeCities : undefined)}
|
2019-03-10 12:56:36 +03:00
|
|
|
</div>
|
|
|
|
)}
|
2019-03-10 10:28:14 +03:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
2020-04-04 21:16:20 +03:00
|
|
|
return (
|
|
|
|
<GraphCard
|
|
|
|
isBarChart
|
|
|
|
title={computeTitle}
|
|
|
|
stats={currentPageStats}
|
|
|
|
footer={pagination}
|
|
|
|
max={max}
|
2020-04-10 12:59:53 +03:00
|
|
|
{...rest}
|
2020-04-04 21:16:20 +03:00
|
|
|
/>
|
|
|
|
);
|
2018-10-29 00:54:08 +03:00
|
|
|
}
|
|
|
|
}
|