Skip to content

Commit a98c384

Browse files
authored
added presisted time range (#56)
1 parent ac72771 commit a98c384

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

public/app/core/components/TimePicker/TimePickerWithHistory.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { merge } from 'lodash';
12
import React, { CSSProperties } from 'react';
2-
import { useSelector } from 'react-redux';
3+
// eslint-disable-next-line no-restricted-imports
4+
import { useDispatch, useSelector } from 'react-redux';
35

46
import { TimeRange, isDateTime, toUtc } from '@grafana/data';
57
import { TimeRangePickerProps, TimeRangePicker, useTheme2 } from '@grafana/ui';
6-
import { FnGlobalState } from 'app/core/reducers/fn-slice';
8+
import { FnGlobalState, updatePartialFnStates } from 'app/core/reducers/fn-slice';
79
import { StoreState } from 'app/types';
810

911
import { LocalStorageValueProvider } from '../LocalStorageValueProvider';
@@ -22,14 +24,22 @@ const FnText: React.FC = () => {
2224
};
2325

2426
export const TimePickerWithHistory: React.FC<Props> = (props) => {
27+
const { fnGlobalTimeRange } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
28+
const dispatch = useDispatch();
29+
2530
return (
2631
<LocalStorageValueProvider<TimeRange[]> storageKey={LOCAL_STORAGE_KEY} defaultValue={[]}>
2732
{(values, onSaveToStore) => {
2833
return (
2934
<TimeRangePicker
30-
{...props}
35+
{...merge({}, props, { value: fnGlobalTimeRange || props.value })}
3136
history={convertIfJson(values)}
3237
onChange={(value) => {
38+
dispatch(
39+
updatePartialFnStates({
40+
fnGlobalTimeRange: value,
41+
})
42+
);
3343
onAppendToHistory(value, values, onSaveToStore);
3444
props.onChange(value);
3545
}}

public/app/core/reducers/fn-slice.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createSlice, PayloadAction, SliceCaseReducers } from '@reduxjs/toolkit';
22

3-
import { GrafanaThemeType } from '@grafana/data';
3+
import { GrafanaThemeType, TimeRange } from '@grafana/data';
44

55
import { AnyObject } from '../../fn-app/types';
66

@@ -13,6 +13,7 @@ export interface FnGlobalState {
1313
pageTitle: string;
1414
queryParams: AnyObject;
1515
hiddenVariables: readonly string[];
16+
fnGlobalTimeRange: TimeRange | null;
1617
}
1718

1819
export type UpdateFNGlobalStateAction = PayloadAction<Partial<FnGlobalState>>;
@@ -55,6 +56,7 @@ export const INITIAL_FN_STATE: FnGlobalState = {
5556
pageTitle: '',
5657
queryParams: {},
5758
hiddenVariables: [],
59+
fnGlobalTimeRange: null,
5860
} as const;
5961

6062
const reducers: SliceCaseReducers<FnGlobalState> = {

0 commit comments

Comments
 (0)