diff --git a/client/app/components/proptypes.js b/client/app/components/proptypes.js index ff18c69de8..19b7f6c5aa 100644 --- a/client/app/components/proptypes.js +++ b/client/app/components/proptypes.js @@ -15,6 +15,20 @@ export const Table = PropTypes.shape({ export const Schema = PropTypes.arrayOf(Table); +export const RefreshScheduleType = PropTypes.shape({ + interval: PropTypes.number, + time: PropTypes.string, + day_of_week: PropTypes.string, + until: PropTypes.string, +}); + +export const RefreshScheduleDefault = { + interval: null, + time: null, + day_of_week: null, + until: null, +}; + export const Field = PropTypes.shape({ name: PropTypes.string.isRequired, title: PropTypes.string, diff --git a/client/app/components/queries/ScheduleDialog.jsx b/client/app/components/queries/ScheduleDialog.jsx index d33b961e76..5557aa5da7 100644 --- a/client/app/components/queries/ScheduleDialog.jsx +++ b/client/app/components/queries/ScheduleDialog.jsx @@ -9,6 +9,7 @@ import Radio from 'antd/lib/radio'; import { capitalize, clone, isEqual } from 'lodash'; import moment from 'moment'; import { secondsToInterval, durationHumanize, pluralize, IntervalEnum, localizeTime } from '@/filters'; +import { RefreshScheduleType, RefreshScheduleDefault } from '../proptypes'; import './ScheduleDialog.css'; @@ -21,13 +22,16 @@ const { Option, OptGroup } = Select; export class ScheduleDialog extends React.Component { static propTypes = { show: PropTypes.bool.isRequired, - // eslint-disable-next-line react/forbid-prop-types - query: PropTypes.object.isRequired, + schedule: RefreshScheduleType, refreshOptions: PropTypes.arrayOf(PropTypes.number).isRequired, updateQuery: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired, }; + static defaultProps = { + schedule: RefreshScheduleDefault, + }; + constructor(props) { super(props); this.state = this.initState; @@ -35,7 +39,7 @@ export class ScheduleDialog extends React.Component { } get initState() { - const newSchedule = clone(this.props.query.schedule); + const newSchedule = clone(this.props.schedule || ScheduleDialog.defaultProps.schedule); const { time, interval: seconds, day_of_week: day } = newSchedule; const { interval } = secondsToInterval(seconds); const [hour, minute] = time ? localizeTime(time).split(':') : [null, null]; @@ -144,9 +148,15 @@ export class ScheduleDialog extends React.Component { } save() { + const { newSchedule } = this.state; + // save if changed - if (!isEqual(this.state.newSchedule, this.props.query.schedule)) { - this.props.updateQuery({ schedule: clone(this.state.newSchedule) }); + if (!isEqual(newSchedule, this.props.schedule)) { + if (newSchedule.interval) { + this.props.updateQuery({ schedule: clone(newSchedule) }); + } else { + this.props.updateQuery({ schedule: null }); + } } this.props.onClose(); } diff --git a/client/app/components/queries/ScheduleDialog.test.js b/client/app/components/queries/ScheduleDialog.test.js index cb0fc8ee2b..9227173344 100644 --- a/client/app/components/queries/ScheduleDialog.test.js +++ b/client/app/components/queries/ScheduleDialog.test.js @@ -1,17 +1,11 @@ import React from 'react'; import { mount } from 'enzyme'; import { ScheduleDialog } from './ScheduleDialog'; +import RefreshScheduleDefault from '../proptypes'; const defaultProps = { show: true, - query: { - schedule: { - time: null, - until: null, - interval: null, - day_of_week: null, - }, - }, + schedule: RefreshScheduleDefault, refreshOptions: [ 60, 300, 600, // 1, 5 ,10 mins 3600, 36000, 82800, // 1, 10, 23 hours @@ -23,12 +17,11 @@ const defaultProps = { }; function getWrapper(schedule = {}, props = {}) { - const defaultSchedule = defaultProps.query.schedule; props = Object.assign( {}, defaultProps, props, - { query: { schedule: Object.assign({}, defaultSchedule, schedule) } }, + { schedule: Object.assign({}, RefreshScheduleDefault, schedule) }, ); return [mount(), props]; } @@ -78,7 +71,7 @@ describe('ScheduleDialog', () => { const [wrapper] = getWrapper({ interval: 1209600, time: '22:15', - day_of_week: 2, + day_of_week: 'Monday', }); test('Sets to correct interval', () => { diff --git a/client/app/components/queries/SchedulePhrase.jsx b/client/app/components/queries/SchedulePhrase.jsx index 665b0ae1ab..9858d7b4f0 100644 --- a/client/app/components/queries/SchedulePhrase.jsx +++ b/client/app/components/queries/SchedulePhrase.jsx @@ -3,23 +3,24 @@ import React from 'react'; import PropTypes from 'prop-types'; import Tooltip from 'antd/lib/tooltip'; import { localizeTime, durationHumanize } from '@/filters'; +import { RefreshScheduleType, RefreshScheduleDefault } from '../proptypes'; import './ScheduleDialog.css'; class SchedulePhrase extends React.Component { static propTypes = { - // eslint-disable-next-line react/forbid-prop-types - schedule: PropTypes.object.isRequired, + schedule: RefreshScheduleType, isNew: PropTypes.bool.isRequired, isLink: PropTypes.bool, }; static defaultProps = { + schedule: RefreshScheduleDefault, isLink: false, }; get content() { - const { interval: seconds } = this.props.schedule; + const { interval: seconds } = this.props.schedule || SchedulePhrase.defaultProps.schedule; if (!seconds) { return ['Never']; } diff --git a/client/app/components/queries/__snapshots__/ScheduleDialog.test.js.snap b/client/app/components/queries/__snapshots__/ScheduleDialog.test.js.snap index 899d1525ca..65a8d28ff2 100644 --- a/client/app/components/queries/__snapshots__/ScheduleDialog.test.js.snap +++ b/client/app/components/queries/__snapshots__/ScheduleDialog.test.js.snap @@ -1632,6 +1632,7 @@ exports[`ScheduleDialog Sets correct schedule settings Sets to "2 Weeks 22:15 Tu >