-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Anomaly Detection: Fixes hiding date picker for settings pages. (#…
…57544) (#57790) - Fixes hiding the global date picker on anomaly detection settings pages. - Consolidates various timefilter usages for enabling/disabling the date picker into a useTimefilter() hook.
- Loading branch information
Showing
17 changed files
with
139 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
x-pack/legacy/plugins/ml/public/application/contexts/kibana/use_timefilter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useTimefilter } from './use_timefilter'; | ||
|
||
jest.mock('./kibana_context', () => ({ | ||
useMlKibana: () => { | ||
return { | ||
services: { | ||
data: { | ||
query: { | ||
timefilter: { | ||
timefilter: { | ||
disableTimeRangeSelector: jest.fn(), | ||
disableAutoRefreshSelector: jest.fn(), | ||
enableTimeRangeSelector: jest.fn(), | ||
enableAutoRefreshSelector: jest.fn(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
})); | ||
|
||
describe('useTimefilter', () => { | ||
test('will not trigger any date picker settings by default', () => { | ||
const { result } = renderHook(() => useTimefilter()); | ||
const timefilter = result.current; | ||
|
||
expect(timefilter.disableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.disableAutoRefreshSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('custom disabled overrides', () => { | ||
const { result } = renderHook(() => | ||
useTimefilter({ timeRangeSelector: false, autoRefreshSelector: false }) | ||
); | ||
const timefilter = result.current; | ||
|
||
expect(timefilter.disableTimeRangeSelector).toHaveBeenCalledTimes(1); | ||
expect(timefilter.disableAutoRefreshSelector).toHaveBeenCalledTimes(1); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('custom enabled overrides', () => { | ||
const { result } = renderHook(() => | ||
useTimefilter({ timeRangeSelector: true, autoRefreshSelector: true }) | ||
); | ||
const timefilter = result.current; | ||
|
||
expect(timefilter.disableTimeRangeSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.disableAutoRefreshSelector).toHaveBeenCalledTimes(0); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(1); | ||
expect(timefilter.enableTimeRangeSelector).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
x-pack/legacy/plugins/ml/public/application/contexts/kibana/use_timefilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useEffect } from 'react'; | ||
|
||
import { useMlKibana } from './kibana_context'; | ||
|
||
interface UseTimefilterOptions { | ||
timeRangeSelector?: boolean; | ||
autoRefreshSelector?: boolean; | ||
} | ||
|
||
export const useTimefilter = ({ | ||
timeRangeSelector, | ||
autoRefreshSelector, | ||
}: UseTimefilterOptions = {}) => { | ||
const { services } = useMlKibana(); | ||
const { timefilter } = services.data.query.timefilter; | ||
|
||
useEffect(() => { | ||
if (timeRangeSelector === true) { | ||
timefilter.enableTimeRangeSelector(); | ||
} else if (timeRangeSelector === false) { | ||
timefilter.disableTimeRangeSelector(); | ||
} | ||
|
||
if (autoRefreshSelector === true) { | ||
timefilter.enableAutoRefreshSelector(); | ||
} else if (autoRefreshSelector === false) { | ||
timefilter.disableAutoRefreshSelector(); | ||
} | ||
}, [timeRangeSelector, autoRefreshSelector]); | ||
|
||
return timefilter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.