Skip to content

Commit

Permalink
feat: allow overriding patch request call
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Sep 24, 2022
1 parent 81e52a2 commit 41d4751
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions packages/react/src/common/hooks/useAxiosPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,50 @@ export const useAxiosPatch = <T>({
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<T>(url, data, {
cancelToken: source.token,
timeout: timeout,
headers,
params,
const sendRequest = useCallback(
async (
overrides?: Partial<
Pick<Config, 'url' | 'data' | 'params' | 'headers' | 'timeout'>
>,
) => {
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<T>(
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) {
Expand Down

0 comments on commit 41d4751

Please sign in to comment.