Skip to content

Commit

Permalink
Disable voting (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianmarti authored Sep 6, 2024
1 parent 00a0a3b commit 2163497
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/nextjs/app/admin/_components/SubmissionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { useMutation } from "@tanstack/react-query";
import clsx from "clsx";
import { useAccount } from "wagmi";
import { Address } from "~~/components/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { SubmissionWithAvg } from "~~/services/database/repositories/submissions";
import { postMutationFetcher } from "~~/utils/react-query";
import { notification } from "~~/utils/scaffold-eth";

export const SubmissionCard = ({ submission }: { submission: SubmissionWithAvg }) => {
const { votingEnabled } = scaffoldConfig;
const { address: connectedAddress } = useAccount();

const { mutateAsync: postNewVote, isPending: isVotePending } = useMutation({
Expand All @@ -23,6 +25,11 @@ export const SubmissionCard = ({ submission }: { submission: SubmissionWithAvg }

const vote = async (newScore: number) => {
try {
if (!votingEnabled) {
notification.error("Voting is disabled");
return;
}

if (newScore < 0 || newScore > 10) {
notification.error("Wrong score");
return;
Expand Down
13 changes: 12 additions & 1 deletion packages/nextjs/app/admin/_components/Submissions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { SubmissionTabs } from "./SubmissionTabs";
import scaffoldConfig from "~~/scaffold.config";
import { getAllSubmissions } from "~~/services/database/repositories/submissions";

export const Submissions = async () => {
const submissions = await getAllSubmissions();
return <SubmissionTabs submissions={submissions} />;
const { votingEnabled } = scaffoldConfig;
return (
<>
{!votingEnabled && (
<div className="max-w-7xl container mx-auto px-6 mb-6">
<div className="alert alert-warning">Voting period ended</div>
</div>
)}
<SubmissionTabs submissions={submissions} />
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import scaffoldConfig from "~~/scaffold.config";
import { createOrUpdateVote, deleteVote } from "~~/services/database/repositories/votes";
import { authOptions } from "~~/utils/auth";

export async function POST(req: NextRequest, { params }: { params: { submissionId: number } }) {
const { votingEnabled } = scaffoldConfig;
try {
const session = await getServerSession(authOptions);

if (session?.user.role !== "admin") {
return NextResponse.json({ error: "Only admins can vote" }, { status: 401 });
}

if (!votingEnabled) {
return NextResponse.json({ error: "Voting is disabled" }, { status: 400 });
}

const { submissionId } = params;

const { score } = (await req.json()) as { score: string };
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type ScaffoldConfig = {
alchemyApiKey: string;
walletConnectProjectId: string;
onlyLocalBurnerWallet: boolean;
votingEnabled: boolean;
};

const scaffoldConfig = {
Expand All @@ -30,6 +31,9 @@ const scaffoldConfig = {

// Only show the Burner Wallet when running on hardhat network
onlyLocalBurnerWallet: true,

// Enable voting on submissions
votingEnabled: false,
} as const satisfies ScaffoldConfig;

export default scaffoldConfig;

0 comments on commit 2163497

Please sign in to comment.