From 41d4751e07a5370f8d9631f85e1f986eeabcaee4 Mon Sep 17 00:00:00 2001 From: aramalipoor Date: Sat, 24 Sep 2022 13:09:26 +0200 Subject: [PATCH] feat: allow overriding patch request call --- .../react/src/common/hooks/useAxiosPatch.ts | 71 +++++++++++-------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/packages/react/src/common/hooks/useAxiosPatch.ts b/packages/react/src/common/hooks/useAxiosPatch.ts index e901d0ba9..a408bf8b5 100644 --- a/packages/react/src/common/hooks/useAxiosPatch.ts +++ b/packages/react/src/common/hooks/useAxiosPatch.ts @@ -25,39 +25,50 @@ export const useAxiosPatch = ({ const [isLoading, setLoading] = useState(false); const cancelQuery = useCancel(); - const sendRequest = useCallback(async () => { - let didCancel = false; - let source = axios.CancelToken.source(); - cancelQuery(() => { - didCancel = true; - source.cancel('Cancelling in cleanup'); - }); - try { - setLoading(true); - setError(undefined); - const response = await axios.patch(url, data, { - cancelToken: source.token, - timeout: timeout, - headers, - params, + const sendRequest = useCallback( + async ( + overrides?: Partial< + Pick + >, + ) => { + let didCancel = false; + let source = axios.CancelToken.source(); + cancelQuery(() => { + didCancel = true; + source.cancel('Cancelling in cleanup'); }); - if (!didCancel) { - setResponse(response.data); - setLoading(false); - } - return response; - } catch (error: any) { - if (!didCancel) { - setError(error); - setLoading(false); - if (axios.isCancel(error)) { - console.log(`request cancelled: ${error.message}`); - } else { - console.log(`another error happened: ${error.message}`); + try { + setLoading(true); + setError(undefined); + const response = await axios.patch( + overrides?.url || url, + overrides?.data || data, + { + cancelToken: source.token, + timeout: overrides?.timeout || timeout, + headers: overrides?.headers || headers, + params: overrides?.params || params, + }, + ); + if (!didCancel) { + setResponse(response.data); + setLoading(false); + } + return response; + } catch (error: any) { + if (!didCancel) { + setError(error); + setLoading(false); + if (axios.isCancel(error)) { + console.log(`request cancelled: ${error.message}`); + } else { + console.log(`another error happened: ${error.message}`); + } } } - } - }, [cancelQuery, url, data, params, timeout, headers]); + }, + [cancelQuery, url, data, params, timeout, headers], + ); useEffect(() => { if (enabled) {