diff --git a/web/client/components/data/query/DateField.jsx b/web/client/components/data/query/DateField.jsx index 0dd47574d0..4777a23026 100644 --- a/web/client/components/data/query/DateField.jsx +++ b/web/client/components/data/query/DateField.jsx @@ -8,13 +8,115 @@ import React from 'react'; import PropTypes from 'prop-types'; +import { intlShape } from 'react-intl'; +import { getContext } from 'recompose'; +import isNil from 'lodash/isNil'; +import isEmpty from 'lodash/isEmpty'; import moment from 'moment'; import momentLocalizer from 'react-widgets/lib/localizers/moment'; momentLocalizer(moment); + +import { getDateTimeFormat } from '../../../utils/TimeUtils'; +import { getMessageById } from '../../../utils/LocaleUtils'; + import utcDateWrapper from '../../misc/enhancers/utcDateWrapper'; import Message from '../../I18N/Message'; -import { getDateTimeFormat } from '../../../utils/TimeUtils'; -import { DateTimePicker } from 'react-widgets'; +import DateTimePicker from '../../misc/datetimepicker'; +import RangedDateTimePicker from '../../misc/datetimepicker/RangedDateTimePicker'; +import { DATE_TYPE } from '../../../utils/FeatureGridUtils'; + +const DEFAULT_QUICK_TIME_SELECTORS = [ + { + "type": DATE_TYPE.DATE, + "value": "{today}+P0D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.today" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}-P1D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.yesterday" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}+P1D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.tomorrow" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}+P0D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.now" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}-P1D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.yesterday" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}+P1D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.tomorrow" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}/{today}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.today" + }, + { + "type": DATE_TYPE.DATE, + "value": "{thisWeekStart}/{thisWeekEnd}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.thisWeek" + }, + { + "type": DATE_TYPE.DATE, + "value": "{thisMonthStart}/{thisMonthEnd}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.thisMonth" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}/{today}+P7D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}/{today}+P30D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + }, + { + "type": DATE_TYPE.DATE, + "value": "{today}/{today}+P90D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}/{now}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.now" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{thisWeekStart}/{thisWeekEnd}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.thisWeek" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{thisMonthStart}/{thisMonthEnd}", + "labelId": "queryform.attributefilter.datefield.quickSelectors.thisMonth" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}/{now}+P7D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}/{now}+P30D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + }, + { + "type": DATE_TYPE.DATE_TIME, + "value": "{now}/{now}+P90D", + "labelId": "queryform.attributefilter.datefield.quickSelectors.nDaysFrom" + } +]; /** * Date time picker enhanced with UTC and timezone offset @@ -27,6 +129,12 @@ const UTCDateTimePicker = utcDateWrapper({ setDateProp: "onChange" })(DateTimePicker); +const UTCDateTimePickerWithRange = utcDateWrapper({ + dateProp: "value", + dateTypeProp: "type", + setDateProp: "onChange" +})(RangedDateTimePicker); + /** * This enhanced Component is used for supporting "date", "time" or "date-time" attribute types */ @@ -42,10 +150,15 @@ class DateField extends React.Component { onUpdateField: PropTypes.func, onUpdateExceptionField: PropTypes.func, showLabels: PropTypes.bool, - timeEnabled: PropTypes.bool + timeEnabled: PropTypes.bool, + quickDateTimeSelectors: PropTypes.array, + placeholderMsgId: PropTypes.string, + tooltipMsgId: PropTypes.string, + className: PropTypes.string }; static contextTypes = { + messages: PropTypes.object, locale: PropTypes.string }; @@ -60,66 +173,90 @@ class DateField extends React.Component { fieldException: null, onUpdateField: () => {}, onUpdateExceptionField: () => {}, - showLabels: false + showLabels: false, + className: "query-date", + placeholderMsgId: "queryform.attributefilter.datefield.placeholder", + tooltipMsgId: "queryform.attributefilter.datefield.tooltip", + quickDateTimeSelectors: DEFAULT_QUICK_TIME_SELECTORS }; render() { - // these values are already parsed by the enhancer - const startdate = this.props.fieldValue && this.props.fieldValue.startDate || null; - const enddate = this.props.fieldValue && this.props.fieldValue.endDate || null; + const format = getDateTimeFormat(this.context.locale, this.props.attType); + const placeholder = getMessageById(this.context.messages, this.props.placeholderMsgId) || "Insert date"; + const toolTip = this.props.intl && this.props.intl.formatMessage({id: `${this.props.tooltipMsgId}`}, {format}) || `Insert date in ${format} format`; - // needed to initialize the time parts to 00:00:00 - - let dateRow = this.props.operator === "><" ? + return this.props.operator === "><" ? (