Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Feb 1, 2021
1 parent 81f3f22 commit 114fa49
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ServiceNowSIRParamsFields: React.FunctionComponent<
[editSubActionProperty]
);

const onChoicesSuccess = (values: Choice[]) => {
const onChoicesSuccess = useCallback((values: Choice[]) => {
setChoices(
values.reduce(
(acc, value) => ({
Expand All @@ -87,7 +87,7 @@ const ServiceNowSIRParamsFields: React.FunctionComponent<
defaultFields
)
);
};
}, []);

const { isLoading: isLoadingChoices } = useGetChoices({
http,
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 { useState, useEffect, useRef } from 'react';
import { useState, useEffect, useRef, useCallback } from 'react';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { ActionConnector } from '../../../../types';
import { getChoices } from './api';
Expand Down Expand Up @@ -36,60 +36,63 @@ export const useGetChoices = ({
}: UseGetChoicesProps): UseGetChoices => {
const [isLoading, setIsLoading] = useState(false);
const [choices, setChoices] = useState<Choice[]>([]);
const didCancel = useRef(false);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let didCancel = false;
const fetchData = async () => {
if (!actionConnector) {
setIsLoading(false);
return;
}
const fetchData = useCallback(async () => {
if (!actionConnector) {
setIsLoading(false);
return;
}

abortCtrl.current = new AbortController();
setIsLoading(true);

try {
didCancel.current = false;
abortCtrl.current.abort();
abortCtrl.current = new AbortController();
setIsLoading(true);

try {
const res = await getChoices({
http,
signal: abortCtrl.current.signal,
connectorId: actionConnector.id,
fields,
});
const res = await getChoices({
http,
signal: abortCtrl.current.signal,
connectorId: actionConnector.id,
fields,
});

if (!didCancel) {
setIsLoading(false);
setChoices(res.data ?? []);
if (res.status && res.status === 'error') {
toastNotifications.addDanger({
title: i18n.CHOICES_API_ERROR,
text: `${res.serviceMessage ?? res.message}`,
});
} else if (onSuccess) {
onSuccess(res.data ?? []);
}
}
} catch (error) {
if (!didCancel) {
setIsLoading(false);
if (!didCancel) {
setIsLoading(false);
setChoices(res.data ?? []);
if (res.status && res.status === 'error') {
toastNotifications.addDanger({
title: i18n.CHOICES_API_ERROR,
text: error.message,
text: `${res.serviceMessage ?? res.message}`,
});
} else if (onSuccess) {
onSuccess(res.data ?? []);
}
}
};
} catch (error) {
if (!didCancel) {
setIsLoading(false);
toastNotifications.addDanger({
title: i18n.CHOICES_API_ERROR,
text: error.message,
});
}
}
}, [actionConnector, http, fields, onSuccess, toastNotifications]);

abortCtrl.current.abort();
useEffect(() => {
fetchData();

return () => {
didCancel = true;
setIsLoading(false);
didCancel.current = true;
abortCtrl.current.abort();
setIsLoading(false);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [http, actionConnector, toastNotifications, fields]);
}, []);

return {
choices,
Expand Down

0 comments on commit 114fa49

Please sign in to comment.