From 40da76ebb299916dfe4dd7c18c306426745dd20b Mon Sep 17 00:00:00 2001 From: Damian Date: Thu, 5 Sep 2024 23:48:08 -0300 Subject: [PATCH] Winners tag --- .../_components/SubmissionCard.tsx | 9 ++++++-- packages/nextjs/app/submissions/page.tsx | 21 ++++++++++++++++--- packages/nextjs/scaffold.config.ts | 6 ++++++ .../database/repositories/submissions.ts | 3 +++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/app/submissions/_components/SubmissionCard.tsx b/packages/nextjs/app/submissions/_components/SubmissionCard.tsx index 44e0f80..efc73f1 100644 --- a/packages/nextjs/app/submissions/_components/SubmissionCard.tsx +++ b/packages/nextjs/app/submissions/_components/SubmissionCard.tsx @@ -1,10 +1,15 @@ import { Address } from "~~/components/scaffold-eth"; -import { Submission } from "~~/services/database/repositories/submissions"; +import { SubmissionWithWinnerTag } from "~~/services/database/repositories/submissions"; -export const SubmissionCard = ({ submission }: { submission: Submission }) => { +export const SubmissionCard = ({ submission }: { submission: SubmissionWithWinnerTag }) => { return (
+ {submission.winnerTag && ( +
+ {submission.winnerTag} +
+ )}

{submission.title}

diff --git a/packages/nextjs/app/submissions/page.tsx b/packages/nextjs/app/submissions/page.tsx index 4b8547d..cbf2f84 100644 --- a/packages/nextjs/app/submissions/page.tsx +++ b/packages/nextjs/app/submissions/page.tsx @@ -1,6 +1,7 @@ import { SubmissionCard } from "./_components/SubmissionCard"; import type { NextPage } from "next"; -import { getAllSubmissions } from "~~/services/database/repositories/submissions"; +import scaffoldConfig from "~~/scaffold.config"; +import { SubmissionWithWinnerTag, getAllSubmissions } from "~~/services/database/repositories/submissions"; import { getMetadata } from "~~/utils/scaffold-eth/getMetadata"; export const metadata = getMetadata({ @@ -10,16 +11,30 @@ export const metadata = getMetadata({ const Submissions: NextPage = async () => { const submissions = await getAllSubmissions(); + const { winnersThreshold, runnersUpThreshold } = scaffoldConfig; + + const submissionsWithAvgScore: SubmissionWithWinnerTag[] = submissions + .map(submission => { + const avgScore = + submission.votes.length > 0 + ? submission.votes.map(vote => vote.score).reduce((a, b) => a + b, 0) / submission.votes.length + : 0; + + const winnerTag = avgScore >= winnersThreshold ? "Winner" : avgScore >= runnersUpThreshold ? "Runner Up" : null; + + return { ...submission, avgScore, winnerTag }; + }) + .sort((a, b) => b.avgScore - a.avgScore); return (
-
+
{submissions.length === 0 && (
No submissions yet.
)} - {submissions.map(submission => { + {submissionsWithAvgScore.map((submission: SubmissionWithWinnerTag) => { return ; })}
diff --git a/packages/nextjs/scaffold.config.ts b/packages/nextjs/scaffold.config.ts index ffe76bf..ec8f25b 100644 --- a/packages/nextjs/scaffold.config.ts +++ b/packages/nextjs/scaffold.config.ts @@ -7,6 +7,8 @@ export type ScaffoldConfig = { walletConnectProjectId: string; onlyLocalBurnerWallet: boolean; votingEnabled: boolean; + winnersThreshold: number; + runnersUpThreshold: number; }; const scaffoldConfig = { @@ -34,6 +36,10 @@ const scaffoldConfig = { // Enable voting on submissions votingEnabled: false, + + // Score threshold for winners and runners up + winnersThreshold: 7, + runnersUpThreshold: 6, } as const satisfies ScaffoldConfig; export default scaffoldConfig; diff --git a/packages/nextjs/services/database/repositories/submissions.ts b/packages/nextjs/services/database/repositories/submissions.ts index 145fd27..117fa26 100644 --- a/packages/nextjs/services/database/repositories/submissions.ts +++ b/packages/nextjs/services/database/repositories/submissions.ts @@ -9,6 +9,9 @@ export type Submission = InferSelectModel & { comments: Comm export type SubmissionWithAvg = Submission & { avgScore: number; }; +export type SubmissionWithWinnerTag = Submission & { + winnerTag: string | null; +}; export async function getAllSubmissions() { return await db.query.submissions.findMany({