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

Winners tag and disable voting #75

Merged
merged 3 commits into from
Sep 6, 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
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;
Loading