Skip to content

Commit

Permalink
Merge pull request #1171 from interlay/tom/staking-refactor
Browse files Browse the repository at this point in the history
refactor: invalidate queries
  • Loading branch information
tomjeatt authored Apr 28, 2023
2 parents 6f7c8ab + 1ee800d commit c3ae461
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/pages/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as React from 'react';
import { useErrorHandler, withErrorBoundary } from 'react-error-boundary';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useMutation, useQuery } from 'react-query';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { useSelector } from 'react-redux';

import { StoreType } from '@/common/types/util.types';
Expand Down Expand Up @@ -119,6 +119,8 @@ const Staking = (): JSX.Element => {
const { data: balances, isLoading: isBalancesLoading } = useGetBalances();
const governanceTokenBalance = balances?.[GOVERNANCE_TOKEN.ticker];

const queryClient = useQueryClient();

const {
register,
handleSubmit,
Expand Down Expand Up @@ -151,8 +153,7 @@ const Staking = (): JSX.Element => {
isIdle: voteGovernanceTokenBalanceIdle,
isLoading: voteGovernanceTokenBalanceLoading,
data: voteGovernanceTokenBalance,
error: voteGovernanceTokenBalanceError,
refetch: voteGovernanceTokenBalanceRefetch
error: voteGovernanceTokenBalanceError
} = useQuery<VoteGovernanceTokenMonetaryAmount, Error>(
[GENERIC_FETCHER, 'escrow', 'votingBalance', selectedAccountAddress],
genericFetcher<VoteGovernanceTokenMonetaryAmount>(),
Expand All @@ -167,8 +168,7 @@ const Staking = (): JSX.Element => {
isIdle: claimableRewardAmountIdle,
isLoading: claimableRewardAmountLoading,
data: claimableRewardAmount,
error: claimableRewardAmountError,
refetch: claimableRewardAmountRefetch
error: claimableRewardAmountError
} = useQuery<GovernanceTokenMonetaryAmount, Error>(
[GENERIC_FETCHER, 'escrow', 'getRewards', selectedAccountAddress],
genericFetcher<GovernanceTokenMonetaryAmount>(),
Expand All @@ -183,8 +183,7 @@ const Staking = (): JSX.Element => {
isIdle: projectedRewardAmountAndAPYIdle,
isLoading: projectedRewardAmountAndAPYLoading,
data: projectedRewardAmountAndAPY,
error: rewardAmountAndAPYError,
refetch: rewardAmountAndAPYRefetch
error: rewardAmountAndAPYError
} = useQuery<EstimatedRewardAmountAndAPY, Error>(
[GENERIC_FETCHER, 'escrow', 'getRewardEstimate', selectedAccountAddress],
genericFetcher<EstimatedRewardAmountAndAPY>(),
Expand Down Expand Up @@ -222,8 +221,7 @@ const Staking = (): JSX.Element => {
isIdle: stakedAmountAndEndBlockIdle,
isLoading: stakedAmountAndEndBlockLoading,
data: stakedAmountAndEndBlock,
error: stakedAmountAndEndBlockError,
refetch: stakedAmountAndEndBlockRefetch
error: stakedAmountAndEndBlockError
} = useQuery<StakedAmountAndEndBlock, Error>(
[GENERIC_FETCHER, 'escrow', 'getStakedBalance', selectedAccountAddress],
genericFetcher<StakedAmountAndEndBlock>(),
Expand Down Expand Up @@ -258,10 +256,7 @@ const Staking = (): JSX.Element => {
},
{
onSuccess: () => {
voteGovernanceTokenBalanceRefetch();
stakedAmountAndEndBlockRefetch();
claimableRewardAmountRefetch();
rewardAmountAndAPYRefetch();
queryClient.invalidateQueries({ queryKey: [GENERIC_FETCHER, 'escrow'] });
reset({
[LOCKING_AMOUNT]: '0.0',
[LOCK_TIME]: '0'
Expand Down Expand Up @@ -299,10 +294,7 @@ const Staking = (): JSX.Element => {
},
{
onSuccess: () => {
voteGovernanceTokenBalanceRefetch();
stakedAmountAndEndBlockRefetch();
claimableRewardAmountRefetch();
rewardAmountAndAPYRefetch();
queryClient.invalidateQueries({ queryKey: [GENERIC_FETCHER, 'escrow'] });
reset({
[LOCKING_AMOUNT]: '0.0',
[LOCK_TIME]: '0'
Expand All @@ -312,11 +304,11 @@ const Staking = (): JSX.Element => {
);

React.useEffect(() => {
if (isValidating || !isValid) return;
if (isValidating || !isValid || !estimatedRewardAmountAndAPYRefetch) return;

estimatedRewardAmountAndAPYRefetch();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isValid, isValidating, monetaryLockingAmount, blockLockTimeExtension]);
}, [isValid, isValidating, lockTime, lockingAmount, estimatedRewardAmountAndAPYRefetch]);

React.useEffect(() => {
if (!lockTime) return;
Expand All @@ -330,10 +322,12 @@ const Staking = (): JSX.Element => {
}, [currentBlockNumber, lockTime, stakedAmountAndEndBlock]);

React.useEffect(() => {
queryClient.invalidateQueries({ queryKey: [GENERIC_FETCHER, 'escrow'] });
reset({
[LOCKING_AMOUNT]: '',
[LOCK_TIME]: ''
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedAccount, reset]);

const votingBalanceGreaterThanZero = voteGovernanceTokenBalance?.gt(ZERO_VOTE_GOVERNANCE_TOKEN_AMOUNT);
Expand Down

2 comments on commit c3ae461

@vercel
Copy link

@vercel vercel bot commented on c3ae461 Apr 28, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on c3ae461 Apr 28, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.