Skip to content

Commit

Permalink
Merge pull request #3029 from cardano-foundation/fix/MET-1918-fetch-data
Browse files Browse the repository at this point in the history
fix: MET-1918 fix fetch data
  • Loading branch information
Sotatek-TaiTruong authored Feb 19, 2024
2 parents 59b46de + 12c5bc0 commit 4e001f2
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/commons/hooks/useFetchInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const useFetchInterval = <T>(
const [error, setError] = useState<string | null>(null);
const lastFetch = useRef<number>();
const intervalRef = useRef<any>();
const isFetchedDataWhenError = useRef(false);

const fetch = useCallback(
async (needLoading?: boolean, abortSignal?: AbortController) => {
Expand All @@ -38,26 +39,24 @@ const useFetchInterval = <T>(
setError(null);
setInitialized(true);
} catch (error) {
if (urlAlt) {
if (urlAlt && !isFetchedDataWhenError.current) {
defaultAxios
.get(urlAlt, {
signal: abortSignal?.signal
})
.then((res) => {
setData(res?.data as T);
isFetchedDataWhenError.current = true;
})
.catch(() => {
setData(null);
});
} else {
setInitialized(true);
setData(null);
if (error instanceof AxiosError) setError(error?.response?.data?.message || error?.message);
else if (typeof error === "string") setError(error);
}

if (intervalRef.current) {
clearInterval(intervalRef.current);
}
setInitialized(true);
// setData(null);
if (error instanceof AxiosError) setError(error?.response?.data?.message || error?.message);
else if (typeof error === "string") setError(error);
}
lastFetch.current = Date.now();
if (needLoading) setLoading(false);
Expand Down

0 comments on commit 4e001f2

Please sign in to comment.