forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enabled column in monitor management monitors list table.
- Loading branch information
Showing
6 changed files
with
203 additions
and
5 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
79 changes: 79 additions & 0 deletions
79
...plugins/uptime/public/components/monitor_management/monitor_list/monitor_enabled.test.tsx
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,79 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { ConfigKey, DataStream, SyntheticsMonitor } from '../../../../common/runtime_types'; | ||
import { render } from '../../../lib/helper/rtl_helpers'; | ||
import { FETCH_STATUS } from '../../../../../observability/public'; | ||
import { spyOnUseFetcher } from '../../../lib/helper/spy_use_fetcher'; | ||
import { MonitorEnabled } from './monitor_enabled'; | ||
|
||
describe('<MonitorEnabled />', () => { | ||
const setRefresh = jest.fn(); | ||
const testMonitor = { | ||
[ConfigKey.MONITOR_TYPE]: DataStream.HTTP, | ||
[ConfigKey.ENABLED]: true, | ||
} as unknown as SyntheticsMonitor; | ||
|
||
const assertMonitorEnabled = (button: HTMLButtonElement) => | ||
expect(button).toHaveAttribute('aria-checked', 'true'); | ||
const assertMonitorDisabled = (button: HTMLButtonElement) => | ||
expect(button).toHaveAttribute('aria-checked', 'false'); | ||
|
||
let useFetcher: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
useFetcher?.mockClear(); | ||
useFetcher = spyOnUseFetcher({}); | ||
}); | ||
|
||
it('correctly renders "enabled" state', () => { | ||
render(<MonitorEnabled id="test-id" monitor={testMonitor} setRefresh={setRefresh} />); | ||
|
||
const switchButton = screen.getByRole('switch') as HTMLButtonElement; | ||
assertMonitorEnabled(switchButton); | ||
}); | ||
|
||
it('correctly renders "disabled" state', () => { | ||
render( | ||
<MonitorEnabled | ||
id="test-id" | ||
monitor={{ ...testMonitor, [ConfigKey.ENABLED]: false }} | ||
setRefresh={setRefresh} | ||
/> | ||
); | ||
|
||
const switchButton = screen.getByRole('switch') as HTMLButtonElement; | ||
assertMonitorDisabled(switchButton); | ||
}); | ||
|
||
it('toggles on click', () => { | ||
render(<MonitorEnabled id="test-id" monitor={testMonitor} setRefresh={setRefresh} />); | ||
|
||
const switchButton = screen.getByRole('switch') as HTMLButtonElement; | ||
userEvent.click(switchButton); | ||
assertMonitorDisabled(switchButton); | ||
userEvent.click(switchButton); | ||
assertMonitorEnabled(switchButton); | ||
}); | ||
|
||
it('is disabled while request is in progress', () => { | ||
useFetcher.mockReturnValue({ | ||
data: {}, | ||
status: FETCH_STATUS.LOADING, | ||
refetch: () => {}, | ||
}); | ||
|
||
render(<MonitorEnabled id="test-id" monitor={testMonitor} setRefresh={setRefresh} />); | ||
const switchButton = screen.getByRole('switch') as HTMLButtonElement; | ||
userEvent.click(switchButton); | ||
|
||
expect(switchButton).toHaveAttribute('disabled'); | ||
}); | ||
}); |
106 changes: 106 additions & 0 deletions
106
x-pack/plugins/uptime/public/components/monitor_management/monitor_list/monitor_enabled.tsx
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,106 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { EuiSwitch } from '@elastic/eui'; | ||
import { EuiSwitchEvent } from '@elastic/eui/src/components/form/switch/switch'; | ||
import { i18n } from '@kbn/i18n'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; | ||
import { FETCH_STATUS, useFetcher } from '../../../../../observability/public'; | ||
import { ConfigKey, SyntheticsMonitor } from '../../../../common/runtime_types'; | ||
import { setMonitor } from '../../../state/api'; | ||
|
||
interface Props { | ||
id: string; | ||
monitor: SyntheticsMonitor; | ||
setRefresh: React.Dispatch<React.SetStateAction<boolean>>; | ||
} | ||
|
||
export const MonitorEnabled = ({ id, monitor, setRefresh }: Props) => { | ||
const [isEnabled, setIsEnabled] = useState<boolean | null>(null); | ||
|
||
const { notifications } = useKibana(); | ||
|
||
const { status } = useFetcher(() => { | ||
if (isEnabled !== null) { | ||
return setMonitor({ id, monitor: { ...monitor, [ConfigKey.ENABLED]: isEnabled } }); | ||
} | ||
}, [isEnabled]); | ||
|
||
useEffect(() => { | ||
if (status === FETCH_STATUS.FAILURE) { | ||
notifications.toasts.danger({ | ||
title: ( | ||
<p data-test-subj="uptimeMonitorEnabledUpdateFailure"> | ||
{getMonitorEnabledUpdateFailureMessage(monitor[ConfigKey.NAME])} | ||
</p> | ||
), | ||
toastLifeTimeMs: 3000, | ||
}); | ||
setIsEnabled(null); | ||
} else if (status === FETCH_STATUS.SUCCESS) { | ||
notifications.toasts.success({ | ||
title: ( | ||
<p data-test-subj="uptimeMonitorEnabledUpdateSuccess"> | ||
{isEnabled | ||
? getMonitorEnabledSuccessLabel(monitor[ConfigKey.NAME]) | ||
: getMonitorDisabledSuccessLabel(monitor[ConfigKey.NAME])} | ||
</p> | ||
), | ||
toastLifeTimeMs: 3000, | ||
}); | ||
setRefresh(true); | ||
} | ||
}, [status]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const enabled = isEnabled ?? monitor[ConfigKey.ENABLED]; | ||
const isLoading = status === FETCH_STATUS.LOADING; | ||
|
||
const handleEnabledChange = (event: EuiSwitchEvent) => { | ||
const checked = event.target.checked; | ||
setIsEnabled(checked); | ||
}; | ||
|
||
return ( | ||
<EuiSwitch | ||
checked={enabled} | ||
disabled={isLoading} | ||
showLabel={false} | ||
label={''} | ||
data-test-subj="syntheticsIsMonitorEnabled" | ||
aria-label={enabled ? DISABLE_MONITOR_LABEL : ENABLE_MONITOR_LABEL} | ||
title={enabled ? DISABLE_MONITOR_LABEL : ENABLE_MONITOR_LABEL} | ||
onChange={handleEnabledChange} | ||
/> | ||
); | ||
}; | ||
|
||
const ENABLE_MONITOR_LABEL = i18n.translate('xpack.uptime.monitorManagement.enableMonitorLabel', { | ||
defaultMessage: 'Enable monitor', | ||
}); | ||
|
||
const DISABLE_MONITOR_LABEL = i18n.translate('xpack.uptime.monitorManagement.disableMonitorLabel', { | ||
defaultMessage: 'Disable monitor', | ||
}); | ||
|
||
const getMonitorEnabledSuccessLabel = (name: string) => | ||
i18n.translate('xpack.uptime.monitorManagement.monitorEnabledSuccessMessage', { | ||
defaultMessage: 'Monitor {name} enabled successfully.', | ||
values: { name }, | ||
}); | ||
|
||
const getMonitorDisabledSuccessLabel = (name: string) => | ||
i18n.translate('xpack.uptime.monitorManagement.monitorDisabledSuccessMessage', { | ||
defaultMessage: 'Monitor {name} disabled successfully.', | ||
values: { name }, | ||
}); | ||
|
||
const getMonitorEnabledUpdateFailureMessage = (name: string) => | ||
i18n.translate('xpack.uptime.monitorManagement.monitorEnabledUpdateFailureMessage', { | ||
defaultMessage: 'Unable to update monitor {name}.', | ||
values: { name }, | ||
}); |
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