-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Uptime][Monitor Management UI] Add Enabled column in monitor management monitors list table. #121682
[Uptime][Monitor Management UI] Add Enabled column in monitor management monitors list table. #121682
Changes from all commits
e096cc0
4fce3da
d6948b3
60d5311
63569cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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, EuiProgress, EuiSwitchEvent } from '@elastic/eui'; | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabling exhaustive deps always gives me pause. Can you explain why you are disabling exhaustive deps? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because here I want the side effect to execute only when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. Could you add an inline comment for context? |
||
|
||
const enabled = isEnabled ?? monitor[ConfigKey.ENABLED]; | ||
const isLoading = status === FETCH_STATUS.LOADING; | ||
|
||
const handleEnabledChange = (event: EuiSwitchEvent) => { | ||
const checked = event.target.checked; | ||
setIsEnabled(checked); | ||
}; | ||
|
||
return ( | ||
<div css={{ position: 'relative' }} aria-busy={isLoading}> | ||
<EuiSwitch | ||
checked={enabled} | ||
disabled={isLoading} | ||
showLabel={false} | ||
label={enabled ? DISABLE_MONITOR_LABEL : ENABLE_MONITOR_LABEL} | ||
title={enabled ? DISABLE_MONITOR_LABEL : ENABLE_MONITOR_LABEL} | ||
data-test-subj="syntheticsIsMonitorEnabled" | ||
onChange={handleEnabledChange} | ||
/> | ||
{isLoading ? ( | ||
<EuiProgress | ||
css={{ position: 'absolute', left: 0, bottom: -4, width: '100%', height: 2 }} | ||
size="xs" | ||
color="primary" | ||
/> | ||
) : null} | ||
</div> | ||
); | ||
}; | ||
|
||
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 }, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we set the default for this to
monitor[ConfigKey.ENABLED]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isEnabled
tracks the recent ui state. When it is null, default would be used. So I am doingconst enabled = isEnabled ?? monitor[ConfigKey.ENABLED];
below.