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';
|
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
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';
|
|
|
|
import { rangeOf, roundTen } from '../utils/utils';
|
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,
|
|
|
|
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,
|
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 11:54:40 +03:00
|
|
|
redrawChart = false;
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2019-03-10 11:54:40 +03:00
|
|
|
doRedrawChart() {
|
|
|
|
const prev = this.redrawChart;
|
2019-03-10 10:28:14 +03:00
|
|
|
|
2019-03-10 11:54:40 +03:00
|
|
|
this.redrawChart = false;
|
2019-03-10 10:28:14 +03:00
|
|
|
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-10-29 00:54:08 +03:00
|
|
|
|
|
|
|
return (
|
2019-03-10 10:28:14 +03:00
|
|
|
<Pagination listClassName="flex-wrap mb-0">
|
|
|
|
<PaginationItem disabled={currentPage === 1}>
|
|
|
|
<PaginationLink previous tag="span" onClick={() => this.setState({ currentPage: currentPage - 1 })} />
|
|
|
|
</PaginationItem>
|
|
|
|
{rangeOf(pagesCount, (page) => (
|
|
|
|
<PaginationItem key={page} active={page === currentPage}>
|
|
|
|
<PaginationLink tag="span" onClick={() => this.setState({ currentPage: page })}>{page}</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
))}
|
|
|
|
<PaginationItem disabled={currentPage >= pagesCount}>
|
|
|
|
<PaginationLink next tag="span" onClick={() => this.setState({ currentPage: currentPage + 1 })} />
|
|
|
|
</PaginationItem>
|
|
|
|
</Pagination>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-03-10 11:54:40 +03:00
|
|
|
const { stats, sortingItems, title, extraHeaderContent, withPagination = true } = this.props;
|
|
|
|
const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems);
|
2019-03-10 14:09:54 +03:00
|
|
|
const activeCities = keys(currentPageStats);
|
2019-03-10 10:28:14 +03:00
|
|
|
const computedTitle = (
|
|
|
|
<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}
|
|
|
|
setValue={(itemsPerPage) => {
|
2019-03-10 11:54:40 +03:00
|
|
|
this.redrawChart = true;
|
2019-03-10 10:28:14 +03:00
|
|
|
this.setState({ itemsPerPage, currentPage: 1 });
|
|
|
|
}}
|
|
|
|
/>
|
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>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GraphCard
|
|
|
|
isBarChart
|
|
|
|
title={computedTitle}
|
|
|
|
stats={currentPageStats}
|
|
|
|
footer={pagination}
|
|
|
|
max={max}
|
2019-03-10 11:54:40 +03:00
|
|
|
redraw={this.doRedrawChart()}
|
2019-03-10 10:28:14 +03:00
|
|
|
/>
|
2018-10-29 00:54:08 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|