Skip to content

Commit a3c837a

Browse files
committed
Fix useRequest to support query change
1 parent 0340fac commit a3c837a

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

src/plugins/es_ui_shared/public/request/np_ready_request.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { useEffect, useState, useRef } from 'react';
20+
import { useEffect, useState, useRef, useMemo } from 'react';
2121

2222
import { HttpSetup, HttpFetchQuery } from '../../../../../src/core/public';
2323

@@ -78,6 +78,7 @@ export const useRequest = <D = any>(
7878
deserializer = (data: any): any => data,
7979
}: UseRequestConfig
8080
): UseRequestResponse<D> => {
81+
const sendRequestRef = useRef<() => Promise<SendRequestResponse<D>>>();
8182
// Main states for tracking request status and data
8283
const [error, setError] = useState<null | any>(null);
8384
const [isLoading, setIsLoading] = useState<boolean>(true);
@@ -102,7 +103,10 @@ export const useRequest = <D = any>(
102103

103104
// Set new interval
104105
if (pollInterval.current) {
105-
pollIntervalId.current = setTimeout(_sendRequest, pollInterval.current);
106+
pollIntervalId.current = setTimeout(
107+
() => (sendRequestRef.current ?? _sendRequest)(),
108+
pollInterval.current
109+
);
106110
}
107111
};
108112

@@ -145,11 +149,17 @@ export const useRequest = <D = any>(
145149
};
146150

147151
useEffect(() => {
148-
_sendRequest();
149-
// To be functionally correct we'd send a new request if the method, path, or body changes.
152+
sendRequestRef.current = _sendRequest;
153+
}, [_sendRequest]);
154+
155+
const stringifiedQuery = useMemo(() => JSON.stringify(query), [query]);
156+
157+
useEffect(() => {
158+
(sendRequestRef.current ?? _sendRequest)();
159+
// To be functionally correct we'd send a new request if the method, path, query or body changes.
150160
// But it doesn't seem likely that the method will change and body is likely to be a new
151-
// object even if its shape hasn't changed, so for now we're just watching the path.
152-
}, [path]);
161+
// object even if its shape hasn't changed, so for now we're just watching the path and the query.
162+
}, [path, stringifiedQuery]);
153163

154164
useEffect(() => {
155165
scheduleRequest();
@@ -168,6 +178,6 @@ export const useRequest = <D = any>(
168178
isLoading,
169179
error,
170180
data,
171-
sendRequest: _sendRequest, // Gives the user the ability to manually request data
181+
sendRequest: sendRequestRef.current ?? _sendRequest, // Gives the user the ability to manually request data
172182
};
173183
};

src/plugins/es_ui_shared/public/request/request.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe.skip('request lib', () => {
7171
it('uses the provided path, method, and body to send the request', async () => {
7272
const response = await sendRequest({ ...successRequest });
7373
sinon.assert.calledOnce(sendPost);
74-
expect(response).toEqual({ data: successResponse.data });
74+
expect(response).toEqual({ data: successResponse.data, error: null });
7575
});
7676

7777
it('surfaces errors', async () => {
@@ -182,11 +182,11 @@ describe.skip('request lib', () => {
182182
expect(hook.error).toBe(errorResponse);
183183
});
184184

185-
it('is undefined when the request is successful', async () => {
185+
it('is null when the request is successful', async () => {
186186
initUseRequest({ ...successRequest });
187187
await wait(50);
188188
expect(hook.isLoading).toBe(false);
189-
expect(hook.error).toBeUndefined();
189+
expect(hook.error).toBeNull();
190190
});
191191
});
192192

@@ -205,11 +205,11 @@ describe.skip('request lib', () => {
205205
expect(hook.data).toBe(successResponse.data);
206206
});
207207

208-
it('is undefined when the request fails', async () => {
208+
it('is null when the request fails', async () => {
209209
initUseRequest({ ...errorRequest });
210210
await wait(50);
211211
expect(hook.isLoading).toBe(false);
212-
expect(hook.data).toBeUndefined();
212+
expect(hook.data).toBeNull();
213213
});
214214
});
215215
});

0 commit comments

Comments
 (0)