Skip to content

Commit eabbb41

Browse files
committed
PR feedback
1 parent 81f3f22 commit eabbb41

2 files changed

Lines changed: 39 additions & 38 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const ServiceNowSIRParamsFields: React.FunctionComponent<
7777
[editSubActionProperty]
7878
);
7979

80-
const onChoicesSuccess = (values: Choice[]) => {
80+
const onChoicesSuccess = useCallback((values: Choice[]) => {
8181
setChoices(
8282
values.reduce(
8383
(acc, value) => ({
@@ -87,7 +87,7 @@ const ServiceNowSIRParamsFields: React.FunctionComponent<
8787
defaultFields
8888
)
8989
);
90-
};
90+
}, []);
9191

9292
const { isLoading: isLoadingChoices } = useGetChoices({
9393
http,

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

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { useState, useEffect, useRef } from 'react';
7+
import { useState, useEffect, useRef, useCallback } from 'react';
88
import { HttpSetup, ToastsApi } from 'kibana/public';
99
import { ActionConnector } from '../../../../types';
1010
import { getChoices } from './api';
@@ -36,60 +36,61 @@ export const useGetChoices = ({
3636
}: UseGetChoicesProps): UseGetChoices => {
3737
const [isLoading, setIsLoading] = useState(false);
3838
const [choices, setChoices] = useState<Choice[]>([]);
39+
const didCancel = useRef(false);
3940
const abortCtrl = useRef(new AbortController());
4041

41-
useEffect(() => {
42-
let didCancel = false;
43-
const fetchData = async () => {
44-
if (!actionConnector) {
45-
setIsLoading(false);
46-
return;
47-
}
42+
const fetchData = useCallback(async () => {
43+
if (!actionConnector) {
44+
setIsLoading(false);
45+
return;
46+
}
4847

48+
try {
49+
didCancel.current = false;
50+
abortCtrl.current.abort();
4951
abortCtrl.current = new AbortController();
5052
setIsLoading(true);
5153

52-
try {
53-
const res = await getChoices({
54-
http,
55-
signal: abortCtrl.current.signal,
56-
connectorId: actionConnector.id,
57-
fields,
58-
});
54+
const res = await getChoices({
55+
http,
56+
signal: abortCtrl.current.signal,
57+
connectorId: actionConnector.id,
58+
fields,
59+
});
5960

60-
if (!didCancel) {
61-
setIsLoading(false);
62-
setChoices(res.data ?? []);
63-
if (res.status && res.status === 'error') {
64-
toastNotifications.addDanger({
65-
title: i18n.CHOICES_API_ERROR,
66-
text: `${res.serviceMessage ?? res.message}`,
67-
});
68-
} else if (onSuccess) {
69-
onSuccess(res.data ?? []);
70-
}
71-
}
72-
} catch (error) {
73-
if (!didCancel) {
74-
setIsLoading(false);
61+
if (!didCancel.current) {
62+
setIsLoading(false);
63+
setChoices(res.data ?? []);
64+
if (res.status && res.status === 'error') {
7565
toastNotifications.addDanger({
7666
title: i18n.CHOICES_API_ERROR,
77-
text: error.message,
67+
text: `${res.serviceMessage ?? res.message}`,
7868
});
69+
} else if (onSuccess) {
70+
onSuccess(res.data ?? []);
7971
}
8072
}
81-
};
73+
} catch (error) {
74+
if (!didCancel.current) {
75+
setIsLoading(false);
76+
toastNotifications.addDanger({
77+
title: i18n.CHOICES_API_ERROR,
78+
text: error.message,
79+
});
80+
}
81+
}
82+
}, [actionConnector, http, fields, onSuccess, toastNotifications]);
8283

83-
abortCtrl.current.abort();
84+
useEffect(() => {
8485
fetchData();
8586

8687
return () => {
87-
didCancel = true;
88-
setIsLoading(false);
88+
didCancel.current = true;
8989
abortCtrl.current.abort();
90+
setIsLoading(false);
9091
};
9192
// eslint-disable-next-line react-hooks/exhaustive-deps
92-
}, [http, actionConnector, toastNotifications, fields]);
93+
}, []);
9394

9495
return {
9596
choices,

0 commit comments

Comments
 (0)