Skip to content

Commit

Permalink
Cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
hide-on-bush-x committed Feb 10, 2023
1 parent 77088af commit 2442070
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
18 changes: 10 additions & 8 deletions src/common/helpers/provider/modules/credit-scores/credit-scores.ts
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useMemo } from 'react';
import { useQuery } from 'react-query';
import { queryClient } from '../../masa-query-client';
import { ICreditScore, Masa } from '@masa-finance/masa-sdk';
Expand Down Expand Up @@ -26,24 +26,26 @@ export const useCreditScores = (
isLoading: boolean;
error: unknown;
} => {
const queryKey: string = useMemo(() => {
return `credit-scores-${walletAddress}-${masa?.config.network}`;
}, [walletAddress, masa]);

const {
data: creditScores,
status,
isLoading,
error,
} = useQuery(
`credit-scores-${walletAddress}`,
() => masa?.creditScore.list(),
{ enabled: !!masa && !!walletAddress && !!identity?.identityId }
);
} = useQuery(queryKey, () => masa?.creditScore.list(), {
enabled: !!masa && !!walletAddress && !!identity?.identityId,
});

const handleCreateCreditScore = useCallback(async () => {
const response = await masa?.creditScore.create();

await queryClient.invalidateQueries(`credit-scores-${walletAddress}`);
await queryClient.invalidateQueries(queryKey);

return response?.success;
}, [masa, walletAddress]);
}, [masa, walletAddress, queryKey]);

return {
creditScores,
Expand Down
21 changes: 10 additions & 11 deletions src/common/helpers/provider/modules/identity/identity.ts
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useMemo } from 'react';
import { useQuery } from 'react-query';
import { queryClient } from '../../masa-query-client';
import { Masa } from '@masa-finance/masa-sdk';
Expand All @@ -19,21 +19,20 @@ export const useIdentity = (
isLoading: boolean;
error: unknown;
} => {
const queryKey: string = useMemo(() => {
return `identity-${walletAddress}-${masa?.config.network}`;
}, [walletAddress, masa]);
const {
data: identity,
status,
isLoading,
error,
} = useQuery(
`identity-${walletAddress}`,
() => masa?.identity.load(walletAddress),
{
enabled: !!masa && !!walletAddress,
onSuccess: (identity: { identityId?: BigNumber; address?: string }) => {
console.log({ identity });
},
}
);
} = useQuery(queryKey, () => masa?.identity.load(walletAddress), {
enabled: !!masa && !!walletAddress,
onSuccess: (identity: { identityId?: BigNumber; address?: string }) => {
console.log({ identity });
},
});

const handlePurchaseIdentity = useCallback(async () => {
await masa?.identity.create();
Expand Down
21 changes: 13 additions & 8 deletions src/common/helpers/provider/modules/session/session.ts
@@ -1,4 +1,4 @@
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { useQuery } from 'react-query';
import { queryClient } from '../../masa-query-client';
import { Masa } from '@masa-finance/masa-sdk';
Expand All @@ -14,40 +14,45 @@ export const useSession = (
isLoading: boolean;
error: unknown;
} => {

const queryKey: string = useMemo(() => {
return `session-${walletAddress}-${masa?.config.network}`;
}, [walletAddress, masa]);

const {
data: loggedIn,
status,
isLoading,
error,
} = useQuery(`session-${walletAddress}`, () => masa?.session.checkLogin(), {
} = useQuery(queryKey, () => masa?.session.checkLogin(), {
enabled: !!masa && !!walletAddress,
});

useEffect(() => {
if (loggedIn && walletAddress) {
void queryClient.invalidateQueries(`session-${walletAddress}`);
void queryClient.invalidateQueries(queryKey);
}
}, [walletAddress, loggedIn]);
}, [walletAddress, loggedIn, queryKey]);

const login = useCallback(async () => {
const logged = await masa?.session.login();
if (logged) {
await queryClient.invalidateQueries(`session-${walletAddress}`);
await queryClient.invalidateQueries(queryKey);
await queryClient.refetchQueries();
}
}, [masa, walletAddress]);
}, [masa, walletAddress, queryKey]);

const logout = useCallback(
async (callback?: () => void) => {
await masa?.session.logout();
await queryClient.invalidateQueries(`session-${walletAddress}`);
await queryClient.invalidateQueries(queryKey);
await queryClient.refetchQueries();

if (callback) {
callback();
}
},
[masa, walletAddress]
[masa, walletAddress, queryKey]
);

return { loggedIn, login, logout, status, isLoading, error };
Expand Down
2 changes: 1 addition & 1 deletion src/common/helpers/provider/modules/soulnames/soulnames.ts
Expand Up @@ -20,7 +20,7 @@ export const useSoulnames = (
} => {
const queryKey: string = useMemo(() => {
return `soulnames-${walletAddress}-${masa?.config.network}`;
}, [walletAddress, masa?.config.network]);
}, [walletAddress, masa]);

console.log(queryKey);
const {
Expand Down
9 changes: 7 additions & 2 deletions src/common/helpers/provider/modules/wallet/wallet.ts
@@ -1,7 +1,7 @@
import { useQuery } from 'react-query';
import { Masa } from '@masa-finance/masa-sdk';
import { ethers } from 'ethers';
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';

export const useWallet = (
masa: Masa | null,
Expand All @@ -13,12 +13,17 @@ export const useWallet = (
error: unknown;
chain?: null | ethers.providers.Network;
} => {

const queryKey: string = useMemo(() => {
return `wallet-${masa?.config.network}`;
}, [masa]);

const {
data: walletAddress,
status,
isLoading,
error,
} = useQuery(`wallet`, () => masa?.config.wallet.getAddress(), {
} = useQuery(queryKey, () => masa?.config.wallet.getAddress(), {
enabled: !!masa && !!provider,
});

Expand Down

0 comments on commit 2442070

Please sign in to comment.