Skip to content

Commit

Permalink
feat(react): implement retries for token fetching from address in use…
Browse files Browse the repository at this point in the history
… watch kpi token (#274)

* feat(react): implement retries for token fetching from address in use watch kpi token

* fix(react): clear interval on unmount

---------

Co-authored-by: Federico Luzzi <[email protected]>
  • Loading branch information
luzzif and Federico Luzzi authored May 31, 2023
1 parent bfd298a commit e1ec7d3
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/react/src/hooks/useWatchKPIToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export function useWatchKPIToken(
const [kpiTokenWithData, setKPITokenWithData] =
useState<ResolvedKPITokenWithData | null>(null);

// in case an address was passed, fetch the kpi token
// in case an address was passed, fetch the kpi token (with retries)
useEffect(() => {
if (kpiToken) return;
if (typeof kpiTokenOrAddress !== "string") return;
let cancelled = false;
const fetchData = async () => {
const interval = setInterval(async () => {
try {
const kpiToken = (
await Fetcher.fetchKPITokens({
Expand All @@ -38,27 +38,27 @@ export function useWatchKPIToken(
addresses: [kpiTokenOrAddress],
})
)[kpiTokenOrAddress];
if (!kpiToken)
console.warn(
`no kpi token with address ${kpiTokenOrAddress} found`
);
if (!kpiToken) return;
const resolvedKPIToken = (
await Fetcher.resolveKPITokens({
ipfsGatewayURL,
kpiTokens: [kpiToken],
})
)[kpiTokenOrAddress];
if (!cancelled) setKPIToken(resolvedKPIToken);
if (!cancelled) {
setKPIToken(resolvedKPIToken);
clearInterval(interval);
}
} catch (error) {
console.error(
`could not fetch kpi token with address ${kpiTokenOrAddress}`,
error
);
}
};
void fetchData();
}, 1_000);
return () => {
cancelled = true;
clearInterval(interval);
};
}, [
ipfsGatewayURL,
Expand Down

2 comments on commit e1ec7d3

@vercel
Copy link

@vercel vercel bot commented on e1ec7d3 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./packages/ui

ui-carrot-kpi.vercel.app
ui-git-main-carrot-kpi.vercel.app
ui.carrot-kpi.dev

@vercel
Copy link

@vercel vercel bot commented on e1ec7d3 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

host-frontend – ./packages/frontend

host-frontend-carrot-kpi.vercel.app
host-frontend-git-main-carrot-kpi.vercel.app

Please sign in to comment.