From f39bbf76e7ad29634d7288be718458e1da8da5f8 Mon Sep 17 00:00:00 2001 From: cfngc4594 Date: Thu, 17 Apr 2025 09:31:04 +0800 Subject: [PATCH] feat(submissions): add login requirement for submissions page - Add SubmissionLoginButton component for unauthenticated users - Modify submissions page to check auth session - Filter submissions by current user when logged in --- .../(app)/problems/[id]/@Submissions/page.tsx | 13 +++++++ .../playground/problem/description/footer.tsx | 2 +- .../playground/problem/solution/footer.tsx | 2 +- src/components/submission-login-button.tsx | 36 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/components/submission-login-button.tsx diff --git a/src/app/(app)/problems/[id]/@Submissions/page.tsx b/src/app/(app)/problems/[id]/@Submissions/page.tsx index c949d4b..9028af6 100644 --- a/src/app/(app)/problems/[id]/@Submissions/page.tsx +++ b/src/app/(app)/problems/[id]/@Submissions/page.tsx @@ -1,8 +1,10 @@ import prisma from "@/lib/prisma"; +import { auth } from "@/lib/auth"; import { notFound } from "next/navigation"; import { getUserLocale } from "@/i18n/locale"; import SubmissionsTable from "@/components/submissions-table"; import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; +import SubmissionLoginButton from "@/components/submission-login-button"; interface SubmissionsPageProps { params: Promise<{ id: string }>; @@ -10,15 +12,26 @@ interface SubmissionsPageProps { export default async function SubmissionsPage({ params }: SubmissionsPageProps) { const { id } = await params; + const session = await auth(); if (!id) { return notFound(); } + if (!session?.user?.id) { + return ( + + ) + } + + const problem = await prisma.problem.findUnique({ where: { id }, select: { submissions: { + where: { + userId: session.user.id, + }, include: { testcaseResults: { include: { diff --git a/src/components/features/playground/problem/description/footer.tsx b/src/components/features/playground/problem/description/footer.tsx index dfcab32..0b8b6ca 100644 --- a/src/components/features/playground/problem/description/footer.tsx +++ b/src/components/features/playground/problem/description/footer.tsx @@ -16,7 +16,7 @@ export default function ProblemDescriptionFooter({ className={cn("h-9 flex flex-none items-center bg-muted px-3 py-2", className)} >
- Description of {title} + {title}
); diff --git a/src/components/features/playground/problem/solution/footer.tsx b/src/components/features/playground/problem/solution/footer.tsx index 69a21d0..60e6832 100644 --- a/src/components/features/playground/problem/solution/footer.tsx +++ b/src/components/features/playground/problem/solution/footer.tsx @@ -16,7 +16,7 @@ export default function ProblemSolutionFooter({ className={cn("h-9 flex flex-none items-center bg-muted px-3 py-2", className)} >
- Solution of {title} + {title}
); diff --git a/src/components/submission-login-button.tsx b/src/components/submission-login-button.tsx new file mode 100644 index 0000000..d504c7f --- /dev/null +++ b/src/components/submission-login-button.tsx @@ -0,0 +1,36 @@ +"use client"; + +import { CodeXmlIcon } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; + +export default function SubmissionLoginButton() { + const router = useRouter(); + const pathname = usePathname(); + const searchParams = useSearchParams(); + + const handleLogIn = () => { + const params = new URLSearchParams(searchParams.toString()); + params.set("redirectTo", pathname); + router.push(`/sign-in?${params.toString()}`); + }; + + return ( +
+
+
+

+ View your Submission records here +

+ +
+ ); +}