Skip to content

Commit 3ed3769

Browse files
gurinder39GurinderRawala
authored andcommitted
added presisted time range (#56)
1 parent bbca583 commit 3ed3769

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { merge, uniqBy } from 'lodash';
12
import React, { CSSProperties } from 'react';
2-
import { useSelector } from 'react-redux';
3-
import { uniqBy } from 'lodash';
3+
// eslint-disable-next-line no-restricted-imports
4+
import { useDispatch, useSelector } from 'react-redux';
45

56
import { t } from '@grafana/ui/src/utils/i18n';
67
import appEvents from 'app/core/app_events';
78
import { TimeRange, isDateTime, rangeUtil, AppEvents } from '@grafana/data';
89
import { TimeRangePickerProps, TimeRangePicker, useTheme2 } from '@grafana/ui';
9-
import { FnGlobalState } from 'app/core/reducers/fn-slice';
10+
import { FnGlobalState, updatePartialFnStates } from 'app/core/reducers/fn-slice';
1011
import { StoreState } from 'app/types';
1112

1213
import { LocalStorageValueProvider } from '../LocalStorageValueProvider';
@@ -35,6 +36,9 @@ const FnText: React.FC = () => {
3536
};
3637

3738
export const TimePickerWithHistory: React.FC<Props> = (props) => {
39+
const { fnGlobalTimeRange } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
40+
const dispatch = useDispatch();
41+
3842
return (
3943
<LocalStorageValueProvider<LSTimePickerHistoryItem[]> storageKey={LOCAL_STORAGE_KEY} defaultValue={[]}>
4044
{(rawValues, onSaveToStore) => {
@@ -45,7 +49,13 @@ export const TimePickerWithHistory: React.FC<Props> = (props) => {
4549
<TimeRangePicker
4650
{...props}
4751
history={history}
52+
{...merge({}, props, { value: fnGlobalTimeRange || props.value })}
4853
onChange={(value) => {
54+
dispatch(
55+
updatePartialFnStates({
56+
fnGlobalTimeRange: value,
57+
})
58+
);
4959
onAppendToHistory(value, values, onSaveToStore);
5060
props.onChange(value);
5161
}}

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

+3-1
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)