2018-05-23 23:15:52 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-06-29 00:34:17 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Dropdown } from '@wordpress/components';
|
2018-05-28 10:55:19 +00:00
|
|
|
import { stringify as stringifyQueryObject } from 'qs';
|
|
|
|
import moment from 'moment';
|
|
|
|
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-05-28 10:55:19 +00:00
|
|
|
import { getCurrentDates, isoDateFormat } from 'lib/date';
|
2018-05-23 23:15:52 +00:00
|
|
|
|
|
|
|
class DatePicker extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-05-28 10:55:19 +00:00
|
|
|
this.state = this.addQueryDefaults( 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO change this to `getDerivedStateFromProps` in React 16.4
|
|
|
|
UNSAFE_componentWillReceiveProps( nextProps ) {
|
|
|
|
this.setState( this.addQueryDefaults( nextProps.query ) );
|
2018-05-28 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 02:51:00 +00:00
|
|
|
addQueryDefaults( { period, compare, after, before } ) {
|
2018-05-28 10:55:19 +00:00
|
|
|
return {
|
|
|
|
period: period || 'today',
|
|
|
|
compare: compare || 'previous_period',
|
2018-07-02 02:51:00 +00:00
|
|
|
after: after ? moment( after ) : null,
|
|
|
|
before: before ? moment( before ) : null,
|
2018-05-28 10:55:19 +00:00
|
|
|
};
|
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() {
|
|
|
|
const queryWithDefaults = this.addQueryDefaults( this.props.query );
|
2018-05-28 10:55:19 +00:00
|
|
|
const { primary, secondary } = getCurrentDates( queryWithDefaults );
|
2018-06-29 00:34:17 +00:00
|
|
|
return [
|
|
|
|
`${ primary.label } (${ primary.range })`,
|
|
|
|
`${ __( 'vs.', 'woo-dash' ) } ${ secondary.label } (${ secondary.range })`,
|
|
|
|
];
|
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-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-06-29 00:34:17 +00:00
|
|
|
<Fragment>
|
|
|
|
<p>{ __( 'Date Range', 'woo-dash' ) }:</p>
|
|
|
|
<Dropdown
|
|
|
|
className="woocommerce-date-picker"
|
|
|
|
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 }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
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;
|