diff --git a/plugins/woocommerce-admin/client/dashboard/dashboard-charts/index.js b/plugins/woocommerce-admin/client/dashboard/dashboard-charts/index.js index d3573496014..842427b8949 100644 --- a/plugins/woocommerce-admin/client/dashboard/dashboard-charts/index.js +++ b/plugins/woocommerce-admin/client/dashboard/dashboard-charts/index.js @@ -9,13 +9,14 @@ import { compose } from '@wordpress/compose'; import Gridicon from 'gridicons'; import { isEqual, xor } from 'lodash'; import PropTypes from 'prop-types'; -import { ToggleControl, IconButton, NavigableMenu } from '@wordpress/components'; +import { ToggleControl, IconButton, NavigableMenu, SelectControl } from '@wordpress/components'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ import { EllipsisMenu, MenuItem, SectionHeader } from '@woocommerce/components'; +import { getAllowedIntervalsForQuery } from '@woocommerce/date'; /** * Internal dependencies @@ -31,7 +32,7 @@ class DashboardCharts extends Component { this.state = { chartType: props.userPrefChartType || 'line', hiddenChartKeys: props.userPrefCharts || [], - query: props.query, + interval: props.userPrefIntervals || 'day', }; this.toggle = this.toggle.bind( this ); @@ -40,8 +41,9 @@ class DashboardCharts extends Component { componentDidUpdate( { userPrefCharts: prevUserPrefCharts, userPrefChartType: prevUserPrefChartType, + userPrefChartInterval: prevUserPrefChartInterval, } ) { - const { userPrefCharts, userPrefChartType } = this.props; + const { userPrefCharts, userPrefChartInterval, userPrefChartType } = this.props; if ( userPrefCharts && ! isEqual( userPrefCharts, prevUserPrefCharts ) ) { /* eslint-disable react/no-did-update-set-state */ this.setState( { @@ -56,6 +58,13 @@ class DashboardCharts extends Component { } ); /* eslint-enable react/no-did-update-set-state */ } + if ( userPrefChartInterval !== prevUserPrefChartInterval ) { + /* eslint-disable react/no-did-update-set-state */ + this.setState( { + interval: userPrefChartInterval, + } ); + /* eslint-enable react/no-did-update-set-state */ + } } toggle( key ) { @@ -97,14 +106,56 @@ class DashboardCharts extends Component { ); } + renderIntervalSelector() { + const allowedIntervals = getAllowedIntervalsForQuery( this.props.query ); + if ( ! allowedIntervals || allowedIntervals.length < 1 ) { + return null; + } + + const intervalLabels = { + hour: __( 'By hour', 'wc-admin' ), + day: __( 'By day', 'wc-admin' ), + week: __( 'By week', 'wc-admin' ), + month: __( 'By month', 'wc-admin' ), + quarter: __( 'By quarter', 'wc-admin' ), + year: __( 'By year', 'wc-admin' ), + }; + + return ( + ( { + value: allowedInterval, + label: intervalLabels[ allowedInterval ], + } ) ) } + onChange={ this.setInterval } + /> + ); + } + + setInterval = interval => { + this.setState( { interval }, () => { + const userDataFields = { + [ 'dashboard_chart_interval' ]: this.state.interval, + }; + this.props.updateCurrentUserData( userDataFields ); + } ); + }; + render() { const { path } = this.props; - const { chartType, hiddenChartKeys } = this.state; - const query = { ...this.props.query, type: chartType }; + const { chartType, hiddenChartKeys, interval } = this.state; + const query = { ...this.props.query, type: chartType, interval }; return (
- + + { this.renderIntervalSelector() } { diff --git a/plugins/woocommerce-admin/client/wc-api/user/operations.js b/plugins/woocommerce-admin/client/wc-api/user/operations.js index 8782ca73f4c..2c036b1fadd 100644 --- a/plugins/woocommerce-admin/client/wc-api/user/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/user/operations.js @@ -42,6 +42,7 @@ function updateCurrentUserData( resourceNames, data, fetch ) { 'variations_report_columns', 'dashboard_charts', 'dashboard_chart_type', + 'dashboard_chart_interval', 'dashboard_leaderboards', 'dashboard_leaderboard_rows', ]; diff --git a/plugins/woocommerce-admin/lib/admin.php b/plugins/woocommerce-admin/lib/admin.php index b78edf2dad5..78b3995e82a 100644 --- a/plugins/woocommerce-admin/lib/admin.php +++ b/plugins/woocommerce-admin/lib/admin.php @@ -401,6 +401,7 @@ function wc_admin_get_user_data_fields() { 'variations_report_columns', 'dashboard_charts', 'dashboard_chart_type', + 'dashboard_chart_interval', 'dashboard_leaderboards', 'dashboard_leaderboard_rows', ); diff --git a/plugins/woocommerce-admin/packages/components/src/section-header/style.scss b/plugins/woocommerce-admin/packages/components/src/section-header/style.scss index a3d8ceb9fb0..61a176f1f14 100644 --- a/plugins/woocommerce-admin/packages/components/src/section-header/style.scss +++ b/plugins/woocommerce-admin/packages/components/src/section-header/style.scss @@ -21,6 +21,19 @@ height: 1px; margin: 0 10px; } + + @include breakpoint( '<782px' ) { + &.has-interval-select { + position: relative; + padding-bottom: 30px; + .woocommerce-chart__interval-select { + position: absolute; + left: 0; + bottom: 0; + padding-left: 6px; + } + } + } } .woocommerce-section-header__actions, @@ -31,6 +44,18 @@ display: flex; flex-grow: 1; justify-content: flex-end; + align-items: center; + + .components-base-control { + padding-top: 0; + min-height: 34px; + } + .components-base-control__field { + margin-bottom: 0; + select { + background: transparent; + } + } } .woocommerce-ellipsis-menu__toggle { @@ -48,7 +73,7 @@ // EllipsisMenu is 24px, so to match we add 6px padding around the // heading text, which we know is 18px from line-height. padding: 3px 0; - @include font-size( 15 ); + @include font-size( 18 ); line-height: 2.2; font-weight: 600; }