Skip to content

Commit

Permalink
Merge pull request #544 from privacy-scaling-explorations/fix/ballot-…
Browse files Browse the repository at this point in the history
…refresh

fix: ballot page refresh redirect to homepage
  • Loading branch information
ctrlc03 authored Dec 11, 2024
2 parents 52a14bf + 6726064 commit 6d9d6d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
7 changes: 2 additions & 5 deletions packages/interface/src/layouts/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useAccount } from "wagmi";
import { Footer } from "~/components/Footer";
import { createComponent } from "~/components/ui";
import { metadata } from "~/config";
import { useMaci } from "~/contexts/Maci";

import type { IBaseLayoutProps } from "./types";

Expand Down Expand Up @@ -50,7 +49,6 @@ export const BaseLayout = ({
sidebar = undefined,
sidebarComponent = null,
requireAuth = false,
requireRegistration = false,
eligibilityCheck = false,
showBallot = false,
type = undefined,
Expand All @@ -59,13 +57,12 @@ export const BaseLayout = ({
const { theme } = useTheme();
const router = useRouter();
const { address, isConnecting } = useAccount();
const { isRegistered } = useMaci();

const manageDisplay = useCallback(() => {
if ((requireAuth && !address && !isConnecting) || (requireRegistration && !isRegistered)) {
if (requireAuth && !address && !isConnecting) {
router.push("/");
}
}, [requireAuth, address, isConnecting, requireRegistration, isRegistered, router]);
}, [requireAuth, address, isConnecting, router]);

useEffect(() => {
manageDisplay();
Expand Down
1 change: 0 additions & 1 deletion packages/interface/src/layouts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { ReactNode, PropsWithChildren } from "react";
export interface LayoutProps {
title?: string;
requireAuth?: boolean;
requireRegistration?: boolean;
eligibilityCheck?: boolean;
showBallot?: boolean;
type?: string;
Expand Down
21 changes: 8 additions & 13 deletions packages/interface/src/pages/rounds/[pollId]/ballot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import clsx from "clsx";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState, useMemo, useCallback } from "react";
import { useCallback, useState, useMemo } from "react";
import { useFormContext } from "react-hook-form";
import { useAccount } from "wagmi";

import { Button } from "~/components/ui/Button";
import { Dialog } from "~/components/ui/Dialog";
Expand Down Expand Up @@ -154,28 +152,21 @@ interface IBallotPageProps {
}

const BallotPage = ({ pollId }: IBallotPageProps): JSX.Element => {
const { address, isConnecting } = useAccount();
const { getBallot, sumBallot } = useBallot();
const { getRoundByPollId } = useRound();
const router = useRouter();
const { isRegistered } = useMaci();
const roundState = useRoundState({ pollId });

const round = useMemo(() => getRoundByPollId(pollId), [pollId, getRoundByPollId]);
const ballot = useMemo(() => getBallot(pollId), [round?.pollId, getBallot]);

useEffect(() => {
if (!address && !isConnecting) {
router.push("/");
}
}, [address, isConnecting, router]);

const handleSubmit = useCallback(() => {
sumBallot();
}, [sumBallot]);

return (
<LayoutWithSidebar requireAuth requireRegistration showBallot showSubmitButton pollId={pollId} sidebar="right">
{roundState === ERoundState.VOTING && (
<LayoutWithSidebar requireAuth showBallot showSubmitButton pollId={pollId} sidebar="right">
{roundState === ERoundState.VOTING && isRegistered && (
<Form defaultValues={ballot} schema={BallotSchema} values={ballot} onSubmit={handleSubmit}>
<BallotAllocationForm mode={round!.mode} pollId={pollId} />
</Form>
Expand All @@ -184,6 +175,10 @@ const BallotPage = ({ pollId }: IBallotPageProps): JSX.Element => {
{roundState !== ERoundState.VOTING && (
<div className="dark:text-white">You can only vote during the voting period.</div>
)}

{!isRegistered && (
<div className="dark:text-white">You must sign up to access the full information on this page.</div>
)}
</LayoutWithSidebar>
);
};
Expand Down

0 comments on commit 6d9d6d9

Please sign in to comment.