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

48 lines
1.3 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, prop, reverse, sortBy, toPairs } from 'ramda';
import SortingDropdown from '../utils/SortingDropdown';
import GraphCard from './GraphCard';
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,
2018-10-29 00:54:08 +03:00
};
state = {
orderField: undefined,
orderDir: undefined,
};
render() {
2018-10-29 01:04:52 +03:00
const { stats, sortingItems, title } = this.props;
2018-10-29 00:54:08 +03:00
const sortStats = () => {
if (!this.state.orderField) {
return stats;
}
2018-10-29 01:04:52 +03:00
const sortedPairs = sortBy(prop(this.state.orderField === head(keys(sortingItems)) ? 0 : 1), toPairs(stats));
2018-10-29 00:54:08 +03:00
return fromPairs(this.state.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs));
};
return (
<GraphCard stats={sortStats()} isBarChart>
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
orderField={this.state.orderField}
orderDir={this.state.orderDir}
2018-10-29 01:04:52 +03:00
items={sortingItems}
2018-10-29 00:54:08 +03:00
onChange={(orderField, orderDir) => this.setState({ orderField, orderDir })}
/>
</div>
</GraphCard>
);
}
}