Skip to content

Commit

Permalink
Fix bug where user can't add an exception when "close alert" is check…
Browse files Browse the repository at this point in the history
…ed (#72919)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
peluja1012 and elasticmachine authored Jul 23, 2020
1 parent bacf9f2 commit ac8cdf3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('useAddOrUpdateException', () => {
await act(async () => {
const { result, waitForNextUpdate } = render();
await waitForNextUpdate();
expect(result.current).toEqual([{ isLoading: false }, null]);
expect(result.current).toEqual([{ isLoading: false }, result.current[1]]);
});
});

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 { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, useCallback } from 'react';
import { HttpStart } from '../../../../../../../src/core/public';

import {
Expand Down Expand Up @@ -60,7 +60,19 @@ export const useAddOrUpdateException = ({
onSuccess,
}: UseAddOrUpdateExceptionProps): ReturnUseAddOrUpdateException => {
const [isLoading, setIsLoading] = useState(false);
const addOrUpdateException = useRef<AddOrUpdateExceptionItemsFunc | null>(null);
const addOrUpdateExceptionRef = useRef<AddOrUpdateExceptionItemsFunc | null>(null);
const addOrUpdateException = useCallback<AddOrUpdateExceptionItemsFunc>(
async (exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex) => {
if (addOrUpdateExceptionRef.current !== null) {
addOrUpdateExceptionRef.current(
exceptionItemsToAddOrUpdate,
alertIdToClose,
bulkCloseIndex
);
}
},
[]
);

useEffect(() => {
let isSubscribed = true;
Expand Down Expand Up @@ -114,6 +126,7 @@ export const useAddOrUpdateException = ({
await updateAlertStatus({
query: getUpdateAlertsQuery([alertIdToClose]),
status: 'closed',
signal: abortCtrl.signal,
});
}

Expand All @@ -131,6 +144,7 @@ export const useAddOrUpdateException = ({
query: filter,
},
status: 'closed',
signal: abortCtrl.signal,
});
}

Expand All @@ -148,12 +162,12 @@ export const useAddOrUpdateException = ({
}
};

addOrUpdateException.current = addOrUpdateExceptionItems;
addOrUpdateExceptionRef.current = addOrUpdateExceptionItems;
return (): void => {
isSubscribed = false;
abortCtrl.abort();
};
}, [http, onSuccess, onError]);

return [{ isLoading }, addOrUpdateException.current];
return [{ isLoading }, addOrUpdateException];
};

0 comments on commit ac8cdf3

Please sign in to comment.