From 8875b6402684e0797f050c8e2416a532b38bd7db Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 28 Jan 2020 10:40:14 +0100 Subject: [PATCH 1/3] [ML] Fix persist/restore of refreshInterval in globalState. --- .../navigation_menu/top_nav/top_nav.tsx | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx index eb068f40716bc..e202c6c44f697 100644 --- a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx +++ b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx @@ -15,6 +15,7 @@ import { mlTimefilterTimeChange$, } from '../../../services/timefilter_refresh_service'; import { useUiContext } from '../../../contexts/ui/use_ui_context'; +import { useUrlState } from '../../../util/url_state'; interface Duration { start: string; @@ -40,9 +41,17 @@ function updateLastRefresh(timeRange: OnRefreshProps) { export const TopNav: FC = () => { const { chrome, timefilter, timeHistory } = useUiContext(); + const [globalState, setGlobalState] = useUrlState('_g'); const getRecentlyUsedRanges = getRecentlyUsedRangesFactory(timeHistory); - const [refreshInterval, setRefreshInterval] = useState(timefilter.getRefreshInterval()); + const [refreshInterval, setRefreshInterval] = useState( + globalState?.refreshInterval ?? timefilter.getRefreshInterval() + ); + useEffect(() => { + setGlobalState({ refreshInterval }); + timefilter.setRefreshInterval(refreshInterval); + }, [refreshInterval.pause, refreshInterval.value]); + const [time, setTime] = useState(timefilter.getTime()); const [recentlyUsedRanges, setRecentlyUsedRanges] = useState(getRecentlyUsedRanges()); const [isAutoRefreshSelectorEnabled, setIsAutoRefreshSelectorEnabled] = useState( @@ -96,20 +105,13 @@ export const TopNav: FC = () => { } function updateInterval({ - isPaused, - refreshInterval: interval, + isPaused: pause, + refreshInterval: value, }: { isPaused: boolean; refreshInterval: number; }) { - const newInterval = { - pause: isPaused, - value: interval, - }; - // Update timefilter for controllers listening for changes - timefilter.setRefreshInterval(newInterval); - // Update state - setRefreshInterval(newInterval); + setRefreshInterval({ pause, value }); } return ( From e3d76bf4cc87ccd07527cd6b34dc37adadb84c39 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 28 Jan 2020 12:46:18 +0100 Subject: [PATCH 2/3] [ML] Fix jest tests. --- .../__snapshots__/top_nav.test.tsx.snap | 76 ------------------- .../navigation_menu/top_nav/top_nav.test.tsx | 12 ++- 2 files changed, 9 insertions(+), 79 deletions(-) delete mode 100644 x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/__snapshots__/top_nav.test.tsx.snap diff --git a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/__snapshots__/top_nav.test.tsx.snap b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/__snapshots__/top_nav.test.tsx.snap deleted file mode 100644 index f9df085d2cbe7..0000000000000 --- a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/__snapshots__/top_nav.test.tsx.snap +++ /dev/null @@ -1,76 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Navigation Menu: Minimal initialization. 1`] = ` - -
- -
-
-`; diff --git a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.test.tsx b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.test.tsx index b64cccc9eb9b9..e9bec02868b71 100644 --- a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.test.tsx +++ b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.test.tsx @@ -4,8 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { mount, shallow } from 'enzyme'; +import { mount } from 'enzyme'; import React from 'react'; +import { MemoryRouter } from 'react-router-dom'; import { EuiSuperDatePicker } from '@elastic/eui'; @@ -34,8 +35,13 @@ describe('Navigation Menu: ', () => { const refreshListener = jest.fn(); const refreshSubscription = mlTimefilterRefresh$.subscribe(refreshListener); - const wrapper = shallow(); - expect(wrapper).toMatchSnapshot(); + const wrapper = mount( + + + + ); + expect(wrapper.find(TopNav)).toHaveLength(1); + expect(wrapper.find('EuiSuperDatePicker')).toHaveLength(1); expect(refreshListener).toBeCalledTimes(0); refreshSubscription.unsubscribe(); From b724e4f31281f71b6e7b143273175a556216e8aa Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 28 Jan 2020 14:20:06 +0100 Subject: [PATCH 3/3] [ML] Fix useEffect refreshInterval comparator. --- .../application/components/navigation_menu/top_nav/top_nav.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx index e202c6c44f697..c76967455fa42 100644 --- a/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx +++ b/x-pack/legacy/plugins/ml/public/application/components/navigation_menu/top_nav/top_nav.tsx @@ -50,7 +50,7 @@ export const TopNav: FC = () => { useEffect(() => { setGlobalState({ refreshInterval }); timefilter.setRefreshInterval(refreshInterval); - }, [refreshInterval.pause, refreshInterval.value]); + }, [refreshInterval?.pause, refreshInterval?.value]); const [time, setTime] = useState(timefilter.getTime()); const [recentlyUsedRanges, setRecentlyUsedRanges] = useState(getRecentlyUsedRanges());