From 3d4ad8da815beff74834f9270536fd9300796850 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 4 Jan 2019 11:51:06 +0100 Subject: [PATCH] display stats depending on duration - #13 --- fittrackee_client/src/components/App.css | 4 ++ .../src/components/Common/Stats/index.jsx | 13 +++++- .../src/components/Statistics/index.jsx | 44 ++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/fittrackee_client/src/components/App.css b/fittrackee_client/src/components/App.css index aa9d131e..b60041d3 100644 --- a/fittrackee_client/src/components/App.css +++ b/fittrackee_client/src/components/App.css @@ -123,6 +123,10 @@ label { font-size: 0.9em; } +.chart-filters { + padding-bottom: 10px; +} + .chart-info { font-size: 0.8em; font-style: italic; diff --git a/fittrackee_client/src/components/Common/Stats/index.jsx b/fittrackee_client/src/components/Common/Stats/index.jsx index b6b33d9e..3244746f 100644 --- a/fittrackee_client/src/components/Common/Stats/index.jsx +++ b/fittrackee_client/src/components/Common/Stats/index.jsx @@ -9,12 +9,21 @@ import StatsChart from './StatsChart' class Statistics extends React.PureComponent { componentDidMount() { - this.props.loadMonthActivities( + this.props.loadActivities( this.props.user.id, this.props.statsParams, ) } + componentDidUpdate(prevProps) { + if (this.props.statsParams !== prevProps.statsParams) { + this.props.loadActivities( + this.props.user.id, + this.props.statsParams, + ) + } + } + render() { const { sports, statistics, statsParams } = this.props const stats = formatStats(statistics, sports, statsParams) @@ -37,7 +46,7 @@ export default connect( user: state.user, }), dispatch => ({ - loadMonthActivities: (userId, data) => { + loadActivities: (userId, data) => { const dateFormat = 'YYYY-MM-DD' const params = { from: format(data.start, dateFormat), diff --git a/fittrackee_client/src/components/Statistics/index.jsx b/fittrackee_client/src/components/Statistics/index.jsx index dbbbc0ab..93180fa3 100644 --- a/fittrackee_client/src/components/Statistics/index.jsx +++ b/fittrackee_client/src/components/Statistics/index.jsx @@ -1,8 +1,17 @@ -import { endOfMonth, startOfMonth, subMonths } from 'date-fns' +import { + endOfMonth, + endOfWeek, + endOfYear, + startOfMonth, + startOfYear, + subMonths, + subYears +} from 'date-fns' import React from 'react' import Stats from '../Common/Stats' +const durations = ['week', 'month', 'year'] export default class Statistics extends React.Component { constructor(props, context) { @@ -16,7 +25,24 @@ export default class Statistics extends React.Component { } } + handleOnChange(e) { + const duration = e.target.value + const date = new Date() + const start = duration === 'year' + ? startOfYear(subYears(date, 9)) + : duration === 'week' + ? startOfMonth(subMonths(date, 2)) + : startOfMonth(subMonths(date, 11)) + const end = duration === 'year' + ? endOfYear(date) + : duration === 'week' + ? endOfWeek(date) + : endOfMonth(date) + this.setState({ duration, end, start }) + } + render() { + const { duration } = this.state return (
@@ -24,6 +50,22 @@ export default class Statistics extends React.Component { Statistics
+
+
+ +
+