From c935b4c8f0e410b1112c22d05922622fcd979400 Mon Sep 17 00:00:00 2001 From: Chao Zhou Date: Mon, 7 Jun 2021 12:09:33 +0800 Subject: [PATCH] feat: return error to useConfluxPortal hook (#7) --- src/useConfluxPortal.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/useConfluxPortal.js b/src/useConfluxPortal.js index 576e9c3..f99acda 100644 --- a/src/useConfluxPortal.js +++ b/src/useConfluxPortal.js @@ -36,6 +36,7 @@ export default function useConfluxPortal(tokenAddrs = []) { : null ); const [chainId, setChainId] = useState(window?.conflux?.chainId); + const [error,setError]=useState(null) useEffectOnce(() => { window?.conflux?.send({ method: "cfx_accounts" }).then((accounts) => { @@ -60,12 +61,15 @@ export default function useConfluxPortal(tokenAddrs = []) { const login = (fallbackFn) => { if (!address) { - if (window?.conflux?.enable) + if (window?.conflux) return window.conflux - .enable() + .send('cfx_requestAccounts') .then( (addresses) => validAddresses(addresses) && setAddress(addresses[0]) - ); + ) + .catch( + (error)=>setError(error) + ) return typeof fallbackFn === "function" && fallbackFn(); } }; @@ -107,6 +111,7 @@ export default function useConfluxPortal(tokenAddrs = []) { address, balances: [balance, tokenBalances], chainId, + error, login, useEnsurePortalLogin, conflux: window?.conflux,