From af5b0e188959b19c1275d4da680bca86d4f84b72 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 3 Jan 2019 11:16:46 +0100 Subject: [PATCH] stats components refactor --- fittrackee_client/src/components/App.css | 8 +- .../src/components/Dashboard/Statistics.jsx | 121 +----------------- .../components/Others/Stats/StatsChart.jsx | 97 ++++++++++++++ .../src/components/Others/Stats/index.jsx | 50 ++++++++ .../src/components/Statistics/index.jsx | 121 +----------------- fittrackee_client/src/utils.js | 12 +- 6 files changed, 170 insertions(+), 239 deletions(-) create mode 100644 fittrackee_client/src/components/Others/Stats/StatsChart.jsx create mode 100644 fittrackee_client/src/components/Others/Stats/index.jsx diff --git a/fittrackee_client/src/components/App.css b/fittrackee_client/src/components/App.css index ec986f13..aa9d131e 100644 --- a/fittrackee_client/src/components/App.css +++ b/fittrackee_client/src/components/App.css @@ -128,10 +128,6 @@ label { font-style: italic; } -.chart-month { - font-size: 0.8em; -} - .chart-radio { display: flex; font-size: 0.9em; @@ -145,6 +141,10 @@ label { margin-right: 10px; } +.chart-stats { + font-size: 0.8em; +} + .chart-title { font-size: 1.1em; margin-bottom: 10px; diff --git a/fittrackee_client/src/components/Dashboard/Statistics.jsx b/fittrackee_client/src/components/Dashboard/Statistics.jsx index 3f39aa18..c8a4367d 100644 --- a/fittrackee_client/src/components/Dashboard/Statistics.jsx +++ b/fittrackee_client/src/components/Dashboard/Statistics.jsx @@ -1,140 +1,31 @@ -import { endOfMonth, format, startOfMonth } from 'date-fns' +import { endOfMonth, 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, formatDuration, formatStats } from '../../utils' -import CustomTooltip from '../Others/CustomTooltip' +import Stats from '../Others/Stats' -class Statistics extends React.Component { +export default 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' + duration: 'week', + type: 'by_time', } } - 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 workouts' - ) : ( -
-
- - - -
- - - - displayedData === 'distance' - ? `${value} km` - : displayedData === 'duration' - ? formatDuration(value) - : value - } - /> - - } - /> - {sports.map((s, i) => ( - - ))} - - -
- )} +
) } } - -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 = { - from: format(start, dateFormat), - to: format(end, dateFormat), - time: 'week' - } - dispatch(getStats(userId, 'by_time', params)) - }, - }) -)(Statistics) diff --git a/fittrackee_client/src/components/Others/Stats/StatsChart.jsx b/fittrackee_client/src/components/Others/Stats/StatsChart.jsx new file mode 100644 index 00000000..5a152fb5 --- /dev/null +++ b/fittrackee_client/src/components/Others/Stats/StatsChart.jsx @@ -0,0 +1,97 @@ +import React from 'react' +import { + Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis +} from 'recharts' + +import { activityColors, formatDuration } from '../../../utils' +import CustomTooltip from '../CustomTooltip' + + +export default class StatsCharts extends React.PureComponent { + constructor(props, context) { + super(props, context) + this.state = { + displayedData: 'distance' + } + } + handleRadioChange (changeEvent) { + this.setState({ + displayedData: changeEvent.target.name + }) + } + + render() { + const { displayedData } = this.state + const { sports, stats } = this.props + if (Object.keys(stats).length === 0) { + return 'No workouts' + } + return ( +
+
+ + + +
+ + + + displayedData === 'distance' + ? `${value} km` + : displayedData === 'duration' + ? formatDuration(value) + : value + } + /> + + } + /> + {sports.map((s, i) => ( + + ))} + + +
+ ) + } +} diff --git a/fittrackee_client/src/components/Others/Stats/index.jsx b/fittrackee_client/src/components/Others/Stats/index.jsx new file mode 100644 index 00000000..3ac8e0ad --- /dev/null +++ b/fittrackee_client/src/components/Others/Stats/index.jsx @@ -0,0 +1,50 @@ +import { format } from 'date-fns' +import React from 'react' +import { connect } from 'react-redux' + +import { getStats } from '../../../actions/stats' +import { formatStats } from '../../../utils' +import StatsChart from './StatsChart' + + +class Statistics extends React.PureComponent { + componentDidMount() { + this.props.loadMonthActivities( + this.props.user.id, + this.props.statsParams, + ) + } + + render() { + const { sports, statistics, statsParams } = this.props + const stats = formatStats(statistics, sports, statsParams) + return ( + <> + {Object.keys(statistics).length === 0 ? ( + 'No workouts' + ) : ( + + )} + + ) + } +} + +export default connect( + state => ({ + sports: state.sports.data, + statistics: state.statistics.data, + user: state.user, + }), + dispatch => ({ + loadMonthActivities: (userId, data) => { + const dateFormat = 'YYYY-MM-DD' + const params = { + from: format(data.start, dateFormat), + to: format(data.end, dateFormat), + time: data.duration + } + dispatch(getStats(userId, data.type, params)) + }, + }) +)(Statistics) diff --git a/fittrackee_client/src/components/Statistics/index.jsx b/fittrackee_client/src/components/Statistics/index.jsx index 0c99d5ba..1f34c2f8 100644 --- a/fittrackee_client/src/components/Statistics/index.jsx +++ b/fittrackee_client/src/components/Statistics/index.jsx @@ -1,44 +1,22 @@ -import { endOfMonth, format, startOfMonth, subMonths } from 'date-fns' +import { endOfMonth, startOfMonth, subMonths } 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, formatDuration, formatStats } from '../../utils' -import CustomTooltip from '../Others/CustomTooltip' +import Stats from '../Others/Stats' -class Statistics extends React.Component { +export default class Statistics extends React.Component { constructor(props, context) { super(props, context) const date = new Date() this.state = { start: startOfMonth(subMonths(date, 12)), end: endOfMonth(date), - displayedData: 'distance' + duration: 'month', + type: 'by_time', } } - 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, 'month') return (
@@ -46,97 +24,10 @@ class Statistics extends React.Component { Statistics
- {Object.keys(statistics).length === 0 ? ( - 'No workouts' - ) : ( -
-
- - - -
- - - - displayedData === 'distance' - ? `${value} km` - : displayedData === 'duration' - ? formatDuration(value) - : value - } - /> - - } - /> - {sports.map((s, i) => ( - - ))} - - -
- )} +
) } } - -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 = { - from: format(start, dateFormat), - to: format(end, dateFormat), - time: 'month' - } - dispatch(getStats(userId, 'by_time', params)) - }, - }) -)(Statistics) diff --git a/fittrackee_client/src/utils.js b/fittrackee_client/src/utils.js index 414ddce0..4ed8db9a 100644 --- a/fittrackee_client/src/utils.js +++ b/fittrackee_client/src/utils.js @@ -172,17 +172,19 @@ const dateIncrement = (duration, day) => { } export const formatStats = ( - stats, sports, startDate, endDate, duration = 'week' + stats, sports, params ) => { const nbActivitiesStats = [] const distanceStats = [] const durationStats = [] - for (let day = startOfWeek(startDate); - day <= endDate; - day = dateIncrement(duration, day) + for (let day = startOfWeek(params.start); + day <= params.end; + day = dateIncrement(params.duration, day) ) { - const [xAxisFormat] = xAxisFormats.filter(x => x.duration === duration) + const [xAxisFormat] = xAxisFormats.filter( + x => x.duration === params.duration + ) const date = format(day, xAxisFormat.dateFormat) const xAxis = format(day, xAxisFormat.xAxis) const dataNbActivities = { date: xAxis }