Skip to content

Commit b9f9432

Browse files
committed
refactor context initilization
1 parent b3c517c commit b9f9432

File tree

8 files changed

+26
-89
lines changed

8 files changed

+26
-89
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import { UptimeDatePicker } from '../uptime_date_picker';
1010

1111
describe('UptimeDatePicker component', () => {
1212
it('validates props with shallow render', () => {
13-
const component = shallowWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
13+
const component = shallowWithIntl(<UptimeDatePicker />);
1414
expect(component).toMatchSnapshot();
1515
});
1616

1717
it('renders properly with mock data', () => {
18-
const component = renderWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
18+
const component = renderWithIntl(<UptimeDatePicker />);
1919
expect(component).toMatchSnapshot();
2020
});
2121

2222
it('renders properly without commonlyUsedRanges prop', () => {
23-
const component = renderWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
23+
const component = renderWithIntl(<UptimeDatePicker />);
2424
expect(component).toMatchSnapshot();
2525
});
2626
});

x-pack/legacy/plugins/uptime/public/components/functional/monitor_list/monitor_list_drawer/monitor_status_row.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import React, { useContext } from 'react';
88
import { EuiHealth, EuiSpacer } from '@elastic/eui';
99
import { FormattedMessage } from '@kbn/i18n/react';
10-
import { UptimeSettingsContext } from '../../../../contexts';
10+
import { UptimeThemeContext } from '../../../../contexts';
1111
import { UNNAMED_LOCATION, UP } from './monitor_status_list';
1212

