From bb5f95b81fca2105deb9aef6528e4b7e293403ec Mon Sep 17 00:00:00 2001 From: Shubham Parkhi Date: Wed, 9 Oct 2024 05:59:56 +0530 Subject: [PATCH 01/10] fix(web): disable button if insufficient balance --- .../ActionButton/Modal/ResubmitModal.tsx | 16 +++++++--- .../NavigationButtons/SubmitItemButton.tsx | 26 +++++++++++++---- .../NavigationButtons/SubmitListButton.tsx | 29 ++++++++++++++++--- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/web/src/components/ActionButton/Modal/ResubmitModal.tsx b/web/src/components/ActionButton/Modal/ResubmitModal.tsx index d3a758e..67ebbad 100644 --- a/web/src/components/ActionButton/Modal/ResubmitModal.tsx +++ b/web/src/components/ActionButton/Modal/ResubmitModal.tsx @@ -36,11 +36,19 @@ const ResubmitModal: React.FC = ({ toggleModal, isItem, registryAd const { data: userBalance, isLoading: isBalanceLoading } = useBalance({ address }); const { data: itemData, isLoading: isItemDataLoading } = useItemDetailsQuery(`${itemId}@${registryAddress}`); - const { data: arbitratorExtraData, isLoading: isLoadingExtradata } = useReadCurateV2GetArbitratorExtraData({ + const { + data: arbitratorExtraData, + isLoading: isLoadingExtradata, + isError: isErrorExtradata, + } = useReadCurateV2GetArbitratorExtraData({ address: registryAddress, }); - const { data: removalDeposit, isLoading: isSubmissionDepositLoading } = useReadCurateV2SubmissionBaseDeposit({ + const { + data: removalDeposit, + isLoading: isSubmissionDepositLoading, + isError: isErrorSubmissionDeposit, + } = useReadCurateV2SubmissionBaseDeposit({ address: registryAddress, }); @@ -59,8 +67,8 @@ const ResubmitModal: React.FC = ({ toggleModal, isItem, registryAd const isDisabled = useMemo(() => { if (!userBalance || !depositRequired) return true; - return userBalance?.value < depositRequired; - }, [depositRequired, userBalance]); + return userBalance?.value < depositRequired && isErrorExtradata && isErrorSubmissionDeposit; + }, [depositRequired, userBalance, isErrorExtradata, isErrorSubmissionDeposit]); const { data: config, isError } = useSimulateCurateV2AddItem({ query: { diff --git a/web/src/pages/SubmitItem/NavigationButtons/SubmitItemButton.tsx b/web/src/pages/SubmitItem/NavigationButtons/SubmitItemButton.tsx index f142a1e..6b87f20 100644 --- a/web/src/pages/SubmitItem/NavigationButtons/SubmitItemButton.tsx +++ b/web/src/pages/SubmitItem/NavigationButtons/SubmitItemButton.tsx @@ -1,7 +1,7 @@ import React, { useMemo, useState } from "react"; import styled from "styled-components"; import { Button } from "@kleros/ui-components-library"; -import { usePublicClient } from "wagmi"; +import { useAccount, useBalance, usePublicClient } from "wagmi"; import { EnsureChain } from "components/EnsureChain"; import { useSubmitItemContext } from "context/SubmitItemContext"; import { wrapWithToast } from "utils/wrapWithToast"; @@ -16,14 +16,27 @@ const StyledButton = styled(Button)``; const SubmitItemButton: React.FC = () => { const [isSubmittingItem, setIsSubmittingItem] = useState(false); const publicClient = usePublicClient(); + const { address } = useAccount(); const navigate = useNavigate(); const { fields, submissionDeposit, resetItemData } = useSubmitItemContext(); const { id: registryAddress } = useRegistryDetailsContext(); - const { data: config } = useSimulateCurateV2AddItem({ + const { data: balanceData } = useBalance({ + address: address as `0x${string}` | undefined, + }); + + const insufficientBalance = useMemo(() => { + return balanceData?.value === BigInt(0); + }, [balanceData]); + + const { + data: config, + isLoading: isConfigLoading, + isError: isConfigError, + } = useSimulateCurateV2AddItem({ query: { - enabled: Boolean(fields && submissionDeposit && registryAddress), + enabled: Boolean(fields && submissionDeposit && registryAddress && !insufficientBalance), }, address: registryAddress as `0x${string}`, args: [JSON.stringify(fields)], @@ -32,14 +45,17 @@ const SubmitItemButton: React.FC = () => { const { writeContractAsync: submitItem, isLoading } = useWriteCurateV2AddItem(); - const isButtonDisabled = useMemo(() => isSubmittingItem, [isSubmittingItem]); + const isButtonDisabled = useMemo( + () => isSubmittingItem || isConfigLoading || isConfigError || insufficientBalance, + [isSubmittingItem, isConfigLoading, isConfigError, insufficientBalance] + ); return ( { if (submitItem && publicClient && config) { setIsSubmittingItem(true); diff --git a/web/src/pages/SubmitList/NavigationButtons/SubmitListButton.tsx b/web/src/pages/SubmitList/NavigationButtons/SubmitListButton.tsx index ab86a45..87a8f06 100644 --- a/web/src/pages/SubmitList/NavigationButtons/SubmitListButton.tsx +++ b/web/src/pages/SubmitList/NavigationButtons/SubmitListButton.tsx @@ -52,7 +52,11 @@ const SubmitListButton: React.FC = () => { const listParams = useMemo(() => constructListParams(listData, listMetadata), [listData, listMetadata]); - const { data: config } = useSimulateCurateFactoryDeploy({ + const { + data: config, + isLoading: isConfigLoading, + isError: isConfigError, + } = useSimulateCurateFactoryDeploy({ query: { enabled: areListParamsValid(listParams), }, @@ -156,8 +160,19 @@ const SubmitListButton: React.FC = () => { isLoadingArbCost || isEstimatingCost || isLoadingExtradata || - !totalCostToSubmit, - [isSubmittingList, listParams, isLoadingArbCost, isEstimatingCost, isLoadingExtradata, totalCostToSubmit] + !totalCostToSubmit || + isConfigLoading || + isConfigError, + [ + isSubmittingList, + listParams, + isLoadingArbCost, + isEstimatingCost, + isLoadingExtradata, + totalCostToSubmit, + isConfigLoading, + isConfigError, + ] ); const handleDeploy = () => { @@ -185,7 +200,13 @@ const SubmitListButton: React.FC = () => {