Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vote credits input bug #224

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/VotingUsage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import clsx from "clsx";
import { useMemo } from "react";

import { useBallot } from "~/contexts/Ballot";
Expand All @@ -22,7 +23,7 @@ export const VotingUsage = (): JSX.Element => {
</div>

<div>
<p className="text-2xl">
<p className={clsx("text-2xl", sum > initialVoiceCredits && "text-red")}>
<b>{sum}</b>
</p>

Expand Down
26 changes: 20 additions & 6 deletions src/features/ballot/components/SubmitBallotButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRouter } from "next/router";
import { useState, useCallback } from "react";
import { useState, useCallback, useMemo } from "react";
import { toast } from "sonner";

import { Button } from "~/components/ui/Button";
Expand All @@ -11,10 +11,15 @@ import { useProjectIdMapping } from "~/features/projects/hooks/useProjects";
export const SubmitBallotButton = (): JSX.Element => {
const router = useRouter();
const [isOpen, setOpen] = useState(false);
const { onVote, isLoading } = useMaci();
const { ballot, publishBallot } = useBallot();
const { onVote, isLoading, initialVoiceCredits } = useMaci();
const { ballot, publishBallot, sumBallot } = useBallot();
const projectIndices = useProjectIdMapping(ballot);

const ableToSubmit = useMemo(
() => sumBallot(ballot.votes) <= initialVoiceCredits,
[sumBallot, ballot, initialVoiceCredits],
);

const onVotingError = useCallback(() => {
toast.error("Voting error");
}, []);
Expand Down Expand Up @@ -44,8 +49,8 @@ export const SubmitBallotButton = (): JSX.Element => {

return (
<>
<Button variant="primary" onClick={handleOpenDialog}>
submit your ballot
<Button variant={ableToSubmit ? "primary" : "disabled"} onClick={handleOpenDialog}>
{ableToSubmit ? "submit your ballot" : "Exceed initial voice credits"}
</Button>

<Dialog
Expand All @@ -54,11 +59,20 @@ export const SubmitBallotButton = (): JSX.Element => {
buttonName="submit"
description="This is not a final submission. Once you submit your ballot, you can change it during the voting period."
isLoading={isLoading}
isOpen={isOpen}
isOpen={ableToSubmit && isOpen}
size="sm"
title="submit your ballot"
onOpenChange={setOpen}
/>

<Dialog
description="You cannot submit this ballot, since the sum of votes exceeds the initial voice credits. Please edit your ballot."
isLoading={isLoading}
isOpen={!ableToSubmit && isOpen}
size="sm"
title="exceed initial voice credits"
onOpenChange={setOpen}
/>
</>
);
};
12 changes: 7 additions & 5 deletions src/features/projects/components/ImpactCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export interface IImpacCategoriesProps {
tags?: string[];
}

export const ImpactCategories = ({ tags = [] }: IImpacCategoriesProps): JSX.Element => (
export const ImpactCategories = ({ tags = undefined }: IImpacCategoriesProps): JSX.Element => (
<div className="no-scrollbar">
<div className="flex gap-1 overflow-x-auto">
{tags.map((key) => (
<Tag key={key} size="sm">
{impactCategories[key as keyof typeof impactCategories].label}
</Tag>
{tags?.map((key) => (
<div key={key}>
{Object.keys(impactCategories).includes(key) ? (
<Tag size="sm">{impactCategories[key as keyof typeof impactCategories].label}</Tag>
) : null}
</div>
))}
</div>
</div>
Expand Down
14 changes: 9 additions & 5 deletions src/features/projects/components/VotingWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Image from "next/image";
import { useMemo, useCallback, useState, type ChangeEvent } from "react";
import { NumericFormat } from "react-number-format";

import { Button } from "~/components/ui/Button";
import { Input } from "~/components/ui/Input";
Expand All @@ -9,7 +10,7 @@ import { useMaci } from "~/contexts/Maci";
import { EButtonState } from "../types";

export const VotingWidget = ({ projectId }: { projectId: string }): JSX.Element => {
const { pollId } = useMaci();
const { pollId, initialVoiceCredits } = useMaci();
const { ballotContains, removeFromBallot, addToBallot } = useBallot();
const projectBallot = useMemo(() => ballotContains(projectId), [ballotContains, projectId]);
const projectIncluded = useMemo(() => !!projectBallot, [projectBallot]);
Expand Down Expand Up @@ -66,11 +67,14 @@ export const VotingWidget = ({ projectId }: { projectId: string }): JSX.Element
)}

<div className="flex items-center justify-center gap-5 rounded-xl border border-gray-200 p-5 dark:border-gray-800">
<Input
<NumericFormat
aria-label="allocation-input"
autoComplete="off"
className="w-auto dark:bg-lightBlack dark:text-white"
placeholder="Add votes here"
type="number"
value={amount}
customInput={Input}
defaultValue={amount}
isAllowed={({ floatValue }) => (floatValue ?? 0) <= initialVoiceCredits}
thousandSeparator=","
onChange={handleInput}
/>

Expand Down
8 changes: 6 additions & 2 deletions src/layouts/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useAccount } from "wagmi";
import { Footer } from "~/components/Footer";
import { createComponent } from "~/components/ui";
import { metadata } from "~/config";
import { useMaci } from "~/contexts/Maci";

const Context = createContext({ eligibilityCheck: false, showBallot: false });

Expand Down Expand Up @@ -52,6 +53,7 @@ const Sidebar = ({ side = undefined, ...props }: { side?: "left" | "right" } & P
export interface LayoutProps {
title?: string;
requireAuth?: boolean;
requireRegistration?: boolean;
eligibilityCheck?: boolean;
showBallot?: boolean;
type?: string;
Expand All @@ -63,6 +65,7 @@ export const BaseLayout = ({
sidebar = undefined,
sidebarComponent = null,
requireAuth = false,
requireRegistration = false,
eligibilityCheck = false,
showBallot = false,
type = undefined,
Expand All @@ -77,12 +80,13 @@ export const BaseLayout = ({
const { theme } = useTheme();
const router = useRouter();
const { address, isConnecting } = useAccount();
const { isRegistered } = useMaci();

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

useEffect(() => {
manageDisplay();
Expand Down
7 changes: 5 additions & 2 deletions src/layouts/DefaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const Layout = ({ children = null, ...props }: ILayoutProps): JSX.Element
const { address } = useAccount();
const appState = useAppState();
const { ballot } = useBallot();
const { isRegistered } = useMaci();

const navLinks = useMemo(() => {
const links = [
Expand All @@ -39,7 +40,9 @@ export const Layout = ({ children = null, ...props }: ILayoutProps): JSX.Element
href: "/ballot/confirmation",
children: "My Ballot",
});
} else {
}

if (appState === EAppState.VOTING && !ballot.published && isRegistered) {
links.push({
href: "/ballot",
children: "My Ballot",
Expand Down Expand Up @@ -69,7 +72,7 @@ export const Layout = ({ children = null, ...props }: ILayoutProps): JSX.Element
}

return links;
}, [ballot, appState, address]);
}, [ballot.published, appState, isRegistered, address]);

return (
<BaseLayout {...props} header={<Header navLinks={navLinks} />}>
Expand Down
11 changes: 7 additions & 4 deletions src/pages/ballot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import clsx from "clsx";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState, useMemo, useCallback } from "react";
Expand All @@ -8,6 +9,7 @@ import { Button } from "~/components/ui/Button";
import { Dialog } from "~/components/ui/Dialog";
import { Form } from "~/components/ui/Form";
import { useBallot } from "~/contexts/Ballot";
import { useMaci } from "~/contexts/Maci";
import { AllocationFormWrapper } from "~/features/ballot/components/AllocationList";
import { BallotSchema } from "~/features/ballot/types";
import { LayoutWithSidebar } from "~/layouts/DefaultLayout";
Expand Down Expand Up @@ -79,8 +81,9 @@ const EmptyBallot = (): JSX.Element => (
const BallotAllocationForm = (): JSX.Element => {
const appState = useAppState();
const { ballot, sumBallot } = useBallot();
const { initialVoiceCredits } = useMaci();

const sum = useMemo(() => formatNumber(sumBallot(ballot.votes)), [ballot, sumBallot]);
const sum = useMemo(() => sumBallot(ballot.votes), [ballot, sumBallot]);

return (
<div className="px-8">
Expand All @@ -99,10 +102,10 @@ const BallotAllocationForm = (): JSX.Element => {
)}
</div>

<div className="flex h-16 items-center justify-end gap-2">
<div className={clsx("flex h-16 items-center justify-end gap-2", sum > initialVoiceCredits && "text-red")}>
<h4>Total votes:</h4>

<p>{sum}</p>
<p>{formatNumber(sum)}</p>
</div>
</div>
</div>
Expand All @@ -125,7 +128,7 @@ const BallotPage = (): JSX.Element => {
}, [sumBallot]);

return (
<LayoutWithSidebar requireAuth showBallot showSubmitButton sidebar="right">
<LayoutWithSidebar requireAuth requireRegistration showBallot showSubmitButton sidebar="right">
<Form defaultValues={ballot} schema={BallotSchema} values={ballot} onSubmit={handleSubmit}>
<BallotAllocationForm />
</Form>
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.