From fa3324eb62a1b37aae5ee018f9b0549b9168e4c9 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 8 Aug 2024 18:16:07 +0530 Subject: [PATCH] Handle redirects --- src/App.tsx | 6 +- src/layout/SignPageLayout.tsx | 2 +- src/pages/ConnectWallet.tsx | 15 +++-- src/pages/SignWithNitroKey.tsx | 17 +++-- src/pages/Thanks.tsx | 2 +- .../{CreateValidator.tsx => Validator.tsx} | 64 +++++++++---------- 6 files changed, 58 insertions(+), 48 deletions(-) rename src/pages/{CreateValidator.tsx => Validator.tsx} (82%) diff --git a/src/App.tsx b/src/App.tsx index 71cb74f..e82836c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -14,7 +14,7 @@ import { WalletConnectProvider } from "./context/WalletConnectContext"; import VerifyEmail from "./pages/VerifyEmail"; import Email from "./pages/Email"; import Thanks from "./pages/Thanks"; -import CreateValidator from "./pages/CreateValidator"; +import Validator from "./pages/Validator"; function App() { return ( @@ -25,7 +25,7 @@ function App() { } /> } /> } /> - } /> + } /> } /> }> } /> @@ -43,7 +43,7 @@ function App() { > } + element={} > } /> diff --git a/src/layout/SignPageLayout.tsx b/src/layout/SignPageLayout.tsx index 208d503..b985f1c 100644 --- a/src/layout/SignPageLayout.tsx +++ b/src/layout/SignPageLayout.tsx @@ -21,7 +21,7 @@ const SignPageLayout = () => { const redirectTo = pathname ? pathname.substring(1) : ""; await disconnect(); - navigate(`/connect-wallet/${redirectTo}`); + navigate(`/connect-wallet?redirectTo=${redirectTo}`); }; return ( diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index aabaa2e..7492805 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from "react"; -import {useNavigate, useParams } from "react-router-dom"; +import {useLocation, useNavigate, useParams, useSearchParams } from "react-router-dom"; import { Button, Box, Container, Typography, colors } from "@mui/material"; @@ -7,14 +7,21 @@ import { useWalletConnectContext } from "../context/WalletConnectContext"; import { WALLET_DISCLAIMER_MSG } from "../constants"; const ConnectWallet = () => { - const { connect, session } = useWalletConnectContext(); + const { connect, session, signClient, checkPersistedState } = useWalletConnectContext(); const navigate = useNavigate(); const location = useLocation(); - const {redirectTo} = useParams(); + const [searchParams] = useSearchParams(); + + const redirectTo = searchParams.get("redirectTo") + + useEffect(()=>{ + if (signClient && !session) { + checkPersistedState(signClient); + } + }, [checkPersistedState, signClient, session]); useEffect(() => { - if (session) { if (redirectTo){ navigate(`/${redirectTo}`, { diff --git a/src/pages/SignWithNitroKey.tsx b/src/pages/SignWithNitroKey.tsx index 51b9754..709b492 100644 --- a/src/pages/SignWithNitroKey.tsx +++ b/src/pages/SignWithNitroKey.tsx @@ -17,17 +17,20 @@ import { ENABLE_KYC } from "../constants"; const SignWithNitroKey = () => { - const { session, signClient, checkPersistedState } = + const { session, signClient } = useWalletConnectContext(); - useEffect(() => { - if (signClient && !session) { - checkPersistedState(signClient); - } - }, [session, signClient, checkPersistedState]); - const navigate = useNavigate(); const location = useLocation(); + + useEffect(() => { + if (!session){ + navigate("/connect-wallet?redirectTo=sign-with-nitro-key", { + state: location.state, + }); + } + }, [session, navigate, location.state]); + const [ethAddress, setEthAddress] = useState(""); const [ethSignature, setEthSignature] = useState(""); diff --git a/src/pages/Thanks.tsx b/src/pages/Thanks.tsx index c8a18f0..b73803b 100644 --- a/src/pages/Thanks.tsx +++ b/src/pages/Thanks.tsx @@ -41,7 +41,7 @@ const Thanks: React.FC = () => { const subscriberIdBytes = ethers.utils.toUtf8Bytes(decoded.subscriber_id) const subscriberIdHash = ethers.utils.sha256(subscriberIdBytes); - navigate('/connect-wallet', { + navigate('/sign-with-nitro-key', { state:{ subscriberIdHash } diff --git a/src/pages/CreateValidator.tsx b/src/pages/Validator.tsx similarity index 82% rename from src/pages/CreateValidator.tsx rename to src/pages/Validator.tsx index 70bf382..67daa80 100644 --- a/src/pages/CreateValidator.tsx +++ b/src/pages/Validator.tsx @@ -12,24 +12,24 @@ import { Registry } from '@cerc-io/registry-sdk'; import { useWalletConnectContext } from '../context/WalletConnectContext' import { Participant } from '../types'; -const CreateValidator = () => { +const Validator = () => { const {session, signClient} = useWalletConnectContext(); const navigate = useNavigate(); const [cosmosAddress, setCosmosAddress] = useState('laconic1z4l4556v8jnk7k456ujme720nk8mzjwhka7qvw'); const [isLoading, setIsLoading] = useState(false); - const [pubkey, setPubkey] = useState('d5aOEYaCIHaDn6kG0tAg699D7sLgAgkJI5reTMl0o5U='); + const [pubkey, setPubkey] = useState('qiQEvyqIf9/e912ZEIZD5CfdqR3iOlpI+NVtRnpHuYc='); const [participant, setParticipant] = useState(null); if (!session){ - navigate("/connect-wallet") + navigate("/connect-wallet?redirectTo=create-validator"); } const msgCreateValidator: MsgCreateValidator = useMemo(() => { return { description: { - moniker: "dockerNode", + moniker: "dockerNode2", identity: "", website: "", securityContact: "", @@ -41,7 +41,7 @@ const CreateValidator = () => { rate: "100000000000000000", // 0.1 }, minSelfDelegation: "1", - delegatorAddress: cosmosAddress, + delegatorAddress: '', validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data), pubkey: encodePubkey({ type: "tendermint/PubKeyEd25519", @@ -183,38 +183,38 @@ const CreateValidator = () => { )} - {participant && participant.role === "validator" && ( - <> - Send transaction to chain - + Send transaction to chain + +
+                {JSON.stringify(msgCreateValidator, null, 2)}
+              
+
+ + { + console.log(msgCreateValidatorEncodeObject); + await sendTransaction(msgCreateValidatorEncodeObject); }} + loading={isLoading} > -
-                  {JSON.stringify(msgCreateValidator, null, 2)}
-                
-
- - { - console.log(msgCreateValidatorEncodeObject); - await sendTransaction(msgCreateValidatorEncodeObject); - }} - loading={isLoading} - > - Send transaction - - - - )} + Send transaction + +
+ + {/* )} */} )} ) } -export default CreateValidator +export default Validator