Fix: when reloading page make sure that time picker history is converted to dateTime.

This commit is contained in:
Marcus Andersson 2020-03-05 13:14:42 +01:00 committed by GitHub
parent ea3d368e91
commit d227353321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import React from 'react';
import { LocalStorageValueProvider } from '../LocalStorageValueProvider';
import { TimeRange, isDateTime } from '@grafana/data';
import { TimeRange, isDateTime, dateTime } from '@grafana/data';
import { Props as TimePickerProps, TimePicker } from '@grafana/ui/src/components/TimePicker/TimePicker';
const LOCAL_STORAGE_KEY = 'grafana.dashboard.timepicker.history';
@ -14,7 +14,7 @@ export const TimePickerWithHistory: React.FC<Props> = props => {
return (
<TimePicker
{...props}
history={values}
history={convertIfJson(values)}
onChange={value => {
onAppendToHistory(value, values, onSaveToStore);
props.onChange(value);
@ -25,6 +25,21 @@ export const TimePickerWithHistory: React.FC<Props> = props => {
</LocalStorageValueProvider>
);
};
function convertIfJson(history: TimeRange[]): TimeRange[] {
return history.map(time => {
if (isDateTime(time.from)) {
return time;
}
return {
from: dateTime(time.from),
to: dateTime(time.to),
raw: time.raw,
};
});
}
function onAppendToHistory(toAppend: TimeRange, values: TimeRange[], onSaveToStore: (values: TimeRange[]) => void) {
if (!isAbsolute(toAppend)) {
return;