Skip to content

Commit 9d90474

Browse files
authored
Fix timerange <> global timerange issues (#76)
1 parent 54feedf commit 9d90474

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { merge, uniqBy } from 'lodash';
2-
import React, { CSSProperties, FC, useEffect } from 'react';
1+
import { isEqual, uniqBy } from 'lodash';
2+
import React, { CSSProperties, FC, useEffect, useRef } from 'react';
33
// eslint-disable-next-line no-restricted-imports
44
import { useDispatch, useSelector } from 'react-redux';
55

@@ -41,24 +41,31 @@ export const Picker: FC<PickerProps> = ({ values, onSaveToStore, pickerProps })
4141
const { fnGlobalTimeRange } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
4242
const dispatch = useDispatch();
4343

44+
const didMountRef = useRef(false);
4445
useEffect(() => {
45-
if (!fnGlobalTimeRange) {
46-
return;
46+
/* The condition below skips the first run of useeffect that happens when this component gets mounted */
47+
if (didMountRef.current) {
48+
/* If the current timerange value has changed, update fnGlobalTimeRange */
49+
if (!isEqual(fnGlobalTimeRange?.raw, pickerProps.value.raw)) {
50+
dispatch(
51+
updatePartialFnStates({
52+
fnGlobalTimeRange: pickerProps.value,
53+
})
54+
);
55+
}
4756
}
48-
onAppendToHistory(fnGlobalTimeRange, values, onSaveToStore);
49-
pickerProps.onChange(fnGlobalTimeRange);
50-
}, [fnGlobalTimeRange, onSaveToStore, pickerProps, values]);
57+
58+
didMountRef.current = true;
59+
}, [dispatch, fnGlobalTimeRange?.raw, pickerProps.value]);
5160

5261
return (
5362
<TimeRangePicker
54-
{...merge({}, pickerProps, { value: pickerProps.value })}
63+
{...pickerProps}
64+
value={fnGlobalTimeRange || pickerProps.value}
5565
history={convertIfJson(values)}
5666
onChange={(value) => {
57-
dispatch(
58-
updatePartialFnStates({
59-
fnGlobalTimeRange: value,
60-
})
61-
);
67+
onAppendToHistory(value, values, onSaveToStore);
68+
pickerProps.onChange(value);
6269
}}
6370
fnText={<FnText />}
6471
/>

0 commit comments

Comments
 (0)