Skip to content
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

Fixed console error, which appears when saving changes in Edit Alert flyout #83610

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, { useState, Fragment, useEffect } from 'react';
import React, { useState, Fragment, useEffect, useReducer } from 'react';
import { keyBy } from 'lodash';
import { useHistory } from 'react-router-dom';
import {
Expand Down Expand Up @@ -41,6 +41,7 @@ import { AlertEdit } from '../../alert_form';
import { AlertsContextProvider } from '../../../context/alerts_context';
import { routeToAlertDetails } from '../../../constants';
import { alertsErrorReasonTranslationsMapping } from '../../alerts_list/translations';
import { alertReducer } from '../../alert_form/alert_reducer';

type AlertDetailsProps = {
alert: Alert;
Expand Down Expand Up @@ -73,6 +74,10 @@ export const AlertDetails: React.FunctionComponent<AlertDetailsProps> = ({
setBreadcrumbs,
chrome,
} = useAppDependencies();
const [{}, dispatch] = useReducer(alertReducer, { alert });
const setInitialAlert = (key: string, value: any) => {
dispatch({ command: { type: 'setAlert' }, payload: { key, value } });
};

// Set breadcrumb and page title
useEffect(() => {
Expand Down Expand Up @@ -166,7 +171,10 @@ export const AlertDetails: React.FunctionComponent<AlertDetailsProps> = ({
>
<AlertEdit
initialAlert={alert}
onClose={() => setEditFlyoutVisibility(false)}
onClose={() => {
setInitialAlert('alert', alert);
setEditFlyoutVisibility(false);
}}
/>
</AlertsContextProvider>
)}
Expand Down
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, { Fragment, useCallback, useReducer, useState } from 'react';
import React, { Fragment, useReducer, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import {
EuiTitle,
Expand Down Expand Up @@ -40,9 +40,6 @@ export const AlertEdit = ({ initialAlert, onClose }: AlertEditProps) => {
const [hasActionsWithBrokenConnector, setHasActionsWithBrokenConnector] = useState<boolean>(
false
);
const setAlert = (key: string, value: any) => {
dispatch({ command: { type: 'setAlert' }, payload: { key, value } });
};

const {
reloadAlerts,
Expand All @@ -53,12 +50,6 @@ export const AlertEdit = ({ initialAlert, onClose }: AlertEditProps) => {
docLinks,
} = useAlertsContext();

const closeFlyout = useCallback(() => {
onClose();
setAlert('alert', initialAlert);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [onClose]);

const alertType = alertTypeRegistry.get(alert.alertTypeId);

const errors = {
Expand Down Expand Up @@ -105,7 +96,7 @@ export const AlertEdit = ({ initialAlert, onClose }: AlertEditProps) => {
return (
<EuiPortal>
<EuiFlyout
onClose={closeFlyout}
onClose={() => onClose()}
aria-labelledby="flyoutAlertEditTitle"
size="m"
maxWidth={620}
Expand Down Expand Up @@ -155,7 +146,7 @@ export const AlertEdit = ({ initialAlert, onClose }: AlertEditProps) => {
<EuiFlexItem grow={false}>
<EuiButtonEmpty
data-test-subj="cancelSaveEditedAlertButton"
onClick={closeFlyout}
onClick={() => onClose()}
>
{i18n.translate(
'xpack.triggersActionsUI.sections.alertEdit.cancelButtonLabel',
Expand All @@ -179,7 +170,7 @@ export const AlertEdit = ({ initialAlert, onClose }: AlertEditProps) => {
const savedAlert = await onSaveAlert();
setIsSaving(false);
if (savedAlert) {
closeFlyout();
onClose();
if (reloadAlerts) {
reloadAlerts();
}
Expand Down