From 3d96d83232f946be940b18eca443b9d75c345162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Sworze=C5=84?= Date: Mon, 26 Feb 2024 15:20:54 +0100 Subject: [PATCH] [#265] make button disabled when tries to connect --- CHANGELOG.md | 1 + .../src/components/molecules/WalletOption.tsx | 81 ++++++++++++------- govtool/frontend/src/context/wallet.tsx | 7 ++ 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90954bacd..b80f9c5f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ changes. - i18next library added to FE [Issue 80](https://github.com/IntersectMBO/govtool/issues/80) ### Fixed +- Fix make button disble when wallet tries connect [Issue 265](https://github.com/IntersectMBO/govtool/issues/265) - Fix drep voting power calculation [Issue 231](https://github.com/IntersectMBO/govtool/issues/231) - Fix proposal/list and network/metrics bug that appeared when noone has delegated their funds either to drep_always_abstain or drep_always_no_confidence [Issue 231](https://github.com/IntersectMBO/govtool/issues/231) - Fix GA details [Issue 272](https://github.com/IntersectMBO/govtool/issues/272) diff --git a/govtool/frontend/src/components/molecules/WalletOption.tsx b/govtool/frontend/src/components/molecules/WalletOption.tsx index 94c5f9ed7..a1a14dd69 100644 --- a/govtool/frontend/src/components/molecules/WalletOption.tsx +++ b/govtool/frontend/src/components/molecules/WalletOption.tsx @@ -1,10 +1,10 @@ -import { Box, Typography } from "@mui/material"; -import { FC } from "react"; +import { FC, useCallback } from "react"; +import { useNavigate } from "react-router-dom"; +import { Box, CircularProgress, Typography } from "@mui/material"; +import { PATHS } from "@consts"; import { useCardano } from "@context"; import { theme } from "@/theme"; -import { useNavigate } from "react-router-dom"; -import { PATHS } from "@/consts"; export interface WalletOption { icon: string; @@ -15,7 +15,7 @@ export interface WalletOption { } export const WalletOptionButton: FC = ({ ...props }) => { - const { enable } = useCardano(); + const { enable, isEnableLoading } = useCardano(); const { palette: { lightBlue }, } = theme; @@ -23,52 +23,77 @@ export const WalletOptionButton: FC = ({ ...props }) => { const { dataTestId, icon, label, name, cip95Available } = props; + const enableByWalletName = useCallback(async() => { + if(isEnableLoading) return; + const result = await enable(name); + if (result?.stakeKey) { + navigate(PATHS.dashboard); + return; + } + navigate(PATHS.stakeKeys); + }, [enable, isEnableLoading]) + return ( { - const result = await enable(name); - if (result?.stakeKey) { - navigate(PATHS.dashboard); - return; - } - navigate(PATHS.stakeKeys); - }} + onClick={enableByWalletName} > {`${name} {name ?? label} - {`${name} +
+ {isEnableLoading === name && ( + + + + )} ); }; diff --git a/govtool/frontend/src/context/wallet.tsx b/govtool/frontend/src/context/wallet.tsx index 3d49f7a25..765e24226 100644 --- a/govtool/frontend/src/context/wallet.tsx +++ b/govtool/frontend/src/context/wallet.tsx @@ -97,6 +97,7 @@ interface CardanoContext { address?: string; disconnectWallet: () => Promise; enable: (walletName: string) => Promise; + isEnableLoading: string | null; error?: string; dRep: DRepInfo | undefined; isEnabled: boolean; @@ -165,6 +166,7 @@ CardanoContext.displayName = "CardanoContext"; function CardanoProvider(props: Props) { const [isEnabled, setIsEnabled] = useState(false); + const [isEnableLoading, setIsEnableLoading] = useState(null); const [dRep, setDRep] = useState(undefined); const [walletApi, setWalletApi] = useState( undefined @@ -517,6 +519,7 @@ function CardanoProvider(props: Props) { const enable = useCallback( async (walletName: string) => { + setIsEnableLoading(walletName); await checkIsMaintenanceOn(); // todo: use .getSupportedExtensions() to check if wallet supports CIP-95 @@ -650,6 +653,8 @@ function CardanoProvider(props: Props) { status: "ERROR", error: `${e == undefined ? t("errors.somethingWentWrong") : e}`, }; + } finally { + setIsEnableLoading(null); } } throw { status: "ERROR", error: t("errors.somethingWentWrong") }; @@ -1141,6 +1146,7 @@ function CardanoProvider(props: Props) { isPendingTransaction, isDrepLoading, setIsDrepLoading, + isEnableLoading, }), [ address, @@ -1173,6 +1179,7 @@ function CardanoProvider(props: Props) { isPendingTransaction, isDrepLoading, setIsDrepLoading, + isEnableLoading, ] );