Skip to content

Commit

Permalink
feat: add onSettled callback
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Jul 26, 2024
1 parent 7774e1e commit 7707a2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
33 changes: 17 additions & 16 deletions libs/defi/oeth/src/redeem/components/ClaimForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';

import {
Button,
Expand Down Expand Up @@ -53,6 +53,10 @@ export const ClaimForm = (props: StackProps) => {
isResultProcessed: (prev, next) =>
prev.filter((r) => r.claimed).length <
next.filter((r) => r.claimed).length,
onSettled: () => {
setSelectedClaimIds([]);
queryClient.invalidateQueries();
},
});
const args =
selectedClaimIds.length === 1
Expand Down Expand Up @@ -91,13 +95,6 @@ export const ClaimForm = (props: StackProps) => {
enableGas: true,
});

useEffect(() => {
if (['timeout', 'processed', 'error'].includes(status)) {
setSelectedClaimIds([]);
queryClient.invalidateQueries();
}
}, [address, queryClient, status]);

const handleClaimClick = (requestId: bigint) => () => {
const idx = selectedClaimIds.findIndex((id) => id === requestId);
if (idx === -1) {
Expand Down Expand Up @@ -170,14 +167,18 @@ export const ClaimForm = (props: StackProps) => {
callbacks={callbacks}
variant="action"
disabled={isNilOrEmpty(selectedClaimIds) || status === 'polling'}
label={intl.formatMessage(
{ defaultMessage: 'Claim{amount}' },
{
amount: eq(selectedAmount, 0)
? ''
: ` ${format(selectedAmount, getFormatPrecision(selectedAmount))} ${tokens.mainnet.WETH.symbol}`,
},
)}
label={
status === 'polling'
? intl.formatMessage({ defaultMessage: 'Processing' })
: intl.formatMessage(
{ defaultMessage: 'Claim{amount}' },
{
amount: eq(selectedAmount, 0)
? ''
: ` ${format(selectedAmount, getFormatPrecision(selectedAmount))} ${tokens.mainnet.WETH.symbol}`,
},
)
}
/>
</Stack>
);
Expand Down
6 changes: 6 additions & 0 deletions libs/shared/providers/src/refresher/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type UseRefresherProps<QueryResult = any> = {
queryKey: QueryKey;
queryFn: QueryFunction<QueryResult>;
isResultProcessed: (prev: QueryResult, next: QueryResult) => boolean;
onSettled?: (status: RefreshStatus, next: QueryResult) => void;
maxRetries?: number;
interval?: number;
};
Expand All @@ -25,6 +26,7 @@ export const useRefresher = <QueryResult = any>({
queryKey,
queryFn,
isResultProcessed,
onSettled,
maxRetries = 10,
interval = 2000,
}: UseRefresherProps<QueryResult>) => {
Expand Down Expand Up @@ -58,16 +60,20 @@ export const useRefresher = <QueryResult = any>({
if (!prev || !next) {
setPollInterval(undefined);
setStatus('error');
onSettled?.('error', next);
} else if (retries > maxRetries) {
setPollInterval(undefined);
setStatus('timeout');
onSettled?.('timeout', next);
} else if (isResultProcessed(prev, next)) {
setPollInterval(undefined);
setStatus('processed');
onSettled?.('processed', next);
}
} catch {
setPollInterval(undefined);
setStatus('error');
onSettled?.('error', next);
}
setRetries((prev) => prev + 1);
})();
Expand Down

0 comments on commit 7707a2e

Please sign in to comment.