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

150 lines
4.8 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';
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
2018-10-29 00:54:08 +03:00
import SortingDropdown from '../utils/SortingDropdown';
import PaginationDropdown from '../utils/PaginationDropdown';
import { rangeOf, roundTen } from '../utils/utils';
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,
title: PropTypes.string.isRequired,
sortingItems: PropTypes.object.isRequired,
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,
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 11:54:40 +03:00
this.redrawChart = false;
return prev;
}
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;
2018-10-29 00:54:08 +03:00
return (
<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);
const filterLocations = (locations) => {
const validCities = keys(currentPageStats);
return locations.filter(({ cityName }) => validCities.includes(cityName));
};
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
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) => {
2019-03-10 11:54:40 +03:00
this.redrawChart = true;
this.setState({ itemsPerPage, currentPage: 1 });
}}
/>
</div>
)}
{extraHeaderContent && (
<div className="float-right">
{extraHeaderContent(pagination ? filterLocations : undefined)}
</div>
)}
</React.Fragment>
);
return (
<GraphCard
isBarChart
title={computedTitle}
stats={currentPageStats}
footer={pagination}
max={max}
2019-03-10 11:54:40 +03:00
redraw={this.doRedrawChart()}
/>
2018-10-29 00:54:08 +03:00
);
}
}