Commit 2f00087a by Tobias Skarhed Committed by Torkel Ödegaard

RefreshPicker: Handle empty intervals (#17585)

* Refresh picker empty

* RefreshPicker: refactoring
parent f9b691bd
...@@ -19,29 +19,20 @@ export interface Props { ...@@ -19,29 +19,20 @@ export interface Props {
} }
export class RefreshPicker extends PureComponent<Props> { export class RefreshPicker extends PureComponent<Props> {
static defaultProps = {
intervals: defaultIntervals,
};
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
} }
hasNoIntervals = () => { intervalsToOptions = (intervals: string[] | undefined): Array<SelectOptionItem<string>> => {
const { intervals } = this.props; const intervalsOrDefault = intervals || defaultIntervals;
// Current implementaion returns an array with length of 1 consisting of const options = intervalsOrDefault
// an empty string when auto-refresh is empty in dashboard settings .filter(str => str !== '')
if (!intervals || intervals.length < 1 || (intervals.length === 1 && intervals[0] === '')) { .map(interval => ({ label: interval, value: interval }));
return true;
}
return false;
};
intervalsToOptions = (intervals: string[] = defaultIntervals): Array<SelectOptionItem<string>> => {
const options = intervals.map(interval => ({ label: interval, value: interval }));
if (this.props.hasLiveOption) { if (this.props.hasLiveOption) {
options.unshift(liveOption); options.unshift(liveOption);
} }
options.unshift(offOption); options.unshift(offOption);
return options; return options;
}; };
...@@ -56,7 +47,7 @@ export class RefreshPicker extends PureComponent<Props> { ...@@ -56,7 +47,7 @@ export class RefreshPicker extends PureComponent<Props> {
render() { render() {
const { onRefresh, intervals, tooltip, value } = this.props; const { onRefresh, intervals, tooltip, value } = this.props;
const options = this.intervalsToOptions(this.hasNoIntervals() ? defaultIntervals : intervals); const options = this.intervalsToOptions(intervals);
const currentValue = value || ''; const currentValue = value || '';
const selectedValue = options.find(item => item.value === currentValue) || offOption; const selectedValue = options.find(item => item.value === currentValue) || offOption;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment