2018-05-23 23:15:52 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-07-12 01:43:33 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-06-29 00:34:17 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Dropdown } from '@wordpress/components';
|
2018-05-28 10:55:19 +00:00
|
|
|
import { stringify as stringifyQueryObject } from 'qs';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-05-23 23:15:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-06-29 00:34:17 +00:00
|
|
|
import DropdownButton from 'components/dropdown-button';
|
2018-05-28 10:55:19 +00:00
|
|
|
import DatePickerContent from './content';
|
2018-07-13 02:05:03 +00:00
|
|
|
import { getCurrentDates, getDateParamsFromQuery, isoDateFormat } from 'lib/date';
|
2018-05-23 23:15:52 +00:00
|
|
|
|
|
|
|
class DatePicker extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-07-13 02:05:03 +00:00
|
|
|
this.state = getDateParamsFromQuery( props.query );
|
2018-05-23 23:15:52 +00:00
|
|
|
this.select = this.select.bind( this );
|
2018-05-28 10:55:19 +00:00
|
|
|
this.getUpdatePath = this.getUpdatePath.bind( this );
|
2018-05-28 10:55:19 +00:00
|
|
|
this.isValidSelection = this.isValidSelection.bind( this );
|
2018-07-22 23:43:21 +00:00
|
|
|
this.resetCustomValues = this.resetCustomValues.bind( this );
|
2018-05-28 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO change this to `getDerivedStateFromProps` in React 16.4
|
|
|
|
UNSAFE_componentWillReceiveProps( nextProps ) {
|
2018-07-13 02:05:03 +00:00
|
|
|
this.setState( getDateParamsFromQuery( nextProps.query ) );
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
select( update ) {
|
|
|
|
this.setState( update );
|
|
|
|
}
|
|
|
|
|
|
|
|
getOtherQueries( query ) {
|
2018-07-02 02:51:00 +00:00
|
|
|
const { period, compare, after, before, ...otherQueries } = query; // eslint-disable-line no-unused-vars
|
2018-05-28 10:55:19 +00:00
|
|
|
return otherQueries;
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
getUpdatePath( selectedTab ) {
|
|
|
|
const { path, query } = this.props;
|
2018-05-28 10:55:19 +00:00
|
|
|
const otherQueries = this.getOtherQueries( query );
|
2018-07-02 02:51:00 +00:00
|
|
|
const { period, compare, after, before } = this.state;
|
2018-05-23 23:15:52 +00:00
|
|
|
const data = {
|
2018-05-28 10:55:19 +00:00
|
|
|
period: 'custom' === selectedTab ? 'custom' : period,
|
|
|
|
compare,
|
2018-05-23 23:15:52 +00:00
|
|
|
};
|
|
|
|
if ( 'custom' === selectedTab ) {
|
2018-05-28 10:55:19 +00:00
|
|
|
Object.assign( data, {
|
2018-07-02 02:51:00 +00:00
|
|
|
after: after ? after.format( isoDateFormat ) : '',
|
|
|
|
before: before ? before.format( isoDateFormat ) : '',
|
2018-05-28 10:55:19 +00:00
|
|
|
} );
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
2018-05-28 10:55:19 +00:00
|
|
|
const queryString = stringifyQueryObject( Object.assign( otherQueries, data ) );
|
|
|
|
return `${ path }?${ queryString }`;
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
getButtonLabel() {
|
2018-07-13 02:05:03 +00:00
|
|
|
const { primary, secondary } = getCurrentDates( this.props.query );
|
2018-06-29 00:34:17 +00:00
|
|
|
return [
|
|
|
|
`${ primary.label } (${ primary.range })`,
|
2018-07-10 12:48:06 +00:00
|
|
|
`${ __( 'vs.', 'wc-admin' ) } ${ secondary.label } (${ secondary.range })`,
|
2018-06-29 00:34:17 +00:00
|
|
|
];
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
2018-05-28 10:55:19 +00:00
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
isValidSelection( selectedTab ) {
|
2018-07-02 02:51:00 +00:00
|
|
|
const { compare, after, before } = this.state;
|
2018-05-28 10:55:19 +00:00
|
|
|
if ( 'custom' === selectedTab ) {
|
2018-07-02 02:51:00 +00:00
|
|
|
return compare && after && before;
|
2018-05-28 10:55:19 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
resetCustomValues() {
|
|
|
|
this.setState( {
|
|
|
|
after: null,
|
|
|
|
before: null,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
render() {
|
2018-07-02 02:51:00 +00:00
|
|
|
const { period, compare, after, before } = this.state;
|
2018-05-28 10:55:19 +00:00
|
|
|
return (
|
2018-07-12 01:43:33 +00:00
|
|
|
<div className="woocommerce-date-picker">
|
2018-07-10 12:48:06 +00:00
|
|
|
<p>{ __( 'Date Range', 'wc-admin' ) }:</p>
|
2018-06-29 00:34:17 +00:00
|
|
|
<Dropdown
|
|
|
|
contentClassName="woocommerce-date-picker__content"
|
|
|
|
position="bottom"
|
|
|
|
expandOnMobile
|
|
|
|
renderToggle={ ( { isOpen, onToggle } ) => (
|
|
|
|
<DropdownButton
|
|
|
|
onClick={ onToggle }
|
|
|
|
isOpen={ isOpen }
|
|
|
|
labels={ this.getButtonLabel() }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
renderContent={ ( { onClose } ) => (
|
|
|
|
<DatePickerContent
|
|
|
|
period={ period }
|
|
|
|
compare={ compare }
|
|
|
|
after={ after }
|
|
|
|
before={ before }
|
|
|
|
onSelect={ this.select }
|
|
|
|
onClose={ onClose }
|
|
|
|
getUpdatePath={ this.getUpdatePath }
|
|
|
|
isValidSelection={ this.isValidSelection }
|
2018-07-22 23:43:21 +00:00
|
|
|
resetCustomValues={ this.resetCustomValues }
|
2018-06-29 00:34:17 +00:00
|
|
|
/>
|
|
|
|
) }
|
|
|
|
/>
|
2018-07-12 01:43:33 +00:00
|
|
|
</div>
|
2018-05-28 10:55:19 +00:00
|
|
|
);
|
|
|
|
}
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
DatePicker.propTypes = {
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2018-05-23 23:15:52 +00:00
|
|
|
export default DatePicker;
|