1313
interface MonitorStatusRowProps {
@@ -24,7 +24,7 @@ interface MonitorStatusRowProps {
2424
export const MonitorStatusRow = ({ locationNames, status }: MonitorStatusRowProps) => {
2525
const {
2626
colors: { success, danger },
27-
} = useContext(UptimeSettingsContext);
27+
} = useContext(UptimeThemeContext);
2828

2929
const color = status === UP ? success : danger;
3030

x-pack/legacy/plugins/uptime/public/components/functional/uptime_date_picker.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EuiSuperDatePicker } from '@elastic/eui';
88
import React, { useContext } from 'react';
99
import { useUrlParams } from '../../hooks';
1010
import { CLIENT_DEFAULTS } from '../../../common/constants';
11-
import { UptimeSettingsContext } from '../../contexts';
11+
import { UptimeRefreshContext, UptimeSettingsContext } from '../../contexts';
1212

1313
// TODO: when EUI exports types for this, this should be replaced
1414
interface SuperDateRangePickerRangeChangedEvent {
@@ -27,14 +27,11 @@ export interface CommonlyUsedRange {
2727
display: string;
2828
}
2929

30-
interface UptimeDatePickerProps {
31-
refreshApp: () => void;
32-
}
33-
34-
export const UptimeDatePicker = ({ refreshApp }: UptimeDatePickerProps) => {
30+
export const UptimeDatePicker = () => {
3531
const [getUrlParams, updateUrl] = useUrlParams();
3632
const { autorefreshInterval, autorefreshIsPaused, dateRangeStart, dateRangeEnd } = getUrlParams();
3733
const { commonlyUsedRanges } = useContext(UptimeSettingsContext);
34+
const { refreshApp } = useContext(UptimeRefreshContext);
3835

3936
const euiCommonlyUsedRanges = commonlyUsedRanges
4037
? commonlyUsedRanges.map(

x-pack/legacy/plugins/uptime/public/contexts/uptime_refresh_context.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,25 @@
55
*/
66

77
import React, { createContext, useMemo, useState } from 'react';
8-
import { History } from 'history';
98
import { useHistory, useLocation } from 'react-router-dom';
109
import { store } from '../state';
1110
import { triggerAppRefresh } from '../state/actions';
1211

13-
interface Location {
14-
pathname: string;
15-
search: string;
16-
}
17-
1812
interface UMRefreshContext {
1913
lastRefresh: number;
20-
history: History | undefined;
21-
location: Location | undefined;
2214
refreshApp: () => void;
2315
}
2416

2517
const defaultContext: UMRefreshContext = {
2618
lastRefresh: 0,
27-
history: undefined,
28-
location: undefined,
2919
refreshApp: () => {
3020
throw new Error('App refresh was not initialized, set it when you invoke the context');
3121
},
3222
};
3323

3424
export const UptimeRefreshContext = createContext(defaultContext);
3525

36-
interface ContextProps {
37-
lastRefresh: number;
38-
}
39-
40-
export const UptimeRefreshContextProvider: React.FC<ContextProps> = ({ children }) => {
26+
export const UptimeRefreshContextProvider: React.FC = ({ children }) => {
4127
const history = useHistory();
4228
const location = useLocation();
4329

x-pack/legacy/plugins/uptime/public/contexts/uptime_settings_context.tsx

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

7-
import DateMath from '@elastic/datemath';
87
import React, { createContext, useMemo } from 'react';
98
import { useParams } from 'react-router-dom';
109
import { UptimeAppProps } from '../uptime_app';
1110
import { CONTEXT_DEFAULTS } from '../../common/constants';
1211
import { CommonlyUsedRange } from '../components/functional/uptime_date_picker';
1312

1413
export interface UMSettingsContextValues {
15-
absoluteStartDate: number;
16-
absoluteEndDate: number;
17-
autorefreshIsPaused: boolean;
18-
autorefreshInterval: number;
1914
basePath: string;
2015
dateRangeStart: string;
2116
dateRangeEnd: string;
@@ -25,27 +20,13 @@ export interface UMSettingsContextValues {
2520
commonlyUsedRanges?: CommonlyUsedRange[];
2621
}
2722

28-
const {
29-
AUTOREFRESH_IS_PAUSED,
30-
AUTOREFRESH_INTERVAL,
31-
BASE_PATH,
32-
DATE_RANGE_START,
33-
DATE_RANGE_END,
34-
} = CONTEXT_DEFAULTS;
35-
const parsedStart = DateMath.parse(DATE_RANGE_START);
36-
const parsedEnd = DateMath.parse(DATE_RANGE_END);
37-
const DEFAULT_ABSOLUTE_START_DATE = parsedStart ? parsedStart.valueOf() : 0;
38-
const DEFAULT_ABSOLUTE_END_DATE = parsedEnd ? parsedEnd.valueOf() : 1;
23+
const { BASE_PATH, DATE_RANGE_START, DATE_RANGE_END } = CONTEXT_DEFAULTS;
3924

4025
/**
4126
* These are default values for the context. These defaults are typically
4227
* overwritten by the Uptime App upon its invocation.
4328
*/
4429
const defaultContext: UMSettingsContextValues = {
45-
absoluteStartDate: DEFAULT_ABSOLUTE_START_DATE,
46-
absoluteEndDate: DEFAULT_ABSOLUTE_END_DATE,
47-
autorefreshIsPaused: AUTOREFRESH_IS_PAUSED,
48-
autorefreshInterval: AUTOREFRESH_INTERVAL,
4930
basePath: BASE_PATH,
5031
dateRangeStart: DATE_RANGE_START,
5132
dateRangeEnd: DATE_RANGE_END,
@@ -58,33 +39,18 @@ export const UptimeSettingsContext = createContext(defaultContext);
5839
export const UptimeSettingsContextProvider: React.FC<UptimeAppProps> = ({ children, ...props }) => {
5940
const { basePath, isApmAvailable, isInfraAvailable, isLogsAvailable } = props;
6041

61-
const { autorefreshInterval, autorefreshIsPaused, dateRangeStart, dateRangeEnd } = useParams();
42+
const { dateRangeStart, dateRangeEnd } = useParams();
6243

6344
const value = useMemo(() => {
64-
const absoluteStartDate = DateMath.parse(dateRangeStart ?? '');
65-
const absoluteEndDate = DateMath.parse(dateRangeEnd ?? '');
6645
return {
67-
absoluteStartDate: absoluteStartDate ? absoluteStartDate.valueOf() : 0,
68-
absoluteEndDate: absoluteEndDate ? absoluteEndDate.valueOf() : 1,
69-
autorefreshInterval,
70-
autorefreshIsPaused,
7146
basePath,
72-
dateRangeStart,
73-
dateRangeEnd,
7447
isApmAvailable,
7548
isInfraAvailable,
7649
isLogsAvailable,
50+
dateRangeStart: dateRangeStart ?? DATE_RANGE_START,
51+
dateRangeEnd: dateRangeEnd ?? DATE_RANGE_END,
7752
};
78-
}, [
79-
autorefreshInterval,
80-
autorefreshIsPaused,
81-
dateRangeStart,
82-
dateRangeEnd,
83-
basePath,
84-
isApmAvailable,
85-
isInfraAvailable,
86-
isLogsAvailable,
87-
]);
53+
}, [basePath, isApmAvailable, isInfraAvailable, isLogsAvailable, dateRangeStart, dateRangeEnd]);
8854

8955
return <UptimeSettingsContext.Provider value={value} children={children} />;
9056
};

x-pack/legacy/plugins/uptime/public/hooks/__tests__/use_url_params.test.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ describe('useUrlParams', () => {
6767

6868
it('accepts router props, updates URL params, and returns the current params', () => {
6969
const component = mountWithIntl(
70-
<UptimeRefreshContext.Provider
71-
value={{ lastRefresh: 123, history: mockRouter.history, location: mockRouter.location }}
72-
>
70+
<UptimeRefreshContext.Provider value={{ lastRefresh: 123, refreshApp: jest.fn() }}>
7371
<UseUrlParamsTestComponent hook={useUrlParams} />
7472
</UptimeRefreshContext.Provider>
7573
);
@@ -88,11 +86,7 @@ describe('useUrlParams', () => {
8886
<UptimeRefreshContext.Provider
8987
value={{
9088
lastRefresh: 123,
91-
history: mockRouter.history,
92-
location: {
93-
...mockRouter.location,
94-
search: 'g=%22%22&dateRangeStart=now-19d&dateRangeEnd=now-1m',
95-
},
89+
refreshApp: jest.fn(),
9690
}}
9791
>
9892
<UseUrlParamsTestComponent hook={useUrlParams} />
@@ -111,8 +105,7 @@ describe('useUrlParams', () => {
111105
<UptimeRefreshContext.Provider
112106
value={{
113107
lastRefresh: 123,
114-
history: mockRouter.history,
115-
location: mockRouter.location,
108+
refreshApp: jest.fn(),
116109
}}
117110
>
118111
<UseUrlParamsTestComponent hook={useUrlParams} updateParams={{ pagination: '' }} />

x-pack/legacy/plugins/uptime/public/hooks/use_url_params.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66

77
import qs from 'querystring';
8-
import { useContext } from 'react';
9-
import { UptimeRefreshContext } from '../contexts';
8+
import { useLocation, useHistory } from 'react-router-dom';
109
import { UptimeUrlParams, getSupportedUrlParams } from '../lib/helper';
1110

1211
type GetUrlParams = () => UptimeUrlParams;
@@ -15,24 +14,22 @@ type UpdateUrlParams = (updatedParams: { [key: string]: string | number | boolea
1514
export type UptimeUrlParamsHook = () => [GetUrlParams, UpdateUrlParams];
1615

1716
export const useUrlParams: UptimeUrlParamsHook = () => {
18-
const refreshContext = useContext(UptimeRefreshContext);
17+
const location = useLocation();
18+
const history = useHistory();
1919

2020
const getUrlParams: GetUrlParams = () => {
2121
let search: string | undefined;
22-
if (refreshContext.location) {
23-
search = refreshContext.location.search;
22+
if (location) {
23+
search = location.search;
2424
}
2525

2626
const params = search ? { ...qs.parse(search[0] === '?' ? search.slice(1) : search) } : {};
2727
return getSupportedUrlParams(params);
2828
};
2929

3030
const updateUrlParams: UpdateUrlParams = updatedParams => {
31-
if (!refreshContext.history || !refreshContext.location) return;
32-
const {
33-
history,
34-
location: { pathname, search },
35-
} = refreshContext;
31+
if (!history || !location) return;
32+
const { pathname, search } = location;
3633
const currentParams: any = qs.parse(search[0] === '?' ? search.slice(1) : search);
3734
const mergedParams = {
3835
...currentParams,

x-pack/legacy/plugins/uptime/public/pages/page_header.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { EuiTitle, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
8-
import React, { useEffect, useState, useContext } from 'react';
8+
import React, { useEffect, useState } from 'react';
99
import { connect } from 'react-redux';
1010
import { useRouteMatch, useParams } from 'react-router-dom';
1111
import { i18n } from '@kbn/i18n';
@@ -14,7 +14,6 @@ import { AppState } from '../state';
1414
import { selectSelectedMonitor } from '../state/selectors';
1515
import { getMonitorPageBreadcrumb, getOverviewPageBreadcrumbs } from '../breadcrumbs';
1616
import { stringifyUrlParams } from '../lib/helper/stringify_url_params';
17-
import { UptimeSettingsContext } from '../contexts';
1817
import { getTitle } from '../lib/helper/get_title';
1918
import { UMUpdateBreadcrumbs } from '../lib/lib';
2019
import { MONITOR_ROUTE } from '../routes';
@@ -28,7 +27,6 @@ export const PageHeaderComponent = ({ monitorStatus, setBreadcrumbs }: PageHeade
2827
const monitorPage = useRouteMatch({
2928
path: MONITOR_ROUTE,
3029
});
31-
const { refreshApp } = useContext(UptimeSettingsContext);
3230

3331
const { absoluteDateRangeStart, absoluteDateRangeEnd, ...params } = useParams();
3432

@@ -74,7 +72,7 @@ export const PageHeaderComponent = ({ monitorStatus, setBreadcrumbs }: PageHeade
7472
</EuiTitle>
7573
</EuiFlexItem>
7674
<EuiFlexItem grow={false}>
77-
<UptimeDatePicker refreshApp={refreshApp} />
75+
<UptimeDatePicker />
7876
</EuiFlexItem>
7977
</EuiFlexGroup>
8078
<EuiSpacer size="s" />

0 commit comments

Comments
 (0)