|
4 | 4 | * you may not use this file except in compliance with the Elastic License. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { useState, useEffect, useRef } from 'react'; |
| 7 | +import { useState, useEffect, useRef, useCallback } from 'react'; |
8 | 8 | import { HttpSetup, ToastsApi } from 'kibana/public'; |
9 | 9 | import { ActionConnector } from '../../../../types'; |
10 | 10 | import { getChoices } from './api'; |
@@ -36,60 +36,61 @@ export const useGetChoices = ({ |
36 | 36 | }: UseGetChoicesProps): UseGetChoices => { |
37 | 37 | const [isLoading, setIsLoading] = useState(false); |
38 | 38 | const [choices, setChoices] = useState<Choice[]>([]); |
| 39 | + const didCancel = useRef(false); |
39 | 40 | const abortCtrl = useRef(new AbortController()); |
40 | 41 |
|
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 | + } |
48 | 47 |
|
| 48 | + try { |
| 49 | + didCancel.current = false; |
| 50 | + abortCtrl.current.abort(); |
49 | 51 | abortCtrl.current = new AbortController(); |
50 | 52 | setIsLoading(true); |
51 | 53 |
|
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 | + }); |
59 | 60 |
|
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') { |
75 | 65 | toastNotifications.addDanger({ |
76 | 66 | title: i18n.CHOICES_API_ERROR, |
77 | | - text: error.message, |
| 67 | + text: `${res.serviceMessage ?? res.message}`, |
78 | 68 | }); |
| 69 | + } else if (onSuccess) { |
| 70 | + onSuccess(res.data ?? []); |
79 | 71 | } |
80 | 72 | } |
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]); |
82 | 83 |
|
83 | | - abortCtrl.current.abort(); |
| 84 | + useEffect(() => { |
84 | 85 | fetchData(); |
85 | 86 |
|
86 | 87 | return () => { |
87 | | - didCancel = true; |
88 | | - setIsLoading(false); |
| 88 | + didCancel.current = true; |
89 | 89 | abortCtrl.current.abort(); |
| 90 | + setIsLoading(false); |
90 | 91 | }; |
91 | 92 | // eslint-disable-next-line react-hooks/exhaustive-deps |
92 | | - }, [http, actionConnector, toastNotifications, fields]); |
| 93 | + }, []); |
93 | 94 |
|
94 | 95 | return { |
95 | 96 | choices, |
|
0 commit comments