Skip to content

Commit 7e5be98

Browse files
authored
Fixed AddAlert flyout does not immediately update state to reflect new props (#64927)
* Fixed `AddAlert` flyout does not immediately update state to reflect new props * fixed add form * Fixed type check
1 parent 8128b91 commit 7e5be98

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/es_index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ const IndexActionConnectorFields: React.FunctionComponent<ActionConnectorFieldsP
8686

8787
const [indexPatterns, setIndexPatterns] = useState([]);
8888
const [indexOptions, setIndexOptions] = useState<EuiComboBoxOptionOption[]>([]);
89-
const [timeFieldOptions, setTimeFieldOptions] = useState([firstFieldOption]);
89+
const [timeFieldOptions, setTimeFieldOptions] = useState<Array<{ value: string; text: string }>>([
90+
firstFieldOption,
91+
]);
9092
const [isIndiciesLoading, setIsIndiciesLoading] = useState<boolean>(false);
9193

9294
useEffect(() => {

x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_add.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import React, { useCallback, useReducer, useState } from 'react';
6+
import React, { useCallback, useReducer, useState, useEffect } from 'react';
77
import { isObject } from 'lodash';
88
import { FormattedMessage } from '@kbn/i18n/react';
99
import {
@@ -60,6 +60,9 @@ export const AlertAdd = ({
6060
const setAlert = (value: any) => {
6161
dispatch({ command: { type: 'setAlert' }, payload: { key: 'alert', value } });
6262
};
63+
const setAlertProperty = (key: string, value: any) => {
64+
dispatch({ command: { type: 'setProperty' }, payload: { key, value } });
65+
};
6366

6467
const {
6568
reloadAlerts,
@@ -70,6 +73,10 @@ export const AlertAdd = ({
7073
docLinks,
7174
} = useAlertsContext();
7275

76+
useEffect(() => {
77+
setAlertProperty('alertTypeId', alertTypeId);
78+
}, [alertTypeId]);
79+
7380
const closeFlyout = useCallback(() => {
7481
setAddFlyoutVisibility(false);
7582
setAlert(initialAlert);

x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_edit.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export const AlertEdit = ({
4343
const [{ alert }, dispatch] = useReducer(alertReducer, { alert: initialAlert });
4444
const [isSaving, setIsSaving] = useState<boolean>(false);
4545
const [hasActionsDisabled, setHasActionsDisabled] = useState<boolean>(false);
46+
const setAlert = (key: string, value: any) => {
47+
dispatch({ command: { type: 'setAlert' }, payload: { key, value } });
48+
};
4649

4750
const {
4851
reloadAlerts,
@@ -55,6 +58,8 @@ export const AlertEdit = ({
5558

5659
const closeFlyout = useCallback(() => {
5760
setEditFlyoutVisibility(false);
61+
setAlert('alert', initialAlert);
62+
// eslint-disable-next-line react-hooks/exhaustive-deps
5863
}, [setEditFlyoutVisibility]);
5964

6065
if (!editFlyoutVisible) {

x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,53 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
197197
const headingText = await pageObjects.alertDetailsUI.getHeadingText();
198198
expect(headingText).to.be(updatedAlertName);
199199
});
200+
201+
it('should reset alert when canceling an edit', async () => {
202+
await pageObjects.common.navigateToApp('triggersActions');
203+
const params = {
204+
aggType: 'count',
205+
termSize: 5,
206+
thresholdComparator: '>',
207+
timeWindowSize: 5,
208+
timeWindowUnit: 'm',
209+
groupBy: 'all',
210+
threshold: [1000, 5000],
211+
index: ['.kibana_1'],
212+
timeField: 'alert',
213+
};
214+
const alert = await alerting.alerts.createAlertWithActions(
215+
testRunUuid,
216+
'.index-threshold',
217+
params
218+
);
219+
// refresh to see alert
220+
await browser.refresh();
221+
222+
await pageObjects.header.waitUntilLoadingHasFinished();
223+
224+
// Verify content
225+
await testSubjects.existOrFail('alertsList');
226+
227+
// click on first alert
228+
await pageObjects.triggersActionsUI.clickOnAlertInAlertsList(alert.name);
229+
230+
const editButton = await testSubjects.find('openEditAlertFlyoutButton');
231+
await editButton.click();
232+
233+
const updatedAlertName = `Changed Alert Name ${uuid.v4()}`;
234+
await testSubjects.setValue('alertNameInput', updatedAlertName, {
235+
clearWithKeyboard: true,
236+
});
237+
238+
await testSubjects.click('cancelSaveEditedAlertButton');
239+
await find.waitForDeletedByCssSelector('[data-test-subj="cancelSaveEditedAlertButton"]');
240+
241+
await editButton.click();
242+
243+
const nameInputAfterCancel = await testSubjects.find('alertNameInput');
244+
const textAfterCancel = await nameInputAfterCancel.getAttribute('value');
245+
expect(textAfterCancel).to.eql(alert.name);
246+
});
200247
});
201248

202249
describe('View In App', function() {

0 commit comments

Comments
 (0)