From 65653ff748e53280bd5c413c11a15a1bf74ed4cf Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Tue, 14 Mar 2023 17:09:28 -0400 Subject: [PATCH] fix --- .../TimePicker/TimePickerWithHistory.tsx | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/public/app/core/components/TimePicker/TimePickerWithHistory.tsx b/public/app/core/components/TimePicker/TimePickerWithHistory.tsx index cb0d84bcc3583..c0ff897f4afd3 100644 --- a/public/app/core/components/TimePicker/TimePickerWithHistory.tsx +++ b/public/app/core/components/TimePicker/TimePickerWithHistory.tsx @@ -1,5 +1,5 @@ import { merge } from 'lodash'; -import React, { CSSProperties } from 'react'; +import React, { CSSProperties, FC, useEffect } from 'react'; // eslint-disable-next-line no-restricted-imports import { useDispatch, useSelector } from 'react-redux'; @@ -23,31 +23,48 @@ const FnText: React.FC = () => { return <>{FNDashboard ? UTC : ''}; }; -export const TimePickerWithHistory: React.FC = (props) => { +export const TimePickerWithHistory: FC = (props) => ( + storageKey={LOCAL_STORAGE_KEY} defaultValue={[]}> + {(values, onSaveToStore) => { + return ; + }} + +); + +export interface PickerProps { + values: TimeRange[]; + onSaveToStore: (value: TimeRange[]) => void; + pickerProps: Props; +} + +export const Picker: FC = ({ values, onSaveToStore, pickerProps }) => { const { fnGlobalTimeRange } = useSelector(({ fnGlobalState }) => fnGlobalState); const dispatch = useDispatch(); + useEffect(() => { + if (!fnGlobalTimeRange) { + return; + } + onAppendToHistory(fnGlobalTimeRange, values, onSaveToStore); + pickerProps.onChange(fnGlobalTimeRange); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + return ( - storageKey={LOCAL_STORAGE_KEY} defaultValue={[]}> - {(values, onSaveToStore) => { - return ( - { - dispatch( - updatePartialFnStates({ - fnGlobalTimeRange: value, - }) - ); - onAppendToHistory(value, values, onSaveToStore); - props.onChange(value); - }} - fnText={} - /> + { + dispatch( + updatePartialFnStates({ + fnGlobalTimeRange: value, + }) ); + onAppendToHistory(value, values, onSaveToStore); + pickerProps.onChange(value); }} - + fnText={} + /> ); }; @@ -69,6 +86,7 @@ function onAppendToHistory(toAppend: TimeRange, values: TimeRange[], onSaveToSto if (!isAbsolute(toAppend)) { return; } + const toStore = limit([toAppend, ...values]); onSaveToStore(toStore); }