2018-10-29 00:54:08 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-01-09 22:34:35 +03:00
|
|
|
import { fromPairs, head, keys, pipe, prop, reverse, sortBy, toLower, toPairs, type } from 'ramda';
|
2018-10-29 00:54:08 +03:00
|
|
|
import SortingDropdown from '../utils/SortingDropdown';
|
|
|
|
import GraphCard from './GraphCard';
|
2019-01-07 15:35:14 +03:00
|
|
|
|
2019-01-09 22:34:35 +03:00
|
|
|
const toLowerIfString = (value) => type(value) === 'String' ? toLower(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-01-07 21:43:25 +03:00
|
|
|
extraHeaderContent: PropTypes.arrayOf(PropTypes.func),
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
orderField: undefined,
|
|
|
|
orderDir: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-01-07 21:43:25 +03:00
|
|
|
const { stats, sortingItems, title, extraHeaderContent } = this.props;
|
2018-10-29 00:54:08 +03:00
|
|
|
const sortStats = () => {
|
|
|
|
if (!this.state.orderField) {
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:07:06 +03:00
|
|
|
const sortedPairs = sortBy(
|
|
|
|
pipe(
|
|
|
|
prop(this.state.orderField === head(keys(sortingItems)) ? 0 : 1),
|
|
|
|
toLowerIfString
|
|
|
|
),
|
|
|
|
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>
|
2019-01-07 21:43:25 +03:00
|
|
|
{extraHeaderContent && extraHeaderContent.map((content, index) => (
|
|
|
|
<div key={index} className="float-right">
|
|
|
|
{content()}
|
|
|
|
</div>
|
|
|
|
))}
|
2018-10-29 00:54:08 +03:00
|
|
|
</GraphCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|