Skip to content

Commit

Permalink
update Axios hook to make multiple queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thomlamb committed Feb 21, 2025
1 parent e59cd72 commit af95d00
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/imio/smartweb/core/webcomponents/src/hooks/useAxios.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,39 @@ const useAxios = (params) => {
} else {
setIsMore(false);
}
if (Object.keys(params.params).length == 0) {

if (Object.keys(params.params).length === 0) {
setResponse(null);
return;
} else {
try {
}

try {
// Handle multiple URLs
if (Array.isArray(params.url)) {
const requests = params.url.map((url) =>
axios.request({ ...params, url, signal: controller.signal })
);
const responses = await Promise.all(requests);
setResponse(responses.map((res) => res.data));
}
// Handle single URL
else {
const res = await axios.request(params);
setResponse(res.data);
setIsLoading(false);
setError(null);
} catch (err) {
setError(err);
}
setIsLoading(false);
setError(null);
} catch (err) {
setError(err);
setIsLoading(false);
}
};

useEffect(() => {
fetchData({ ...params, signal: controller.signal });
return () => controller.abort();
}, [params.params]);

return { response, error, isLoading, isMore };
};

Expand Down

0 comments on commit af95d00

Please sign in to comment.