Skip to content

Commit 31fbabd

Browse files
update date picker component (#79418)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent c776193 commit 31fbabd

File tree

4 files changed

+140
-17
lines changed

4 files changed

+140
-17
lines changed

x-pack/plugins/uptime/public/components/common/__tests__/uptime_date_picker.test.tsx

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66

77
import React from 'react';
88
import { UptimeDatePicker } from '../uptime_date_picker';
9-
import { renderWithRouter, shallowWithRouter, MountWithReduxProvider } from '../../../lib';
9+
import {
10+
renderWithRouter,
11+
shallowWithRouter,
12+
MountWithReduxProvider,
13+
mountWithRouterRedux,
14+
} from '../../../lib';
15+
import { UptimeStartupPluginsContextProvider } from '../../../contexts';
16+
import { startPlugins } from '../../../lib/__mocks__/uptime_plugin_start_mock';
17+
import { ClientPluginsStart } from '../../../apps/plugin';
18+
import { createMemoryHistory } from 'history';
1019

1120
describe('UptimeDatePicker component', () => {
1221
it('validates props with shallow render', () => {
@@ -22,4 +31,59 @@ describe('UptimeDatePicker component', () => {
2231
);
2332
expect(component).toMatchSnapshot();
2433
});
34+
35+
it('uses shared date range state when there is no url date range state', () => {
36+
const customHistory = createMemoryHistory();
37+
jest.spyOn(customHistory, 'push');
38+
39+
const component = mountWithRouterRedux(
40+
<UptimeStartupPluginsContextProvider
41+
{...((startPlugins as unknown) as Partial<ClientPluginsStart>)}
42+
>
43+
<UptimeDatePicker />
44+
</UptimeStartupPluginsContextProvider>,
45+
{ customHistory }
46+
);
47+
48+
const startBtn = component.find('[data-test-subj="superDatePickerstartDatePopoverButton"]');
49+
50+
expect(startBtn.text()).toBe('~ 30 minutes ago');
51+
52+
const endBtn = component.find('[data-test-subj="superDatePickerendDatePopoverButton"]');
53+
54+
expect(endBtn.text()).toBe('~ 15 minutes ago');
55+
56+
expect(customHistory.push).toHaveBeenCalledWith({
57+
pathname: '/',
58+
search: 'dateRangeStart=now-30m&dateRangeEnd=now-15m',
59+
});
60+
});
61+
62+
it('should use url date range even if shared date range is present', () => {
63+
const customHistory = createMemoryHistory({
64+
initialEntries: ['/?g=%22%22&dateRangeStart=now-10m&dateRangeEnd=now'],
65+
});
66+
67+
jest.spyOn(customHistory, 'push');
68+
69+
const component = mountWithRouterRedux(
70+
<UptimeStartupPluginsContextProvider
71+
{...((startPlugins as unknown) as Partial<ClientPluginsStart>)}
72+
>
73+
<UptimeDatePicker />
74+
</UptimeStartupPluginsContextProvider>,
75+
{ customHistory }
76+
);
77+
78+
const showDateBtn = component.find('[data-test-subj="superDatePickerShowDatesButton"]');
79+
80+
expect(showDateBtn.childAt(0).text()).toBe('Last 10 minutes');
81+
82+
// it should update shared state
83+
84+
expect(startPlugins.data.query.timefilter.timefilter.setTime).toHaveBeenCalledWith({
85+
from: 'now-10m',
86+
to: 'now',
87+
});
88+
});
2589
});

x-pack/plugins/uptime/public/components/common/uptime_date_picker.tsx

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,59 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { useContext } from 'react';
7+
import React, { useContext, useEffect } from 'react';
88
import { EuiSuperDatePicker } from '@elastic/eui';
99
import { useUrlParams } from '../../hooks';
1010
import { CLIENT_DEFAULTS } from '../../../common/constants';
11-
import { UptimeRefreshContext, UptimeSettingsContext } from '../../contexts';
11+
import {
12+
UptimeRefreshContext,
13+
UptimeSettingsContext,
14+
UptimeStartupPluginsContext,
15+
} from '../../contexts';
1216

1317
export interface CommonlyUsedRange {
1418
from: string;
1519
to: string;
1620
display: string;
1721
}
1822

23+
const isUptimeDefaultDateRange = (dateRangeStart: string, dateRangeEnd: string) => {
24+
const { DATE_RANGE_START, DATE_RANGE_END } = CLIENT_DEFAULTS;
25+
26+
return dateRangeStart === DATE_RANGE_START && dateRangeEnd === DATE_RANGE_END;
27+
};
28+
1929
export const UptimeDatePicker = () => {
2030
const [getUrlParams, updateUrl] = useUrlParams();
21-
const { autorefreshInterval, autorefreshIsPaused, dateRangeStart, dateRangeEnd } = getUrlParams();
2231
const { commonlyUsedRanges } = useContext(UptimeSettingsContext);
2332
const { refreshApp } = useContext(UptimeRefreshContext);
2433

34+
const { data } = useContext(UptimeStartupPluginsContext);
35+
36+
// read time from state and update the url
37+
const sharedTimeState = data?.query.timefilter.timefilter.getTime();
38+
39+
const {
40+
autorefreshInterval,
41+
autorefreshIsPaused,
42+
dateRangeStart: start,
43+
dateRangeEnd: end,
44+
} = getUrlParams();
45+
46+
useEffect(() => {
47+
const { from, to } = sharedTimeState ?? {};
48+
// if it's uptime default range, and we have shared state from kibana, let's use that
49+
if (isUptimeDefaultDateRange(start, end) && (from !== start || to !== end)) {
50+
updateUrl({ dateRangeStart: from, dateRangeEnd: to });
51+
} else if (from !== start || to !== end) {
52+
// if it's coming url. let's update shared state
53+
data?.query.timefilter.timefilter.setTime({ from: start, to: end });
54+
}
55+
56+
// only need at start, rest date picker on change fucn will take care off
57+
// eslint-disable-next-line react-hooks/exhaustive-deps
58+
}, []);
59+
2560
const euiCommonlyUsedRanges = commonlyUsedRanges
2661
? commonlyUsedRanges.map(
2762
({ from, to, display }: { from: string; to: string; display: string }) => {
@@ -36,13 +71,17 @@ export const UptimeDatePicker = () => {
3671

3772
return (
3873
<EuiSuperDatePicker
39-
start={dateRangeStart}
40-
end={dateRangeEnd}
74+
start={start}
75+
end={end}
4176
commonlyUsedRanges={euiCommonlyUsedRanges}
4277
isPaused={autorefreshIsPaused}
4378
refreshInterval={autorefreshInterval}
44-
onTimeChange={({ start, end }) => {
45-
updateUrl({ dateRangeStart: start, dateRangeEnd: end });
79+
onTimeChange={({ start: startN, end: endN }) => {
80+
if (data?.query?.timefilter?.timefilter) {
81+
data?.query.timefilter.timefilter.setTime({ from: startN, to: endN });
82+
}
83+
84+
updateUrl({ dateRangeStart: startN, dateRangeEnd: endN });
4685
refreshApp();
4786
}}
4887
onRefresh={refreshApp}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
interface InputTimeRange {
8+
from: string;
9+
to: string;
10+
}
11+
12+
export const startPlugins = {
13+
data: {
14+
query: {
15+
timefilter: {
16+
timefilter: {
17+
getTime: () => ({ to: 'now-15m', from: 'now-30m' }),
18+
setTime: jest.fn(({ from, to }: InputTimeRange) => {}),
19+
},
20+
},
21+
},
22+
},
23+
};

x-pack/plugins/uptime/public/lib/helper/helper_with_router.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,19 @@ const helperWithRouter: <R>(
2020
wrapReduxStore?: boolean,
2121
storeState?: AppState
2222
) => R = (helper, component, customHistory, wrapReduxStore, storeState) => {
23-
if (customHistory) {
24-
customHistory.location.key = 'TestKeyForTesting';
25-
return helper(<Router history={customHistory}>{component}</Router>);
26-
}
27-
const history = createMemoryHistory();
23+
const history = customHistory ?? createMemoryHistory();
24+
2825
history.location.key = 'TestKeyForTesting';
2926

27+
const routerWrapper = <Router history={history}>{component}</Router>;
28+
3029
if (wrapReduxStore) {
3130
return helper(
32-
<MountWithReduxProvider store={storeState}>
33-
<Router history={history}>{component}</Router>
34-
</MountWithReduxProvider>
31+
<MountWithReduxProvider store={storeState}>{routerWrapper}</MountWithReduxProvider>
3532
);
3633
}
3734

38-
return helper(<Router history={history}>{component}</Router>);
35+
return helper(routerWrapper);
3936
};
4037

4138
export const renderWithRouter = (component: ReactElement, customHistory?: MemoryHistory) => {

0 commit comments

Comments
 (0)