Skip to content

Commit

Permalink
[Alerts] Remove Add Alerts flyout onClose (elastic#85462)
Browse files Browse the repository at this point in the history
* Remove add alerts flyout after onClose

* Updating tests

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Gidi Meir Morris <[email protected]>
  • Loading branch information
3 people committed Dec 15, 2020
1 parent 4b57ac5 commit dd358b8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useMemo, useState } from 'react';
import React, { useMemo, useState, useCallback } from 'react';

import { EuiIcon, EuiFlexItem, EuiCard, EuiFlexGroup } from '@elastic/eui';

Expand All @@ -14,15 +14,18 @@ import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';
export const CreateAlert = ({ triggersActionsUi }: AlertingExampleComponentParams) => {
const [alertFlyoutVisible, setAlertFlyoutVisibility] = useState<boolean>(false);

const onCloseAlertFlyout = useCallback(() => setAlertFlyoutVisibility(false), [
setAlertFlyoutVisibility,
]);

const AddAlertFlyout = useMemo(
() =>
triggersActionsUi.getAddAlertFlyout({
consumer: ALERTING_EXAMPLE_APP_ID,
addFlyoutVisible: alertFlyoutVisible,
setAddFlyoutVisibility: setAlertFlyoutVisibility,
onClose: onCloseAlertFlyout,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[alertFlyoutVisible]
[onCloseAlertFlyout]
);

return (
Expand All @@ -35,7 +38,7 @@ export const CreateAlert = ({ triggersActionsUi }: AlertingExampleComponentParam
onClick={() => setAlertFlyoutVisibility(true)}
/>
</EuiFlexItem>
<EuiFlexItem>{AddAlertFlyout}</EuiFlexItem>
<EuiFlexItem>{alertFlyoutVisible && AddAlertFlyout}</EuiFlexItem>
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
import { AlertType } from '../../../../common/alert_types';
import { TriggersAndActionsUIPublicPluginStart } from '../../../../../triggers_actions_ui/public';
Expand All @@ -23,17 +23,21 @@ export function AlertingFlyout(props: Props) {
const {
services: { triggersActionsUi },
} = useKibana<KibanaDeps>();

const onCloseAddFlyout = useCallback(() => setAddFlyoutVisibility(false), [
setAddFlyoutVisibility,
]);

const addAlertFlyout = useMemo(
() =>
alertType &&
triggersActionsUi.getAddAlertFlyout({
consumer: 'apm',
addFlyoutVisible,
setAddFlyoutVisibility,
onClose: onCloseAddFlyout,
alertTypeId: alertType,
canChangeTrigger: false,
}),
[addFlyoutVisible, alertType, setAddFlyoutVisibility, triggersActionsUi]
[alertType, onCloseAddFlyout, triggersActionsUi]
);
return <>{addAlertFlyout}</>;
return <>{addFlyoutVisible && addAlertFlyout}</>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useContext, useMemo } from 'react';
import React, { useCallback, useContext, useMemo } from 'react';

import { TriggerActionsContext } from '../../../utils/triggers_actions_context';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
Expand All @@ -26,14 +26,13 @@ export const AlertFlyout = ({ options, nodeType, filter, visible, setVisible }:

const { inventoryPrefill } = useAlertPrefillContext();
const { customMetrics } = inventoryPrefill;

const onCloseFlyout = useCallback(() => setVisible(false), [setVisible]);
const AddAlertFlyout = useMemo(
() =>
triggersActionsUI &&
triggersActionsUI.getAddAlertFlyout({
consumer: 'infrastructure',
addFlyoutVisible: visible!,
setAddFlyoutVisibility: setVisible,
onClose: onCloseFlyout,
canChangeTrigger: false,
alertTypeId: METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID,
metadata: {
Expand All @@ -47,5 +46,5 @@ export const AlertFlyout = ({ options, nodeType, filter, visible, setVisible }:
[triggersActionsUI, visible]
);

return <>{AddAlertFlyout}</>;
return <>{visible && AddAlertFlyout}</>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useContext, useMemo } from 'react';
import React, { useCallback, useContext, useMemo } from 'react';
import { TriggerActionsContext } from '../../../utils/triggers_actions_context';
import { LOG_DOCUMENT_COUNT_ALERT_TYPE_ID } from '../../../../common/alerting/logs/log_threshold/types';

Expand All @@ -14,24 +14,23 @@ interface Props {
}

export const AlertFlyout = (props: Props) => {
const { visible, setVisible } = props;
const { triggersActionsUI } = useContext(TriggerActionsContext);

const onCloseFlyout = useCallback(() => setVisible(false), [setVisible]);
const AddAlertFlyout = useMemo(
() =>
triggersActionsUI &&
triggersActionsUI.getAddAlertFlyout({
consumer: 'logs',
addFlyoutVisible: props.visible!,
setAddFlyoutVisibility: props.setVisible,
onClose: onCloseFlyout,
canChangeTrigger: false,
alertTypeId: LOG_DOCUMENT_COUNT_ALERT_TYPE_ID,
metadata: {
isInternal: true,
},
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[triggersActionsUI, props.visible]
[triggersActionsUI, onCloseFlyout]
);

return <>{AddAlertFlyout}</>;
return <>{visible && AddAlertFlyout}</>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useContext, useMemo } from 'react';
import React, { useCallback, useContext, useMemo } from 'react';
import { TriggerActionsContext } from '../../../utils/triggers_actions_context';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { METRIC_THRESHOLD_ALERT_TYPE_ID } from '../../../../server/lib/alerting/metric_threshold/types';
Expand All @@ -19,15 +19,15 @@ interface Props {
}

export const AlertFlyout = (props: Props) => {
const { visible, setVisible } = props;
const { triggersActionsUI } = useContext(TriggerActionsContext);

const onCloseFlyout = useCallback(() => setVisible(false), [setVisible]);
const AddAlertFlyout = useMemo(
() =>
triggersActionsUI &&
triggersActionsUI.getAddAlertFlyout({
consumer: 'infrastructure',
addFlyoutVisible: props.visible!,
setAddFlyoutVisibility: props.setVisible,
onClose: onCloseFlyout,
canChangeTrigger: false,
alertTypeId: METRIC_THRESHOLD_ALERT_TYPE_ID,
metadata: {
Expand All @@ -36,8 +36,8 @@ export const AlertFlyout = (props: Props) => {
},
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[triggersActionsUI, props.visible]
[triggersActionsUI, onCloseFlyout]
);

return <>{AddAlertFlyout}</>;
return <>{visible && AddAlertFlyout}</>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ describe('alert_add', () => {
wrapper = mountWithIntl(
<AlertAdd
consumer={ALERTS_FEATURE_ID}
addFlyoutVisible={true}
setAddFlyoutVisibility={() => {}}
onClose={() => {}}
initialValues={initialValues}
reloadAlerts={() => {
return new Promise<void>(() => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import { useKibana } from '../../../common/lib/kibana';

export interface AlertAddProps<MetaData = Record<string, any>> {
consumer: string;
addFlyoutVisible: boolean;
alertTypeRegistry: AlertTypeRegistryContract;
actionTypeRegistry: ActionTypeRegistryContract;
setAddFlyoutVisibility: React.Dispatch<React.SetStateAction<boolean>>;
onClose: () => void;
alertTypeId?: string;
canChangeTrigger?: boolean;
initialValues?: Partial<Alert>;
Expand All @@ -39,10 +38,9 @@ export interface AlertAddProps<MetaData = Record<string, any>> {

const AlertAdd = ({
consumer,
addFlyoutVisible,
alertTypeRegistry,
actionTypeRegistry,
setAddFlyoutVisibility,
onClose,
canChangeTrigger,
alertTypeId,
initialValues,
Expand Down Expand Up @@ -92,9 +90,9 @@ const AlertAdd = ({
}, [alertTypeId]);

const closeFlyout = useCallback(() => {
setAddFlyoutVisibility(false);
setAlert(initialAlert);
}, [initialAlert, setAddFlyoutVisibility]);
onClose();
}, [initialAlert, onClose]);

const saveAlertAndCloseFlyout = async () => {
const savedAlert = await onSaveAlert();
Expand All @@ -107,10 +105,6 @@ const AlertAdd = ({
}
};

if (!addFlyoutVisible) {
return null;
}

const alertType = alert.alertTypeId ? alertTypeRegistry.get(alert.alertTypeId) : null;
const errors = {
...(alertType ? alertType.validate(alert.params).errors : []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jest.mock('../../../lib/action_connector_api', () => ({
jest.mock('../../../lib/alert_api', () => ({
loadAlerts: jest.fn(),
loadAlertTypes: jest.fn(),
health: jest.fn(() => ({ isSufficientlySecure: true, hasPermanentEncryptionKey: true })),
}));
jest.mock('react-router-dom', () => ({
useHistory: () => ({
Expand Down Expand Up @@ -114,7 +115,16 @@ describe('alerts_list component empty', () => {
expect(
wrapper.find('[data-test-subj="createFirstAlertButton"]').find('EuiButton')
).toHaveLength(1);
expect(wrapper.find('AlertAdd')).toHaveLength(1);
expect(wrapper.find('AlertAdd').exists()).toBeFalsy();

wrapper.find('button[data-test-subj="createFirstAlertButton"]').simulate('click');

// When the AlertAdd component is rendered, it waits for the healthcheck to resolve
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
wrapper.update();
expect(wrapper.find('AlertAdd').exists()).toEqual(true);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,17 @@ export const AlertsList: React.FunctionComponent = () => {
) : (
noPermissionPrompt
)}
<AlertAdd
consumer={ALERTS_FEATURE_ID}
addFlyoutVisible={alertFlyoutVisible}
setAddFlyoutVisibility={setAlertFlyoutVisibility}
actionTypeRegistry={actionTypeRegistry}
alertTypeRegistry={alertTypeRegistry}
reloadAlerts={loadAlertsData}
/>
{alertFlyoutVisible && (
<AlertAdd
consumer={ALERTS_FEATURE_ID}
onClose={() => {
setAlertFlyoutVisibility(false);
}}
actionTypeRegistry={actionTypeRegistry}
alertTypeRegistry={alertTypeRegistry}
reloadAlerts={loadAlertsData}
/>
)}
</section>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
import { TriggersAndActionsUIPublicPluginStart } from '../../../../../../plugins/triggers_actions_ui/public';

Expand All @@ -24,19 +24,20 @@ export const UptimeAlertsFlyoutWrapperComponent = ({
setAlertFlyoutVisibility,
}: Props) => {
const { triggersActionsUi } = useKibana<KibanaDeps>().services;

const onCloseAlertFlyout = useCallback(() => setAlertFlyoutVisibility(false), [
setAlertFlyoutVisibility,
]);
const AddAlertFlyout = useMemo(
() =>
triggersActionsUi.getAddAlertFlyout({
consumer: 'uptime',
addFlyoutVisible: alertFlyoutVisible,
setAddFlyoutVisibility: setAlertFlyoutVisibility,
onClose: onCloseAlertFlyout,
alertTypeId,
canChangeTrigger: !alertTypeId,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[alertFlyoutVisible, alertTypeId]
[onCloseAlertFlyout, alertTypeId]
);

return <>{AddAlertFlyout}</>;
return <>{alertFlyoutVisible && AddAlertFlyout}</>;
};

0 comments on commit dd358b8

Please sign in to comment.