woocommerce/plugins/woocommerce-admin/client/components/date-picker/index.js

159 lines
3.9 KiB
JavaScript
Raw Normal View History

/** @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';
import { stringify as stringifyQueryObject } from 'qs';
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import './style.scss';
2018-06-29 00:34:17 +00:00
import DropdownButton from 'components/dropdown-button';
import DatePickerContent from './content';
import { getCurrentDates, getDateParamsFromQuery, isoDateFormat } from 'lib/date';
const shortDateFormat = __( 'MM/DD/YYYY', 'wc-admin' );
class DatePicker extends Component {
constructor( props ) {
super( props );
this.state = this.getResetState( props );
this.update = this.update.bind( this );
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
}
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,
};
}
update( update ) {
2018-05-28 10:55:19 +00:00
this.setState( update );
}
getOtherQueries( query ) {
const { period, compare, after, before, ...otherQueries } = query; // eslint-disable-line no-unused-vars
2018-05-28 10:55:19 +00:00
return otherQueries;
}
getUpdatePath( selectedTab ) {
const { path, query } = this.props;
2018-05-28 10:55:19 +00:00
const otherQueries = this.getOtherQueries( query );
const { period, compare, after, before } = this.state;
const data = {
2018-05-28 10:55:19 +00:00
period: 'custom' === selectedTab ? 'custom' : period,
compare,
};
if ( 'custom' === selectedTab ) {
2018-05-28 10:55:19 +00:00
Object.assign( data, {
after: after ? after.format( isoDateFormat ) : '',
before: before ? before.format( isoDateFormat ) : '',
2018-05-28 10:55:19 +00:00
} );
}
const queryString = stringifyQueryObject( Object.assign( otherQueries, data ) );
return `${ path }?${ queryString }`;
}
getButtonLabel() {
const { primary, secondary } = getCurrentDates( this.props.query );
2018-06-29 00:34:17 +00:00
return [
`${ primary.label } (${ primary.range })`,
`${ __( 'vs.', 'wc-admin' ) } ${ secondary.label } (${ secondary.range })`,
2018-06-29 00:34:17 +00:00
];
}
2018-05-28 10:55:19 +00:00
isValidSelection( selectedTab ) {
const { compare, after, before } = this.state;
2018-05-28 10:55:19 +00:00
if ( 'custom' === selectedTab ) {
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,
focusedInput: 'startDate',
afterText: '',
beforeText: '',
afterError: null,
beforeError: null,
2018-07-22 23:43:21 +00:00
} );
}
render() {
const {
period,
compare,
after,
before,
focusedInput,
afterText,
beforeText,
afterError,
beforeError,
} = this.state;
return (
2018-07-12 01:43:33 +00:00
<div className="woocommerce-date-picker">
<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 }
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 }
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>
);
}
}
DatePicker.propTypes = {
path: PropTypes.string.isRequired,
query: PropTypes.object.isRequired,
};
export default DatePicker;