import { endOfMonth, format, startOfMonth } from 'date-fns' import React from 'react' import { connect } from 'react-redux' import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts' import { getStats } from '../../actions/stats' import { activityColors, formatStats } from '../../utils' class Statistics extends React.Component { constructor(props, context) { super(props, context) const date = new Date() this.state = { start: startOfMonth(date), end: endOfMonth(date), displayedData: 'distance' } } componentDidMount() { this.props.loadMonthActivities( this.props.user.id, this.state.start, this.state.end, ) } handleRadioChange (changeEvent) { this.setState({ displayedData: changeEvent.target.name }) } render() { const { sports, statistics } = this.props const { displayedData, end, start } = this.state const stats = formatStats(statistics, sports, start, end) return (
This month
{Object.keys(statistics).length === 0 ? ( 'No activities!' ) : (
displayedData === 'distance' ? `${value} km` : displayedData === 'duration' ? format(new Date(value * 1000), 'HH:mm') : value } /> {sports.map((s, i) => ( displayedData === 'duration' ? format(new Date(value * 1000), 'HH:mm') : value } stackId="a" fill={activityColors[i]} unit={displayedData === 'distance' ? ' km' : ''} /> ))}
)}
) } } export default connect( state => ({ sports: state.sports.data, statistics: state.statistics.data, user: state.user, }), dispatch => ({ loadMonthActivities: (userId, start, end) => { const dateFormat = 'YYYY-MM-DD' const params = { start: format(start, dateFormat), end: format(end, dateFormat), time: 'week' } dispatch(getStats(userId, 'by_time', params)) }, }) )(Statistics)