-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
ScheduleDialog.jsx
274 lines (243 loc) · 7.81 KB
/
ScheduleDialog.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Modal from 'antd/lib/modal';
import DatePicker from 'antd/lib/date-picker';
import TimePicker from 'antd/lib/time-picker';
import Select from 'antd/lib/select';
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 { wrap as wrapDialog, DialogPropType } from '@/components/DialogWrapper';
import { RefreshScheduleType, RefreshScheduleDefault, Moment } from '../proptypes';
import './ScheduleDialog.css';
const WEEKDAYS_SHORT = moment.weekdaysShort();
const WEEKDAYS_FULL = moment.weekdays();
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);
const showUtc = time && !time.isUTC();
function onChange(newTime) {
setTime(newTime);
props.onChange(newTime);
}
return (
<React.Fragment>
<TimePicker
allowClear={false}
value={time}
format={HOUR_FORMAT}
minuteStep={5}
onChange={onChange}
/>
{showUtc && (
<span className="utc" data-testid="utc">
({ moment.utc(time).format(HOUR_FORMAT) } UTC)
</span>
)}
</React.Fragment>
);
}
TimeEditor.propTypes = {
defaultValue: Moment,
onChange: PropTypes.func.isRequired,
};
TimeEditor.defaultProps = {
defaultValue: null,
};
class ScheduleDialog extends React.Component {
static propTypes = {
schedule: RefreshScheduleType,
refreshOptions: PropTypes.arrayOf(PropTypes.number).isRequired,
dialog: DialogPropType.isRequired,
};
static defaultProps = {
schedule: RefreshScheduleDefault,
};
state = this.getState();
getState() {
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];
return {
hour,
minute,
seconds,
interval,
dayOfWeek: day ? WEEKDAYS_SHORT[WEEKDAYS_FULL.indexOf(day)] : null,
newSchedule,
};
}
get intervals() {
const ret = {
[IntervalEnum.NEVER]: [],
};
this.props.refreshOptions.forEach((seconds) => {
const { count, interval } = secondsToInterval(seconds);
if (!(interval in ret)) {
ret[interval] = [];
}
ret[interval].push([count, seconds]);
});
Object.defineProperty(this, 'intervals', { value: ret }); // memoize
return ret;
}
set newSchedule(newProps) {
this.setState(prevState => ({
newSchedule: Object.assign(prevState.newSchedule, newProps),
}));
}
setTime = (time) => {
this.newSchedule = {
time: moment(time)
.utc()
.format(HOUR_FORMAT),
};
};
setInterval = (newSeconds) => {
const { newSchedule } = this.state;
const { interval: newInterval } = secondsToInterval(newSeconds);
// resets to defaults
if (newInterval === IntervalEnum.NEVER) {
newSchedule.until = null;
}
if ([IntervalEnum.NEVER, IntervalEnum.MINUTES, IntervalEnum.HOURS].indexOf(newInterval) !== -1) {
newSchedule.time = null;
}
if (newInterval !== IntervalEnum.WEEKS) {
newSchedule.day_of_week = null;
}
if (
(newInterval === IntervalEnum.DAYS || newInterval === IntervalEnum.WEEKS) &&
(!this.state.minute || !this.state.hour)
) {
newSchedule.time = moment()
.hour('00')
.minute('15')
.utc()
.format(HOUR_FORMAT);
}
if (newInterval === IntervalEnum.WEEKS && !this.state.dayOfWeek) {
newSchedule.day_of_week = WEEKDAYS_FULL[0];
}
newSchedule.interval = newSeconds;
const [hour, minute] = newSchedule.time ? localizeTime(newSchedule.time).split(':') : [null, null];
this.setState({
interval: newInterval,
seconds: newSeconds,
hour,
minute,
dayOfWeek: newSchedule.day_of_week ? WEEKDAYS_SHORT[WEEKDAYS_FULL.indexOf(newSchedule.day_of_week)] : null,
});
this.newSchedule = newSchedule;
};
setScheduleUntil = (_, date) => {
this.newSchedule = { until: date };
};
setWeekday = (e) => {
const dayOfWeek = e.target.value;
this.setState({ dayOfWeek });
this.newSchedule = {
day_of_week: dayOfWeek ? WEEKDAYS_FULL[WEEKDAYS_SHORT.indexOf(dayOfWeek)] : null,
};
};
setUntilToggle = (e) => {
const date = e.target.value ? moment().format(DATE_FORMAT) : null;
this.setScheduleUntil(null, date);
};
save() {
const { newSchedule } = this.state;
// save if changed
if (!isEqual(newSchedule, this.props.schedule)) {
if (newSchedule.interval) {
this.props.dialog.close(clone(newSchedule));
} else {
this.props.dialog.close(null);
}
}
this.props.dialog.dismiss();
}
render() {
const { dialog } = this.props;
const {
interval,
minute,
hour,
seconds,
newSchedule: { until },
} = this.state;
return (
<Modal {...dialog.props} title="Refresh Schedule" className="schedule" onOk={() => this.save()}>
<div className="schedule-component">
<h5>Refresh every</h5>
<div data-testid="interval">
<Select className="input" value={seconds} onChange={this.setInterval} dropdownMatchSelectWidth={false}>
<Option value={null} key="never">
Never
</Option>
{Object.keys(this.intervals).map(int => (
<OptGroup label={capitalize(pluralize(int))} key={int}>
{this.intervals[int].map(([cnt, secs]) => (
<Option value={secs} key={cnt}>
{durationHumanize(secs)}
</Option>
))}
</OptGroup>
))}
</Select>
</div>
</div>
{[IntervalEnum.DAYS, IntervalEnum.WEEKS].indexOf(interval) !== -1 ? (
<div className="schedule-component">
<h5>On time</h5>
<div data-testid="time">
<TimeEditor
defaultValue={hour ? moment().hour(hour).minute(minute) : null}
onChange={this.setTime}
/>
</div>
</div>
) : null}
{IntervalEnum.WEEKS === interval ? (
<div className="schedule-component">
<h5>On day</h5>
<div data-testid="weekday">
<Radio.Group size="medium" defaultValue={this.state.dayOfWeek} onChange={this.setWeekday}>
{WEEKDAYS_SHORT.map(day => (
<Radio.Button value={day} key={day} className="input">
{day[0]}
</Radio.Button>
))}
</Radio.Group>
</div>
</div>
) : null}
{interval !== IntervalEnum.NEVER ? (
<div className="schedule-component">
<h5>Ends</h5>
<div className="ends" data-testid="ends">
<Radio.Group size="medium" value={!!until} onChange={this.setUntilToggle}>
<Radio value={false}>Never</Radio>
<Radio value>On</Radio>
</Radio.Group>
{until ? (
<DatePicker
size="small"
className="datepicker"
value={moment(until)}
allowClear={false}
format={DATE_FORMAT}
onChange={this.setScheduleUntil}
/>
) : null}
</div>
</div>
) : null}
</Modal>
);
}
}
export default wrapDialog(ScheduleDialog);