fix stats graph on refresh

This commit is contained in:
Sam 2019-01-04 15:23:03 +01:00
parent a13d00d5d9
commit 3b499524ca
2 changed files with 25 additions and 17 deletions

View File

@ -9,14 +9,19 @@ import StatsChart from './StatsChart'
class Statistics extends React.PureComponent { class Statistics extends React.PureComponent {
componentDidMount() { componentDidMount() {
this.props.loadActivities( this.updateData()
this.props.user.id,
this.props.statsParams,
)
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
if (this.props.statsParams !== prevProps.statsParams) { if ((this.props.user.id && (this.props.user.id !== prevProps.user.id)) ||
this.props.statsParams !== prevProps.statsParams
) {
this.updateData()
}
}
updateData() {
if (this.props.user.id) {
this.props.loadActivities( this.props.loadActivities(
this.props.user.id, this.props.user.id,
this.props.statsParams, this.props.statsParams,
@ -26,19 +31,11 @@ class Statistics extends React.PureComponent {
render() { render() {
const { displayedSports, sports, statistics, statsParams } = this.props const { displayedSports, sports, statistics, statsParams } = this.props
if (Object.keys(statistics).length === 0) {
return 'No workouts'
}
const stats = formatStats(statistics, sports, statsParams, displayedSports) const stats = formatStats(statistics, sports, statsParams, displayedSports)
return ( return (<StatsChart sports={sports} stats={stats} />)
<>
{Object.keys(statistics).length === 0 ? (
'No workouts'
) : (
<StatsChart
sports={sports}
stats={stats}
/>
)}
</>
)
} }
} }

View File

@ -30,6 +30,17 @@ class Statistics extends React.Component {
} }
} }
componentDidUpdate(prevProps) {
if (this.props.sports !== prevProps.sports) {
this.updateDisplayedSports()
}
}
updateDisplayedSports() {
const { sports } = this.props
this.setState({ displayedSports: sports.map(sport => sport.id) })
}
handleOnChangeDuration(e) { handleOnChangeDuration(e) {
const duration = e.target.value const duration = e.target.value
const date = new Date() const date = new Date()