X Tutup
Skip to content

Commit de04a40

Browse files
ranbenaarikfr
authored andcommitted
Fix: Refresh schedule interval count doesn't adhere to permission rules (getredash#3265)
* Fix: Refresh schedule interval count doesn't adhere to permission rules * Fix “4.28 weeks” to “30 days” * Merge interval type and count into one <Select>
1 parent 0b6f1fc commit de04a40

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

client/app/components/queries/ScheduleDialog.jsx

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@ import DatePicker from 'antd/lib/date-picker';
66
import TimePicker from 'antd/lib/time-picker';
77
import Select from 'antd/lib/select';
88
import Radio from 'antd/lib/radio';
9-
import { range, clone, isEqual } from 'lodash';
9+
import { capitalize, clone, isEqual } from 'lodash';
1010
import moment from 'moment';
11-
import { secondsToInterval, intervalToSeconds, IntervalEnum, localizeTime } from '@/filters';
11+
import { secondsToInterval, IntervalEnum, localizeTime } from '@/filters';
1212

1313
import './ScheduleDialog.css';
1414

1515
const WEEKDAYS_SHORT = moment.weekdaysShort();
1616
const WEEKDAYS_FULL = moment.weekdays();
17-
const INTERVAL_OPTIONS_MAP = {
18-
[IntervalEnum.NEVER]: 1,
19-
[IntervalEnum.MINUTES]: 60,
20-
[IntervalEnum.HOURS]: 24,
21-
[IntervalEnum.DAYS]: 7,
22-
[IntervalEnum.WEEKS]: 5,
23-
};
24-
2517
const DATE_FORMAT = 'YYYY-MM-DD';
2618
const HOUR_FORMAT = 'HH:mm';
27-
const Option = Select.Option;
19+
const { Option, OptGroup } = Select;
2820

