|
| 1 | +import React, { PureComponent } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import _ from 'lodash'; |
| 4 | +import PropTypes from 'prop-types'; |
| 5 | +import { |
| 6 | + BarChart, |
| 7 | + Bar, |
| 8 | + XAxis, |
| 9 | + YAxis, |
| 10 | + CartesianGrid, |
| 11 | + Tooltip, |
| 12 | + Legend, |
| 13 | + ResponsiveContainer, |
| 14 | +} from 'recharts'; |
| 15 | +import Typography from '@material-ui/core/Typography'; |
| 16 | +import { withStyles } from '@material-ui/core'; |
| 17 | +import { getDatabase, setDatabase } from '../../actions'; |
| 18 | +import Loader from '../common/Loader'; |
| 19 | +import Styles from '../../Styles'; |
| 20 | + |
| 21 | +class ActionBarChart extends PureComponent { |
| 22 | + static propTypes = { |
| 23 | + classes: PropTypes.shape({ |
| 24 | + root: PropTypes.string.isRequired, |
| 25 | + appBar: PropTypes.string.isRequired, |
| 26 | + appBarShift: PropTypes.string.isRequired, |
| 27 | + menuButton: PropTypes.string.isRequired, |
| 28 | + hide: PropTypes.string.isRequired, |
| 29 | + drawer: PropTypes.string.isRequired, |
| 30 | + drawerPaper: PropTypes.string.isRequired, |
| 31 | + drawerHeader: PropTypes.string.isRequired, |
| 32 | + content: PropTypes.string.isRequired, |
| 33 | + contentShift: PropTypes.string.isRequired, |
| 34 | + developer: PropTypes.string.isRequired, |
| 35 | + screenTitle: PropTypes.string.isRequired, |
| 36 | + }).isRequired, |
| 37 | + theme: PropTypes.shape({ |
| 38 | + palette: PropTypes.shape({ |
| 39 | + type: PropTypes.string.isRequired, |
| 40 | + primary: PropTypes.arrayOf(PropTypes.string).isRequired, |
| 41 | + }).isRequired, |
| 42 | + }).isRequired, |
| 43 | + history: PropTypes.shape({ |
| 44 | + replace: PropTypes.func.isRequired, |
| 45 | + }).isRequired, |
| 46 | + i18n: PropTypes.shape({ |
| 47 | + changeLanguage: PropTypes.func.isRequired, |
| 48 | + }).isRequired, |
| 49 | + database: PropTypes.shape({ |
| 50 | + user: PropTypes.object, |
| 51 | + spaces: PropTypes.array, |
| 52 | + actions: PropTypes.array, |
| 53 | + }), |
| 54 | + }; |
| 55 | + |
| 56 | + static defaultProps = { |
| 57 | + database: {}, |
| 58 | + }; |
| 59 | + |
| 60 | + render() { |
| 61 | + const { database, theme } = this.props; |
| 62 | + const { |
| 63 | + palette: { primary, type }, |
| 64 | + } = theme; |
| 65 | + |
| 66 | + if (!database || _.isEmpty(database)) { |
| 67 | + return <Loader />; |
| 68 | + } |
| 69 | + |
| 70 | + const { spaces, actions } = database; |
| 71 | + const data = |
| 72 | + // group actions by space id |
| 73 | + Object.entries(_.groupBy(actions, 'spaceId')) |
| 74 | + // map space id to corresponding space name |
| 75 | + // reduce actions by count number |
| 76 | + .map(([id, actionElements]) => ({ |
| 77 | + space: _.find(spaces, ['id', id]).name, |
| 78 | + count: actionElements.length, |
| 79 | + })); |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <Typography variant="h5">Action Count Per Space</Typography> |
| 84 | + <ResponsiveContainer width="100%" height="100%"> |
| 85 | + <BarChart |
| 86 | + width="100%" |
| 87 | + height="100%" |
| 88 | + data={data} |
| 89 | + margin={{ |
| 90 | + top: 5, |
| 91 | + right: 30, |
| 92 | + left: 20, |
| 93 | + bottom: 5, |
| 94 | + }} |
| 95 | + > |
| 96 | + <CartesianGrid strokeDasharray="3 3" /> |
| 97 | + <XAxis dataKey="space" /> |
| 98 | + <YAxis |
| 99 | + label={{ |
| 100 | + value: 'Action count', |
| 101 | + angle: -90, |
| 102 | + position: 'insideLeft', |
| 103 | + }} |
| 104 | + /> |
| 105 | + <Tooltip /> |
| 106 | + <Legend /> |
| 107 | + <Bar name="action count" dataKey="count" fill={primary[type]} /> |
| 108 | + </BarChart> |
| 109 | + </ResponsiveContainer> |
| 110 | + </> |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +const mapStateToProps = ({ Developer }) => ({ |
| 116 | + database: Developer.get('database'), |
| 117 | +}); |
| 118 | + |
| 119 | +const mapDispatchToProps = { |
| 120 | + dispatchGetDatabase: getDatabase, |
| 121 | + dispatchSetDatabase: setDatabase, |
| 122 | +}; |
| 123 | + |
| 124 | +const ConnectedComponent = connect( |
| 125 | + mapStateToProps, |
| 126 | + mapDispatchToProps |
| 127 | +)(ActionBarChart); |
| 128 | + |
| 129 | +const StyledComponent = withStyles(Styles, { withTheme: true })( |
| 130 | + ConnectedComponent |
| 131 | +); |
| 132 | +export default StyledComponent; |
0 commit comments