Skip to content

Commit

Permalink
fix: hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteolaf committed Aug 14, 2023
1 parent 22c19d3 commit 8e553a3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/contracts/identity/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ const IdentityContractProvider = ({ children }: Props) => {
const [identityNo, setIdentityNo] = useState<IdentityNo>(null);
const [networks, setNetworks] = useState<Networks>({});
const [addresses, setAddresses] = useState<Array<Address>>([]);
const [loading, setLoading] = useState(false);
const [loadingIdentityNo, setLoadingIdentityNo] = useState(false);
const [loadingNetworks, setLoadingNetworks] = useState(false);
const { toastError } = useToast();

const fetchIdentityNo = useCallback(async () => {
if (!api || !contract || !activeAccount) {
setIdentityNo(null);
return;
}
setLoadingIdentityNo(true);
try {
const result = await contractQuery(api, '', contract, 'identity_of', {}, [
activeAccount.address,
Expand All @@ -87,6 +89,7 @@ const IdentityContractProvider = ({ children }: Props) => {
} catch (e) {
setIdentityNo(null);
}
setLoadingIdentityNo(false);
}, [activeAccount, api, contract]);

const fetchNetworks = useCallback(async () => {
Expand All @@ -95,8 +98,6 @@ const IdentityContractProvider = ({ children }: Props) => {
return;
}

if (Object.keys(networks).length) return;

const getChainInfo = async (
rpcUrls: string[]
): Promise<NetworkConsts | null> => {
Expand Down Expand Up @@ -125,6 +126,7 @@ const IdentityContractProvider = ({ children }: Props) => {
}
};

setLoadingNetworks(true);
try {
const result = await contractQuery(
api,
Expand Down Expand Up @@ -157,6 +159,7 @@ const IdentityContractProvider = ({ children }: Props) => {
} catch (e: any) {
toastError(e.toString());
}
setLoadingNetworks(false);
}, [api, contract, toastError]);

const fetchAddresses = useCallback(async () => {
Expand Down Expand Up @@ -196,13 +199,12 @@ const IdentityContractProvider = ({ children }: Props) => {
}, [api, contract, identityNo, fetchAddresses]);

useEffect(() => {
const init = async () => {
setLoading(true);
await Promise.all([fetchIdentityNo(), fetchNetworks()]);
setLoading(false);
};
init();
}, [fetchIdentityNo, fetchNetworks]);
fetchIdentityNo();
}, [api, contract, activeAccount]);

useEffect(() => {
fetchNetworks();
}, [api?.isReady, contract?.address]);

return (
<IdentityContext.Provider
Expand All @@ -213,7 +215,7 @@ const IdentityContractProvider = ({ children }: Props) => {
networks,
fetchAddresses,
fetchIdentityNo,
loading,
loading: loadingIdentityNo || loadingNetworks,
}}
>
{children}
Expand Down

0 comments on commit 8e553a3

Please sign in to comment.