2921
class ScheduleDialog extends React.Component {
3022
static propTypes = {
@@ -44,31 +36,31 @@ class ScheduleDialog extends React.Component {
4436

4537
get initState() {
4638
const newSchedule = clone(this.props.query.schedule);
47-
const { time, interval: secs, day_of_week: day } = newSchedule;
48-
const interval = secs ? secondsToInterval(secs) : {};
39+
const { time, interval: seconds, day_of_week: day } = newSchedule;
40+
const { interval } = secondsToInterval(seconds);
4941
const [hour, minute] = time ? localizeTime(time).split(':') : [null, null];
5042

5143
return {
5244
hour,
5345
minute,
54-
count: interval.count ? String(interval.count) : '1',
55-
interval: interval.interval || IntervalEnum.NEVER,
46+
seconds,
47+
interval,
5648
dayOfWeek: day ? WEEKDAYS_SHORT[WEEKDAYS_FULL.indexOf(day)] : null,
5749
newSchedule,
5850
};
5951
}
6052

6153
get intervals() {
62-
const ret = this.props.refreshOptions
63-
.map((seconds) => {
64-
const { count, interval } = secondsToInterval(seconds);
65-
return count === 1 ? {
66-
name: interval,
67-
time: seconds,
68-
} : null;
69-
})
70-
.filter(x => x !== null);
71-
ret.unshift({ name: IntervalEnum.NEVER });
54+
const ret = {
55+
[IntervalEnum.NEVER]: [],
56+
};
57+
this.props.refreshOptions.forEach((seconds) => {
58+
const { count, interval } = secondsToInterval(seconds);
59+
if (!(interval in ret)) {
60+
ret[interval] = [];
61+
}
62+
ret[interval].push([count, seconds]);
63+
});
7264

7365
Object.defineProperty(this, 'intervals', { value: ret }); // memoize
7466

@@ -81,16 +73,15 @@ class ScheduleDialog extends React.Component {
8173
});
8274
}
8375

84-
getCounts = interval => range(1, INTERVAL_OPTIONS_MAP[interval])
85-
8676
setTime = (time) => {
8777
this.newSchedule = {
8878
time: moment(time).utc().format(HOUR_FORMAT),
8979
};
9080
};
9181

92-
setInterval = (newInterval) => {
82+
setInterval = (newSeconds) => {
9383
const { newSchedule } = this.state;
84+
const { interval: newInterval } = secondsToInterval(newSeconds);
9485

9586
// resets to defaults
9687
if (newInterval === IntervalEnum.NEVER) {
@@ -116,23 +107,15 @@ class ScheduleDialog extends React.Component {
116107
newSchedule.day_of_week = WEEKDAYS_FULL[0];
117108
}
118109

119-
// reset count if out of new interval count range
120-
let count = this.state.count;
121-
if (this.getCounts(newInterval).indexOf(Number(count)) === -1) {
122-
count = '1';
123-
}
124-
125-
newSchedule.interval = newInterval
126-
? intervalToSeconds(Number(count), newInterval)
127-
: null;
110+
newSchedule.interval = newSeconds;
128111

129112
const [hour, minute] = newSchedule.time ?
130113
localizeTime(newSchedule.time).split(':')
131114
: [null, null];
132115

133116
this.setState({
134117
interval: newInterval,
135-
count,
118+
seconds: newSeconds,
136119
hour,
137120
minute,
138121
dayOfWeek: newSchedule.day_of_week
@@ -143,12 +126,6 @@ class ScheduleDialog extends React.Component {
143126
this.newSchedule = newSchedule;
144127
};
145128

146-
setCount = (newCount) => {
147-
const totalSeconds = intervalToSeconds(parseInt(newCount, 10), this.state.interval);
148-
this.setState({ count: newCount });
149-
this.newSchedule = { interval: totalSeconds };
150-
};
151-
152129
setScheduleUntil = (_, date) => {
153130
this.newSchedule = { until: date };
154131
};
@@ -182,7 +159,7 @@ class ScheduleDialog extends React.Component {
182159

183160
render() {
184161
const {
185-
interval, minute, hour, count, newSchedule: { until },
162+
interval, minute, hour, seconds, newSchedule: { until },
186163
} = this.state;
187164
const fixZIndex = { getPopupContainer: () => this.modalRef.current };
188165
const selectProps = {
@@ -202,17 +179,15 @@ class ScheduleDialog extends React.Component {
202179
<div className="schedule-component" ref={this.modalRef}>
203180
<h5>Refresh every</h5>
204181
<div>
205-
{interval !== IntervalEnum.NEVER ? (
206-
<Select value={count} onChange={this.setCount} {...selectProps}>
207-
{this.getCounts(this.state.interval).map(cnt => (
208-
<Option value={String(cnt)} key={cnt}>{cnt}</Option>
182+
<Select value={seconds} onChange={this.setInterval} {...selectProps}>
183+
<Option value={null} key="never">Never</Option>
184+
{Object.keys(this.intervals).map(int => (
185+
<OptGroup label={capitalize(int)} key={int}>
186+
{this.intervals[int].map(([cnt, secs]) => (
187+
<Option value={secs} key={cnt}>{cnt} {int}</Option>
188+
))}
189+
</OptGroup>
209190
))}
210-
</Select>
211-
) : null}
212-
<Select value={interval} onChange={this.setInterval} {...selectProps}>
213-
{this.intervals.map(iv => (
214-
<Option value={iv.name} key={iv.name}>{iv.name}</Option>
215-
))}
216191
</Select>
217192
</div>
218193
</div>

client/app/filters/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export function localizeTime(time) {
2020
}
2121

2222
export function secondsToInterval(seconds) {
23+
if (!seconds) {
24+
return { interval: IntervalEnum.NEVER };
25+
}
2326
let interval = IntervalEnum.MINUTES;
2427
let count = seconds / 60;
2528
if (count >= 60) {
@@ -30,7 +33,7 @@ export function secondsToInterval(seconds) {
3033
count /= 24;
3134
interval = IntervalEnum.DAYS;
3235
}
33-
if (count >= 7 && interval === IntervalEnum.DAYS) {
36+
if (count >= 7 && !(count % 7) && interval === IntervalEnum.DAYS) {
3437
count /= 7;
3538
interval = IntervalEnum.WEEKS;
3639
}

0 commit comments

Comments
 (0)
X Tutup