Skip to content

Commit

Permalink
[#1117] change implementation of checkIsWalletConnected
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dyczka committed Jul 26, 2024
1 parent d28b355 commit 93f4210
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/CreateGovernanceAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const CreateGovernanceAction = () => {
});

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
}
}, []);
Expand Down
4 changes: 1 addition & 3 deletions govtool/frontend/src/pages/DRepDirectory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { checkIsWalletConnected } from "@utils";
export const DRepDirectory = () => {
const { t } = useTranslation();

const isConnected = !checkIsWalletConnected();

if (isConnected) {
if (checkIsWalletConnected()) {
return (
<PagePaddingBox
sx={{ display: "flex", flex: 1, flexDirection: "column", py: 2 }}
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const Dashboard = () => {
}, [pathname, divRef]);

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
if (window.location.pathname === PATHS.dashboard) {
navigate(PATHS.home);
} else {
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/EditDRepMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const EditDRepMetadata = () => {
});

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
return;
}
Expand Down
4 changes: 1 addition & 3 deletions govtool/frontend/src/pages/PublicRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { useLocation, useNavigate } from "react-router-dom";
import { checkIsWalletConnected } from "../utils";

export const PublicRoute: FC<{ children: ReactNode }> = ({ children }) => {
// checkIsWalletConnected returns true if wallet is NOT connected
const isConnected = !checkIsWalletConnected();
const navigate = useNavigate();
const { pathname } = useLocation();

if (isConnected && !pathname.startsWith("/connected")) {
if (checkIsWalletConnected() && !pathname.startsWith("/connected")) {
navigate(`/connected${pathname}`);
}

Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/RegisterAsDirectVoter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const RegisterAsDirectVoter = () => {
);

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
}
}, []);
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/RegisterAsdRep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const RegisterAsdRep = () => {
});

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
}
}, []);
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/RetireAsDirectVoter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const RetireAsDirectVoter = () => {
);

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
}
}, []);
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/pages/RetireAsDrep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const RetireAsDrep = () => {
const { isMobile } = useScreenDimension();

useEffect(() => {
if (checkIsWalletConnected()) {
if (!checkIsWalletConnected()) {
navigate(PATHS.home);
}
}, []);
Expand Down
4 changes: 2 additions & 2 deletions govtool/frontend/src/utils/checkIsWalletConnected.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WALLET_LS_KEY, getItemFromLocalStorage } from "@/utils/localStorage";

export const checkIsWalletConnected = () =>
!getItemFromLocalStorage(`${WALLET_LS_KEY}_stake_key`) ||
!getItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
getItemFromLocalStorage(`${WALLET_LS_KEY}_stake_key`) &&
getItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import {
} from "@/utils/localStorage";

describe("checkIsWalletConnected function", () => {
it("returns false when wallet information is present in local storage", () => {
it("returns true when wallet information is present in local storage", () => {
setItemToLocalStorage(`${WALLET_LS_KEY}_name`, "Nami");
setItemToLocalStorage(`${WALLET_LS_KEY}_stake_key`, "teststakekey");
const isConnected = checkIsWalletConnected();

expect(isConnected).toBe(false);
expect(isConnected).toBe(true);
});

it("returns true when wallet information is missing in local storage", () => {
it("returns false when wallet information is missing in local storage", () => {
removeItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
removeItemFromLocalStorage(`${WALLET_LS_KEY}_stake_key`);

const isConnected = checkIsWalletConnected();

expect(isConnected).toBe(true);
expect(isConnected).toBe(false);
});
});

0 comments on commit 93f4210

Please sign in to comment.