Skip to content

Commit

Permalink
isConnected and address
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonmanRolls committed Jan 18, 2024
1 parent 878aedc commit 28a76b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
47 changes: 19 additions & 28 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { useQuery } from "@apollo/client";
import { gql } from "graphql-tag";
import React, { useEffect } from "react";
import {
createWeb3Modal,
defaultConfig,
useWeb3ModalAccount,
} from "@web3modal/ethers5/react";
import {
BrowserRouter as Router,
Route,
Expand All @@ -23,7 +28,8 @@ import Home from "./pages/Home";
import { useGetDelegates, useQueryString } from "./utils/hooks";
import { hasClaimed } from "./utils/token";
import { initWeb3Read } from "./web3modal";
import { createWeb3Modal, defaultConfig } from "@web3modal/ethers5/react";
import { addressDetails, ep2AddressDetails } from "./apollo";
import { getClaimData } from "./utils/utils";

const PROJECT_ID = "02f438d1701ea8029113972850066224";

Expand Down Expand Up @@ -80,14 +86,13 @@ const PRIVATE_ROUTE_QUERY = gql`
query privateRouteQuery @client {
addressDetails
ep2AddressDetails
address
isConnected
}
`;

function PrivateRoute({ component: Component, type = "mainnet", ...rest }) {
const { data } = useQuery(PRIVATE_ROUTE_QUERY);
const history = useHistory();
const { address, isConnected } = useWeb3ModalAccount();

useEffect(() => {
let finalAddressDetails =
Expand All @@ -101,7 +106,7 @@ function PrivateRoute({ component: Component, type = "mainnet", ...rest }) {
history.push("/dashboard");
return;
}
const isClaimed = await hasClaimed(data.address, type);
const isClaimed = await hasClaimed(address, type);
if (isClaimed) {
history.push("/dashboard");
}
Expand All @@ -111,34 +116,26 @@ function PrivateRoute({ component: Component, type = "mainnet", ...rest }) {
}
};

if (
data.isConnected &&
data.address &&
data.addressDetails.eligible !== undefined
) {
if (isConnected && address && data.addressDetails.eligible !== undefined) {
run();
}

if (!data.address && data.isConnected) {
if (!address && isConnected) {
history.push("/");
}
}, [
data.address,
data.isConnected,
data.addressDetails,
data.ep2AddressDetails,
]);
}, [address, isConnected, data.addressDetails, data.ep2AddressDetails]);

return <Route {...rest} render={(props) => <Component {...props} />} />;
}

function ConnectedRoute({ component: Component, ...rest }) {
const { data } = useQuery(PRIVATE_ROUTE_QUERY);
const { address, isConnected } = useWeb3ModalAccount();

const history = useHistory();
useEffect(() => {
function run() {
try {
if (!data.address) {
if (!address) {
history.push("/");
}
} catch (error) {
Expand All @@ -147,23 +144,17 @@ function ConnectedRoute({ component: Component, ...rest }) {
}
}

if (data.isConnected && data.address) {
if (isConnected && address) {
run();
}
}, [data.address, data.isConnected]);
}, [address, isConnected]);
return <Route {...rest} render={(props) => <Component {...props} />} />;
}

function App() {
const query = useQueryString();
const {
data: { address, isConnected },
} = useQuery(gql`
query getAddress @client {
address
isConnected
}
`);
const { address, isConnected } = useWeb3ModalAccount();

useEffect(() => {
const delegate = query.get("delegate");
if (delegate && address) {
Expand Down
5 changes: 1 addition & 4 deletions src/components/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ const Profile = ({ address, size, hasDropdown }) => {

return (
<DropdownWrapper
dropdownItems={[
{ name: "Dashboard", link: "/dashboard" },
{ name: "Disconnect", type: "deny", action: disconnect },
]}
dropdownItems={[{ name: "Disconnect", type: "deny", action: disconnect }]}
isOpen={navOpen}
setIsOpen={setNavOpen}
testId="profile-dropdown"
Expand Down

0 comments on commit 28a76b0

Please sign in to comment.