Skip to content

Commit

Permalink
Fixed console error, which appears when saving changes in Edit Alert …
Browse files Browse the repository at this point in the history
…flyout (#83610) (#83725)
  • Loading branch information
YulNaumenko authored Nov 19, 2020
1 parent 4fe9aa9 commit 985b405
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 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, { 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

0 comments on commit 985b405

Please sign in to comment.