From fa3ec58d26820a2df8ae32935a8771db47bf6250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8r=E2=88=82=C2=A1?= Date: Fri, 23 Aug 2024 17:53:50 +0200 Subject: [PATCH] Improved delegate flow --- .../members/hooks/useAnnounceDelegation.tsx | 9 ++++-- plugins/members/hooks/useDelegates.tsx | 30 ++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/plugins/members/hooks/useAnnounceDelegation.tsx b/plugins/members/hooks/useAnnounceDelegation.tsx index 4d3a10c..12dd3b6 100644 --- a/plugins/members/hooks/useAnnounceDelegation.tsx +++ b/plugins/members/hooks/useAnnounceDelegation.tsx @@ -1,15 +1,17 @@ import { DelegateAnnouncerAbi } from "../artifacts/DelegationWall.sol"; import { PUB_DELEGATION_WALL_CONTRACT_ADDRESS } from "@/constants"; +import { useWaitForTransactionReceipt, useWriteContract } from "wagmi"; +import { toHex } from "viem"; import { useAlerts } from "@/context/Alerts"; import { uploadToPinata } from "@/utils/ipfs"; import { useCallback, useEffect, useState } from "react"; -import { toHex } from "viem"; -import { useWaitForTransactionReceipt, useWriteContract } from "wagmi"; import { type IAnnouncementMetadata } from "../utils/types"; +import { useDelegates } from "./useDelegates"; export function useAnnounceDelegation(onSuccess?: () => void) { const { addAlert } = useAlerts(); const { writeContract, data: hash, error, status } = useWriteContract(); + const { refetch } = useDelegates(); const [uploading, setUploading] = useState(false); const { isLoading: isConfirming, isSuccess: isConfirmed } = useWaitForTransactionReceipt({ hash }); @@ -44,6 +46,9 @@ export function useAnnounceDelegation(onSuccess?: () => void) { txHash: hash, }); + // Force a refresh of the delegates list + refetch(); + onSuccess?.(); }, [status, hash, isConfirming, isConfirmed]); diff --git a/plugins/members/hooks/useDelegates.tsx b/plugins/members/hooks/useDelegates.tsx index 369a3bf..8e038bc 100644 --- a/plugins/members/hooks/useDelegates.tsx +++ b/plugins/members/hooks/useDelegates.tsx @@ -1,30 +1,24 @@ -import { useConfig } from "wagmi"; +import { useReadContract } from "wagmi"; import { DelegateAnnouncerAbi } from "@/plugins/members/artifacts/DelegationWall.sol"; import { PUB_DELEGATION_WALL_CONTRACT_ADDRESS } from "@/constants"; -import { readContract } from "@wagmi/core"; -import { useQuery } from "@tanstack/react-query"; import { useShuffled } from "@/hooks/useShuffled"; /** Returns the list of delegates who posted a candidacy */ export function useDelegates() { - const config = useConfig(); + const { data, status, refetch } = useReadContract({ + abi: DelegateAnnouncerAbi, + address: PUB_DELEGATION_WALL_CONTRACT_ADDRESS, + functionName: "getCandidateAddresses", - const { data, status, refetch } = useQuery({ - queryKey: ["delegate-addresses-list", PUB_DELEGATION_WALL_CONTRACT_ADDRESS], - queryFn: () => { - return readContract(config, { - abi: DelegateAnnouncerAbi, - address: PUB_DELEGATION_WALL_CONTRACT_ADDRESS, - functionName: "getCandidateAddresses", - args: [], - }); + query: { + retry: true, + refetchOnMount: false, + refetchOnReconnect: false, + retryOnMount: true, + staleTime: 1000 * 60 * 10, }, - retry: true, - refetchOnMount: false, - refetchOnReconnect: false, - retryOnMount: true, - staleTime: 1000 * 60 * 10, }); + const shuffledDelegates = useShuffled(data); return { delegates: shuffledDelegates, status, refetch };