start and end dates can be updated on stats graph

This commit is contained in:
Sam 2019-01-04 19:17:16 +01:00
parent 1915c1f374
commit 2a2d3e02ab
5 changed files with 69 additions and 3 deletions

View File

@ -2,8 +2,14 @@ import React from 'react'
import { formatValue } from '../../../utils/stats' import { formatValue } from '../../../utils/stats'
/**
* @return {null}
*/
export default function CustomLabel (props) { export default function CustomLabel (props) {
const { displayedData, x, y, width, value } = props const { displayedData, x, y, width, value } = props
if (!value) {
return null
}
const radius = 10 const radius = 10
const formattedValue = formatValue(displayedData, value) const formattedValue = formatValue(displayedData, value)

View File

@ -79,6 +79,9 @@ export default class StatsCharts extends React.PureComponent {
/> />
{sports.map((s, i) => ( {sports.map((s, i) => (
<Bar <Bar
// disable for now due to problems w/ CustomLabel
// see https://github.com/recharts/recharts/issues/829
isAnimationActive={false}
key={s.id} key={s.id}
dataKey={s.label} dataKey={s.label}
stackId="a" stackId="a"

View File

@ -30,8 +30,10 @@ class Statistics extends React.PureComponent {
} }
render() { render() {
const { displayedSports, sports, statistics, statsParams } = this.props const {
if (Object.keys(statistics).length === 0) { displayedSports, sports, statistics, statsParams, displayEmpty
} = this.props
if (!displayEmpty && Object.keys(statistics).length === 0) {
return 'No workouts' return 'No workouts'
} }
const stats = formatStats(statistics, sports, statsParams, displayedSports) const stats = formatStats(statistics, sports, statsParams, displayedSports)

View File

@ -23,7 +23,7 @@ export default class Statistics extends React.Component {
This month This month
</div> </div>
<div className="card-body"> <div className="card-body">
<Stats statsParams={this.state} /> <Stats displayEmpty={false} statsParams={this.state} />
</div> </div>
</div> </div>
) )

View File

@ -4,7 +4,12 @@ import {
endOfYear, endOfYear,
startOfMonth, startOfMonth,
startOfYear, startOfYear,
startOfWeek,
addMonths,
addWeeks,
addYears,
subMonths, subMonths,
subWeeks,
subYears subYears
} from 'date-fns' } from 'date-fns'
import React from 'react' import React from 'react'
@ -70,6 +75,37 @@ class Statistics extends React.Component {
} }
} }
handleOnClickArrows(forward) {
const { start, end, duration } = this.state.statsParams
let newStart, newEnd
if (forward) {
newStart = duration === 'year'
? startOfYear(subYears(start, 1))
: duration === 'week'
? startOfWeek(subWeeks(start, 1))
: startOfMonth(subMonths(start, 1))
newEnd = duration === 'year'
? endOfYear(subYears(end, 1))
: duration === 'week'
? endOfWeek(subWeeks(end, 1))
: endOfMonth(subMonths(end, 1))
} else {
newStart = duration === 'year'
? startOfYear(addYears(start, 1))
: duration === 'week'
? startOfWeek(addWeeks(start, 1))
: startOfMonth(addMonths(start, 1))
newEnd = duration === 'year'
? endOfYear(addYears(end, 1))
: duration === 'week'
? endOfWeek(addWeeks(end, 1))
: endOfMonth(addMonths(end, 1))
}
this.setState({ statsParams:
{ duration, end: newEnd, start: newStart, type: 'by_time' }
})
}
render() { render() {
const { displayedSports, statsParams } = this.state const { displayedSports, statsParams } = this.state
const { sports } = this.props const { sports } = this.props
@ -81,6 +117,15 @@ class Statistics extends React.Component {
</div> </div>
<div className="card-body"> <div className="card-body">
<div className="chart-filters row"> <div className="chart-filters row">
<div className="col">
<p className="text-center">
<i
className="fa fa-chevron-left"
aria-hidden="true"
onClick={() => this.handleOnClickArrows(true)}
/>
</p>
</div>
<div className="col-md-3"> <div className="col-md-3">
<select <select
className="form-control input-lg" className="form-control input-lg"
@ -95,8 +140,18 @@ class Statistics extends React.Component {
))} ))}
</select> </select>
</div> </div>
<div className="col">
<p className="text-center">
<i
className="fa fa-chevron-right"
aria-hidden="true"
onClick={() => this.handleOnClickArrows(false)}
/>
</p>
</div>
</div> </div>
<Stats <Stats
displayEmpty
displayedSports={displayedSports} displayedSports={displayedSports}
statsParams={statsParams} statsParams={statsParams}
/> />