2019-05-22 21:43:04 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { parse, stringify } from 'qs';
|
2019-11-26 19:39:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
getCurrentDates,
|
|
|
|
getDateParamsFromQuery,
|
|
|
|
isoDateFormat,
|
|
|
|
} from 'lib/date';
|
2019-11-26 19:39:40 +00:00
|
|
|
|
2019-05-22 21:43:04 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { DateRangeFilterPicker } from '@woocommerce/components';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { useSettings } from '@woocommerce/data';
|
2019-05-22 21:43:04 +00:00
|
|
|
|
|
|
|
const DefaultDate = ( { value, onChange } ) => {
|
2020-03-25 03:20:17 +00:00
|
|
|
const { wcAdminSettings } = useSettings( 'wc_admin', [
|
|
|
|
'wcAdminSettings',
|
|
|
|
] );
|
|
|
|
const {
|
|
|
|
woocommerce_default_date_range: defaultDateRange,
|
|
|
|
} = wcAdminSettings;
|
2020-02-14 02:23:21 +00:00
|
|
|
const change = ( query ) => {
|
2019-05-22 21:43:04 +00:00
|
|
|
onChange( {
|
|
|
|
target: {
|
|
|
|
name: 'woocommerce_default_date_range',
|
|
|
|
value: stringify( query ),
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
const query = parse( value.replace( /&/g, '&' ) );
|
2020-03-25 03:20:17 +00:00
|
|
|
const { period, compare, before, after } = getDateParamsFromQuery(
|
|
|
|
query,
|
|
|
|
defaultDateRange
|
|
|
|
);
|
2020-02-14 02:23:21 +00:00
|
|
|
const { primary: primaryDate, secondary: secondaryDate } = getCurrentDates(
|
2020-03-25 03:20:17 +00:00
|
|
|
query,
|
|
|
|
defaultDateRange
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-11-26 19:39:40 +00:00
|
|
|
const dateQuery = {
|
|
|
|
period,
|
|
|
|
compare,
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
primaryDate,
|
|
|
|
secondaryDate,
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<DateRangeFilterPicker
|
|
|
|
query={ query }
|
|
|
|
onRangeSelect={ change }
|
|
|
|
dateQuery={ dateQuery }
|
|
|
|
isoDateFormat={ isoDateFormat }
|
|
|
|
/>
|
|
|
|
);
|
2019-05-22 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default DefaultDate;
|