Handle redirects

This commit is contained in:
Adw8 2024-08-08 18:16:07 +05:30
parent 36bc969804
commit a42684533b
6 changed files with 59 additions and 48 deletions

View File

@ -14,7 +14,7 @@ import { WalletConnectProvider } from "./context/WalletConnectContext";
import VerifyEmail from "./pages/VerifyEmail"; import VerifyEmail from "./pages/VerifyEmail";
import Email from "./pages/Email"; import Email from "./pages/Email";
import Thanks from "./pages/Thanks"; import Thanks from "./pages/Thanks";
import CreateValidator from "./pages/CreateValidator"; import Validator from "./pages/Validator";
function App() { function App() {
return ( return (
@ -25,7 +25,7 @@ function App() {
<Route path="/" element={<LandingPage />} /> <Route path="/" element={<LandingPage />} />
<Route path="/verify-email" element={<VerifyEmail />} /> <Route path="/verify-email" element={<VerifyEmail />} />
<Route path="/email" element={<Email/>} /> <Route path="/email" element={<Email/>} />
<Route path="/connect-wallet/:redirectTo?" element={<ConnectWallet />} /> <Route path="/connect-wallet" element={<ConnectWallet />} />
<Route path="/thanks" element={<Thanks />} /> <Route path="/thanks" element={<Thanks />} />
<Route element={<SignPageLayout />}> <Route element={<SignPageLayout />}>
<Route path="/sign-with-nitro-key" element={<SignWithNitroKey />} /> <Route path="/sign-with-nitro-key" element={<SignWithNitroKey />} />
@ -43,7 +43,7 @@ function App() {
></Route> ></Route>
<Route <Route
path="/validator" path="/validator"
element={<CreateValidator />} element={<Validator />}
></Route> ></Route>
</Route> </Route>
<Route path="*" element={<PageNotFound />} /> <Route path="*" element={<PageNotFound />} />

View File

@ -21,7 +21,7 @@ const SignPageLayout = () => {
const redirectTo = pathname ? pathname.substring(1) : ""; const redirectTo = pathname ? pathname.substring(1) : "";
await disconnect(); await disconnect();
navigate(`/connect-wallet/${redirectTo}`); navigate(`/connect-wallet?redirectTo=${redirectTo}`);
}; };
return ( return (

View File

@ -1,5 +1,5 @@
import React, { useEffect } from "react"; 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"; import { Button, Box, Container, Typography, colors } from "@mui/material";
@ -7,14 +7,21 @@ import { useWalletConnectContext } from "../context/WalletConnectContext";
import { WALLET_DISCLAIMER_MSG } from "../constants"; import { WALLET_DISCLAIMER_MSG } from "../constants";
const ConnectWallet = () => { const ConnectWallet = () => {
const { connect, session } = useWalletConnectContext(); const { connect, session, signClient, checkPersistedState } = useWalletConnectContext();
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const {redirectTo} = useParams(); const [searchParams] = useSearchParams();
const redirectTo = searchParams.get("redirectTo")
useEffect(()=>{
if (signClient && !session) {
checkPersistedState(signClient);
}
}, [checkPersistedState, signClient, session]);
useEffect(() => { useEffect(() => {
if (session) { if (session) {
if (redirectTo){ if (redirectTo){
navigate(`/${redirectTo}`, { navigate(`/${redirectTo}`, {

View File

@ -17,16 +17,20 @@ import { ENABLE_KYC, SUBSCRIBER_ID_HASH_KEY } from "../constants";
const SignWithNitroKey = () => { const SignWithNitroKey = () => {
const { session, signClient, checkPersistedState } = const { session, signClient } =
useWalletConnectContext(); useWalletConnectContext();
useEffect(() => {
if (signClient && !session) {
checkPersistedState(signClient);
}
}, [session, signClient, checkPersistedState]);
const navigate = useNavigate(); 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 [ethAddress, setEthAddress] = useState("");
const [ethSignature, setEthSignature] = useState(""); const [ethSignature, setEthSignature] = useState("");

View File

@ -45,7 +45,7 @@ const Thanks: React.FC = () => {
localStorage.setItem(SUBSCRIBER_ID_HASH_KEY, subscriberIdHash); localStorage.setItem(SUBSCRIBER_ID_HASH_KEY, subscriberIdHash);
navigate('/connect-wallet'); navigate('/sign-with-nitro-key');
} catch (error) { } catch (error) {
setErr(String(error)); setErr(String(error));
} }

View File

@ -12,24 +12,24 @@ import { Registry } from '@cerc-io/registry-sdk';
import { useWalletConnectContext } from '../context/WalletConnectContext' import { useWalletConnectContext } from '../context/WalletConnectContext'
import { Participant } from '../types'; import { Participant } from '../types';
const CreateValidator = () => { const Validator = () => {
const {session, signClient} = useWalletConnectContext(); const {session, signClient} = useWalletConnectContext();
const navigate = useNavigate(); const navigate = useNavigate();
const [cosmosAddress, setCosmosAddress] = useState('laconic1z4l4556v8jnk7k456ujme720nk8mzjwhka7qvw'); const [cosmosAddress, setCosmosAddress] = useState('laconic1z4l4556v8jnk7k456ujme720nk8mzjwhka7qvw');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [pubkey, setPubkey] = useState('d5aOEYaCIHaDn6kG0tAg699D7sLgAgkJI5reTMl0o5U='); const [pubkey, setPubkey] = useState('qiQEvyqIf9/e912ZEIZD5CfdqR3iOlpI+NVtRnpHuYc=');
const [participant, setParticipant] = useState<Participant | null>(null); const [participant, setParticipant] = useState<Participant | null>(null);
if (!session){ if (!session){
navigate("/connect-wallet") navigate("/connect-wallet?redirectTo=create-validator");
} }
const msgCreateValidator: MsgCreateValidator = const msgCreateValidator: MsgCreateValidator =
useMemo(() => { useMemo(() => {
return { return {
description: { description: {
moniker: "dockerNode", moniker: "dockerNode2",
identity: "", identity: "",
website: "", website: "",
securityContact: "", securityContact: "",
@ -41,7 +41,7 @@ const CreateValidator = () => {
rate: "100000000000000000", // 0.1 rate: "100000000000000000", // 0.1
}, },
minSelfDelegation: "1", minSelfDelegation: "1",
delegatorAddress: cosmosAddress, delegatorAddress: '',
validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data), validatorAddress: toBech32('laconicvaloper', fromBech32(cosmosAddress).data),
pubkey: encodePubkey({ pubkey: encodePubkey({
type: "tendermint/PubKeyEd25519", type: "tendermint/PubKeyEd25519",
@ -183,38 +183,38 @@ const CreateValidator = () => {
)} )}
</Box> </Box>
{participant && participant.role === "validator" && ( {/* {participant && participant.role === "validator" && ( */}
<> <>
<Typography >Send transaction to chain</Typography> <Typography >Send transaction to chain</Typography>
<Box <Box
sx={{ sx={{
backgroundColor: "lightgray", backgroundColor: "lightgray",
padding: 3, padding: 3,
wordWrap: "break-word", wordWrap: "break-word",
}}
>
<pre style={{ whiteSpace: "pre-wrap", margin: 0 }}>
{JSON.stringify(msgCreateValidator, null, 2)}
</pre>
</Box>
<Box marginTop={1}>
<LoadingButton
variant="contained"
onClick={async () => {
console.log(msgCreateValidatorEncodeObject);
await sendTransaction(msgCreateValidatorEncodeObject);
}} }}
loading={isLoading}
> >
<pre style={{ whiteSpace: "pre-wrap", margin: 0 }}> Send transaction
{JSON.stringify(msgCreateValidator, null, 2)} </LoadingButton>
</pre> </Box>
</Box> </>
<Box marginTop={1}> {/* )} */}
<LoadingButton
variant="contained"
onClick={async () => {
console.log(msgCreateValidatorEncodeObject);
await sendTransaction(msgCreateValidatorEncodeObject);
}}
loading={isLoading}
>
Send transaction
</LoadingButton>
</Box>
</>
)}
</div> </div>
)} )}
</Box> </Box>
) )
} }
export default CreateValidator export default Validator