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
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
const shortDateFormat = __( 'MM/DD/YYYY', 'wc-admin' );
|
|
|
|
|
2018-05-23 23:15:52 +00:00
|
|
|
class DatePicker extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-07-22 23:43:21 +00:00
|
|
|
this.state = this.getResetState( props );
|
|
|
|
|
|
|
|
this.update = this.update.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
|
|
|
}
|
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
getResetState( props ) {
|
|
|
|
const { period, compare, before, after } = getDateParamsFromQuery( props.query );
|
|
|
|
return {
|
|
|
|
period,
|
|
|
|
compare,
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
focusedInput: 'startDate',
|
|
|
|
afterText: after ? after.format( shortDateFormat ) : '',
|
|
|
|
beforeText: before ? before.format( shortDateFormat ) : '',
|
|
|
|
afterError: null,
|
|
|
|
beforeError: null,
|
|
|
|
};
|
2018-05-23 23:15:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
update( update ) {
|
2018-05-28 10:55:19 +00:00
|
|
|
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-07-22 23:43:21 +00:00
|
|
|
focusedInput: 'startDate',
|
|
|
|
afterText: '',
|
|
|
|
beforeText: '',
|
|
|
|
afterError: null,
|
|
|
|
beforeError: null,
|
2018-07-22 23:43:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
render() {
|
2018-07-22 23:43:21 +00:00
|
|
|
const {
|
|
|
|
period,
|
|
|
|
compare,
|
|
|
|
after,
|
|
|
|
before,
|
|
|
|
focusedInput,
|
|
|
|
afterText,
|
|
|
|
beforeText,
|
|
|
|
afterError,
|
|
|
|
beforeError,
|
|
|
|
} = 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 }
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate={ this.update }
|
2018-06-29 00:34:17 +00:00
|
|
|
onClose={ onClose }
|
|
|
|
getUpdatePath={ this.getUpdatePath }
|
|
|
|
isValidSelection={ this.isValidSelection }
|
2018-07-22 23:43:21 +00:00
|
|
|
resetCustomValues={ this.resetCustomValues }
|
2018-07-22 23:43:21 +00:00
|
|
|
focusedInput={ focusedInput }
|
|
|
|
afterText={ afterText }
|
|
|
|
beforeText={ beforeText }
|
|
|
|
afterError={ afterError }
|
|
|
|
beforeError={ beforeError }
|
|
|
|
shortDateFormat={ shortDateFormat }
|
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;
|