shlink-web-client/src/visits/SortableBarGraph.js

124 lines
4 KiB
JavaScript
Raw Normal View History

2018-10-29 00:54:08 +03:00
import React from 'react';
import PropTypes from 'prop-types';
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';
import PaginationDropdown from '../utils/PaginationDropdown';
2020-04-04 11:36:34 +03:00
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';
const { max } = Math;
2019-01-09 22:34:35 +03:00
const toLowerIfString = (value) => type(value) === 'String' ? toLower(value) : value;
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,
highlightedStats: PropTypes.object,
2018-10-29 01:04:52 +03:00
title: PropTypes.string.isRequired,
sortingItems: PropTypes.object.isRequired,
extraHeaderContent: PropTypes.func,
2019-03-10 11:54:40 +03:00
withPagination: PropTypes.bool,
onClick: PropTypes.func,
2018-10-29 00:54:08 +03:00
};
state = {
orderField: undefined,
orderDir: undefined,
currentPage: 1,
itemsPerPage: Infinity,
2018-10-29 00:54:08 +03:00
};
determineStats(stats, sortingItems) {
2019-03-10 11:54:40 +03:00
const pairs = toPairs(stats);
const sortedPairs = !this.state.orderField ? pairs : sortBy(
pipe(
prop(this.state.orderField === head(keys(sortingItems)) ? 0 : 1),
toLowerIfString
),
2019-03-10 11:54:40 +03:00
pairs
);
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
};
}
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;
const setCurrentPage = (currentPage) => this.setState({ currentPage });
2018-10-29 00:54:08 +03:00
return <SimplePaginator currentPage={currentPage} pagesCount={pagesCount} setCurrentPage={setCurrentPage} />;
}
render() {
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);
const activeCities = keys(currentPageStats);
2019-03-10 19:55:02 +03:00
const computeTitle = () => (
<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
items={sortingItems}
2018-10-29 00:54:08 +03:00
orderField={this.state.orderField}
orderDir={this.state.orderDir}
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 && (
<div className="float-right">
<PaginationDropdown
toggleClassName="btn-sm paddingless mr-3"
ranges={[ 50, 100, 200, 500 ]}
value={this.state.itemsPerPage}
setValue={(itemsPerPage) => this.setState({ itemsPerPage, currentPage: 1 })}
/>
</div>
)}
{extraHeaderContent && (
<div className="float-right">
{extraHeaderContent(pagination ? activeCities : undefined)}
</div>
)}
</React.Fragment>
);
return (
<GraphCard
isBarChart
title={computeTitle}
stats={currentPageStats}
footer={pagination}
max={max}
{...rest}
/>
);
2018-10-29 00:54:08 +03:00
}
}