Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UTC display to scheduler dialog #3517

Merged
merged 5 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/app/components/queries/ScheduleDialog.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@
a.schedule-phrase {
cursor: pointer;
}

.utc {
opacity: 0.4;
margin-left: 10px;
}
53 changes: 40 additions & 13 deletions client/app/components/queries/ScheduleDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Modal from 'antd/lib/modal';
import DatePicker from 'antd/lib/date-picker';
Expand All @@ -9,7 +9,7 @@ import { capitalize, clone, isEqual } from 'lodash';
import moment from 'moment';
import { secondsToInterval, durationHumanize, pluralize, IntervalEnum, localizeTime } from '@/filters';
import { wrap as wrapDialog, DialogPropType } from '@/components/DialogWrapper';
import { RefreshScheduleType, RefreshScheduleDefault } from '../proptypes';
import { RefreshScheduleType, RefreshScheduleDefault, Moment } from '../proptypes';

import './ScheduleDialog.css';

Expand All @@ -19,6 +19,42 @@ const DATE_FORMAT = 'YYYY-MM-DD';
const HOUR_FORMAT = 'HH:mm';
const { Option, OptGroup } = Select;

export function TimeEditor(props) {
const [time, setTime] = useState(props.defaultValue);
arikfr marked this conversation as resolved.
Show resolved Hide resolved
const showUtc = time && !time.isUTC();

function onChange(newTime) {
setTime(newTime);
props.onChange(newTime);
}

return (
<React.Fragment>
<TimePicker
allowEmpty={false}
value={time}
format={HOUR_FORMAT}
minuteStep={5}
onChange={onChange}
/>
{showUtc && (
<span className="utc" data-testid="utc">
({ moment.utc(time).format(HOUR_FORMAT) } UTC)
ranbena marked this conversation as resolved.
Show resolved Hide resolved
</span>
)}
</React.Fragment>
);
}

TimeEditor.propTypes = {
defaultValue: Moment,
onChange: PropTypes.func.isRequired,
};

TimeEditor.defaultProps = {
defaultValue: null,
};

class ScheduleDialog extends React.Component {
static propTypes = {
schedule: RefreshScheduleType,
Expand Down Expand Up @@ -188,17 +224,8 @@ class ScheduleDialog extends React.Component {
<div className="schedule-component">
<h5>On time</h5>
<div data-testid="time">
<TimePicker
allowEmpty={false}
defaultValue={
hour
? moment()
.hour(hour)
.minute(minute)
: null
}
format={HOUR_FORMAT}
minuteStep={5}
<TimeEditor
defaultValue={hour ? moment().hour(hour).minute(minute) : null}
onChange={this.setTime}
/>
</div>
Expand Down
54 changes: 53 additions & 1 deletion client/app/components/queries/ScheduleDialog.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import ScheduleDialog from './ScheduleDialog';
import moment from 'moment';
import ScheduleDialog, { TimeEditor } from './ScheduleDialog';
import RefreshScheduleDefault from '../proptypes';

const defaultProps = {
Expand Down Expand Up @@ -97,6 +98,57 @@ describe('ScheduleDialog', () => {
});
});

describe('TimeEditor', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome 👌

But has one small issue: you assume 2 hours difference between the timezone and UTC. But what happens when DST kicks in (or the other way around)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be solved setting timezone for tests not using name, but explicitly TZ=GMT-2:00 or something

"test": "TZ=Asia/Jerusalem jest",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good save @arikfr :)
Now that I think of it, our other test snapshots might also fail when DST changes on March 29th! 😵

But I couldn't figure out an appropriate TZ value. Couldn't find docs, seems like it accepts only location strings and UTC. No offset. Plz help.

If no solution is found, might need to fallback to TZ=UTC and mock offset in test itself.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranbena TZ is just a system variable and all programs should respect it:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10x @kravets-levko but it just doesn't work.
I changed to a non-dst timezone instead, so we're good.

"test": "TZ=Africa/Khartoum jest",

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it just doesn't work.

Jest - 🤦‍♂️

const defaultValue = moment().hour(5).minute(25); // 05:25

test('UTC set correctly on init', () => {
const editor = mount(<TimeEditor defaultValue={defaultValue} onChange={() => {}} />);
const utc = findByTestID(editor, 'utc');

// expect utc to be 2h below initial time
expect(utc.text()).toBe('(03:25 UTC)');
});

test('UTC time should not render', () => {
const utcValue = moment.utc(defaultValue);
const editor = mount(<TimeEditor defaultValue={utcValue} onChange={() => {}} />);
const utc = findByTestID(editor, 'utc');

// expect utc to not render
expect(utc.exists()).toBeFalsy();
});

test('onChange correct result', () => {
const onChangeCb = jest.fn(time => time.format('HH:mm'));
const editor = mount(<TimeEditor onChange={onChangeCb} />);

// click TimePicker
editor.find('.ant-time-picker-input').simulate('click');

// select hour "07"
const hourSelector = editor.find('.ant-time-picker-panel-select').at(0);
hourSelector
.find('li')
.at(7)
.simulate('click');

// select minute "30"
const minuteSelector = editor.find('.ant-time-picker-panel-select').at(1);
minuteSelector
.find('li')
.at(6)
.simulate('click');

// expect utc to be 2h below initial time
const utc = findByTestID(editor, 'utc');
expect(utc.text()).toBe('(05:30 UTC)');

// expect 07:30 from onChange
const onChangeResult = onChangeCb.mock.results[1].value;
expect(onChangeResult).toBe('07:30');
});
});

describe('Sets to "2 Weeks 22:15 Tuesday"', () => {
const [wrapper] = getWrapper({
interval: 1209600,
Expand Down
Loading