2018-05-23 23:15:52 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-28 10:55:19 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { Button, Dropdown } from '@wordpress/components';
|
|
|
|
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-05-28 10:55:19 +00:00
|
|
|
import DatePickerContent from './content';
|
2018-05-28 10:55:19 +00:00
|
|
|
import { getDateValues, 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
|
|
|
}
|
|
|
|
|
|
|
|
addQueryDefaults( { period, compare, start, end } ) {
|
|
|
|
return {
|
|
|
|
period: period || 'today',
|
|
|
|
compare: compare || 'previous_period',
|
2018-05-28 10:55:19 +00:00
|
|
|
start: start ? moment( start ) : null,
|
|
|
|
end: end ? moment( end ) : 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 ) {
|
|
|
|
const { period, compare, start, end, ...otherQueries } = query; // eslint-disable-line no-unused-vars
|
|
|
|
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 );
|
|
|
|
const { period, compare, start, end } = 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, {
|
|
|
|
start: start ? start.format( isoDateFormat ) : '',
|
|
|
|
end: end ? end.format( isoDateFormat ) : '',
|
|
|
|
} );
|
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 );
|
|
|
|
const { primary, secondary } = getDateValues( queryWithDefaults );
|
2018-05-23 23:15:52 +00:00
|
|
|
return (
|
2018-05-28 10:55:19 +00:00
|
|
|
<div className="woocommerce-date-picker__toggle-label">
|
|
|
|
<p>
|
|
|
|
{ primary.label } ({ primary.range })
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
vs. { secondary.label } ({ secondary.range })
|
|
|
|
</p>
|
2018-05-23 23:15:52 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-05-28 10:55:19 +00:00
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
isValidSelection( selectedTab ) {
|
|
|
|
const { compare, start, end } = this.state;
|
|
|
|
if ( 'custom' === selectedTab ) {
|
|
|
|
return compare && start && end;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
render() {
|
|
|
|
const { period, compare, start, end } = this.state;
|
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
className="woocommerce-date-picker"
|
|
|
|
contentClassName="woocommerce-date-picker__content"
|
|
|
|
position="bottom"
|
|
|
|
expandOnMobile
|
|
|
|
renderToggle={ ( { isOpen, onToggle } ) => (
|
|
|
|
<Button
|
|
|
|
className="woocommerce-date-picker__toggle"
|
|
|
|
onClick={ onToggle }
|
|
|
|
aria-expanded={ isOpen }
|
|
|
|
>
|
|
|
|
{ this.getButtonLabel() }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
renderContent={ ( { onClose } ) => (
|
|
|
|
<DatePickerContent
|
|
|
|
period={ period }
|
|
|
|
compare={ compare }
|
|
|
|
start={ start }
|
|
|
|
end={ end }
|
|
|
|
onSelect={ this.select }
|
|
|
|
onClose={ onClose }
|
|
|
|
getUpdatePath={ this.getUpdatePath }
|
2018-05-28 10:55:19 +00:00
|
|
|
isValidSelection={ this.isValidSelection }
|
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;
|