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

Add eligible / not eligible to submissions #61

Merged
merged 3 commits into from
Aug 26, 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
85 changes: 85 additions & 0 deletions packages/nextjs/app/admin/_components/SubmissionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const SubmissionCard = ({ submission }: { submission: Submission }) => {
mutationFn: (newVote: { score: number }) =>
postMutationFetcher(`/api/submissions/${submission.id}/votes`, { body: newVote }),
});
const { mutateAsync: postNewEligible } = useMutation({
mutationFn: (newEligible: { eligible: boolean; clear: boolean }) =>
postMutationFetcher(`/api/submissions/${submission.id}/eligible`, { body: newEligible }),
});
const { refresh } = useRouter();

const clientFormAction = async (formData: FormData) => {
Expand Down Expand Up @@ -80,6 +84,36 @@ export const SubmissionCard = ({ submission }: { submission: Submission }) => {
}
};

const setEligible = async (newEligible: boolean) => {
try {
const result = await postNewEligible({ eligible: newEligible, clear: false });

notification.success(result.message);
refresh();
} catch (error: any) {
if (error instanceof Error) {
notification.error(error.message);
return;
}
notification.error("Something went wrong");
}
};

const clearEligible = async () => {
try {
const result = await postNewEligible({ eligible: false, clear: true });

notification.success(result.message);
refresh();
} catch (error: any) {
if (error instanceof Error) {
notification.error(error.message);
return;
}
notification.error("Something went wrong");
}
};

const scoreAvg =
submission.votes.length > 0
? (submission.votes.map(vote => vote.score).reduce((a, b) => a + b, 0) / submission.votes.length).toFixed(2)
Expand All @@ -94,6 +128,57 @@ export const SubmissionCard = ({ submission }: { submission: Submission }) => {
<div className="card-body p-4">
<h2 className="card-title mb-3 xl:text-2xl">{submission.title}</h2>
<div className="flex flex-wrap justify-between items-center gap-4">
<div className="flex items-center mb-4">
<input
type="radio"
id={`eligible_${submission.id}_false`}
name={`eligible_${submission.id}`}
className="radio"
checked={submission.eligible === false}
onChange={() => setEligible(false)}
/>
{submission.eligible === false ? (
<div
className="tooltip"
data-tip={`Set by ${submission.eligibleAdmin} on ${submission.eligibleTimestamp ? getFormattedDateTime(new Date(submission.eligibleTimestamp)) : ""}`}
>
<label className="mr-4 ml-1" htmlFor={`eligible_${submission.id}_false`}>
Not eligible
</label>
</div>
) : (
<label className="mr-4 ml-1" htmlFor={`eligible_${submission.id}_false`}>
Not eligible
</label>
)}
<input
type="radio"
id={`eligible_${submission.id}_true`}
name={`eligible_${submission.id}`}
className="radio"
checked={submission.eligible === true}
onChange={() => setEligible(true)}
/>
{submission.eligible === true ? (
<div
className="tooltip"
data-tip={`Set by ${submission.eligibleAdmin} on ${submission.eligibleTimestamp ? getFormattedDateTime(new Date(submission.eligibleTimestamp)) : ""}`}
>
<label className="mr-4 ml-1" htmlFor={`eligible_${submission.id}_true`}>
Eligible
</label>
</div>
) : (
<label className="mr-4 ml-1" htmlFor={`eligible_${submission.id}_true`}>
Eligible
</label>
)}
{submission.eligible !== undefined && (
<button className="cursor-pointer underline text-sm ml-3" onClick={clearEligible}>
Clear
</button>
)}
</div>
<div className="mt-1 flex shrink-0 gap-3">
{submission.linkToRepository && (
<a href={submission.linkToRepository} className="inline-block" target="_blank">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { clearEligible, setEligible } from "~~/services/database/repositories/submissions";
import { authOptions } from "~~/utils/auth";

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

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

const { eligible, clear } = (await req.json()) as { eligible: boolean; clear: boolean };

if (!session.user.address) {
return NextResponse.json({ error: "Invalid admin address" }, { status: 400 });
}

const builderId = session.user.address;

if (clear) {
await clearEligible(submissionId);

return NextResponse.json({ message: "Eligible deleted!" }, { status: 200 });
} else {
await setEligible(submissionId, eligible, builderId);

return NextResponse.json({ message: `Eligible set to ${eligible}` }, { status: 201 });
}
} catch (e) {
console.error(e);
return NextResponse.json({ error: "Error processing form" }, { status: 500 });
}
}
5 changes: 4 additions & 1 deletion packages/nextjs/services/database/config/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relations, sql } from "drizzle-orm";
import { integer, pgTable, primaryKey, serial, text, timestamp, varchar } from "drizzle-orm/pg-core";
import { boolean, integer, pgTable, primaryKey, serial, text, timestamp, varchar } from "drizzle-orm/pg-core";

export const builders = pgTable("builders", {
id: varchar("id", { length: 256 }).primaryKey(),
Expand All @@ -20,6 +20,9 @@ export const submissions = pgTable("submissions", {
builder: varchar("builder_id", { length: 256 })
.references(() => builders.id)
.notNull(),
eligible: boolean("eligible"),
eligibleTimestamp: timestamp("eligible_timestamp"),
eligibleAdmin: varchar("eligible_admin", { length: 256 }),
});

export const comments = pgTable("comments", {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "submissions" ADD COLUMN "eligible" boolean;--> statement-breakpoint
ALTER TABLE "submissions" ADD COLUMN "eligible_timestamp" timestamp;--> statement-breakpoint
ALTER TABLE "submissions" ADD COLUMN "eligible_admin" varchar(256);
Loading
Loading