Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

basic submissions tab (#115)

bautistaaad924b1eda116
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 6a8156db..0762ff6d 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -137,6 +137,7 @@ model Vote {
model SharedSolution {
id Int @id @default(autoincrement())
+ createdAt DateTime @default(now())
description String @db.MediumText
solution Solution @relation(fields: [solutionId], references: [id])
solutionId Int
@@ -155,6 +156,8 @@ model SharedSolution {
model Solution {
id Int @id @default(autoincrement())
code String @db.MediumText
+ createdAt DateTime @default(now())
+ isSuccessful Boolean
user User @relation(fields: [userId], references: [id])
userId String
challenge Challenge @relation(fields: [challengeId], references: [id])
@@ -162,7 +165,6 @@ model Solution {
Vote Vote[]
SharedSolution SharedSolution[]
- @@unique([challengeId, userId])
@@index([userId])
@@index([challengeId])
}
diff --git a/src/components/challenge/description-panel.tsx b/src/components/challenge/description-panel.tsx
index e2fdf7be..1cc58c9a 100644
--- a/src/components/challenge/description-panel.tsx
+++ b/src/components/challenge/description-panel.tsx
@@ -34,6 +34,7 @@ import { TypographyLarge } from '../ui/typography/large';
import { addReport } from './report.action';
import { toast } from '../ui/use-toast';
import { Markdown } from '../ui/markdown';
+import { Submissions } from './submissions';
interface Props {
challenge: NonNullable<Challenge>;
@@ -124,7 +125,7 @@ export function DescriptionPanel({ challenge }: Props) {
return (
<>
<Tabs defaultValue="description" className="w-full">
- <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-2 rounded-none border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">
+ <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-3 rounded-none border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">
<TabsTrigger
value="description"
className="rounded-md rounded-tl-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"
@@ -137,6 +138,12 @@ export function DescriptionPanel({ challenge }: Props) {
>
Solutions
</TabsTrigger>
+ <TabsTrigger
+ value="submissions"
+ className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"
+ >
+ Submissions
+ </TabsTrigger>
</TabsList>
<TabsContent value="description" className="mt-0 dark:bg-muted">
<div className="h-full px-4 py-3">
@@ -371,6 +378,9 @@ export function DescriptionPanel({ challenge }: Props) {
<TabsContent value="solutions" className="mt-0 dark:bg-muted">
<Solutions challenge={challenge} />
</TabsContent>
+ <TabsContent value="submissions" className="mt-0 dark:bg-muted">
+ <Submissions challenge={challenge} />
+ </TabsContent>
</Tabs>
</>
);
diff --git a/src/components/challenge/editor/index.tsx b/src/components/challenge/editor/index.tsx
index 7e6b0fd9..4b08f8fd 100644
--- a/src/components/challenge/editor/index.tsx
+++ b/src/components/challenge/editor/index.tsx
@@ -20,7 +20,7 @@ import { ToastAction } from '~/components/ui/toast';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '~/components/ui/tooltip';
import { useToast } from '~/components/ui/use-toast';
import type { Challenge } from '..';
-import { saveSubmission } from '../save-submission';
+import { saveSubmission } from '../save-submission.action';
import { SettingsForm } from '../settings-form';
import { useEditorSettingsStore } from '../settings-store';
import { USER_CODE_START, USER_CODE_START_REGEX } from './constants';
@@ -95,9 +95,7 @@ export const CodePanel = (props: Props) => {
return props.challenge.prompt;
}
- const [appendSolutionToThis, separator] = (props.challenge.prompt).split(
- USER_CODE_START_REGEX,
- );
+ const [appendSolutionToThis, separator] = props.challenge.prompt.split(USER_CODE_START_REGEX);
const parsedUserSolution = usersExistingSolution?.code;
return `${appendSolutionToThis ?? ''}${separator ?? ''}${parsedUserSolution}`;
@@ -128,6 +126,14 @@ export const CodePanel = (props: Props) => {
const hasErrors = tsErrors.some((e) => e.length);
+ await saveSubmission(
+ props.challenge.id,
+ session?.user?.id as string,
+ solution ?? '',
+ !hasErrors,
+ );
+ router.refresh();
+
if (hasErrors) {
toast({
variant: 'destructive',
@@ -135,12 +141,6 @@ export const CodePanel = (props: Props) => {
action: <ToastAction altText="Try again">Try again</ToastAction>,
});
} else {
- await saveSubmission(
- props.challenge.id,
- session?.user?.id as string,
- solution ?? '',
- );
- router.refresh();
toast({
variant: 'success',
title: 'Good job!',
diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsx
index 694d6087..9588d24a 100644
--- a/src/components/challenge/index.tsx
+++ b/src/components/challenge/index.tsx
@@ -52,6 +52,12 @@ async function getChallenge(id: string, session: Session | null) {
userId: session?.user.id,
challengeId: +id,
},
+ orderBy: [
+ {
+ createdAt: 'desc',
+ },
+ ],
+ take: 10,
},
SharedSolution: {
where: {
diff --git a/src/components/challenge/save-submission.action.ts b/src/components/challenge/save-submission.action.ts
new file mode 100644
index 00000000..74a6e4aa
--- /dev/null
+++ b/src/components/challenge/save-submission.action.ts
@@ -0,0 +1,18 @@
+'use server';
+import { prisma } from '~/server/db';
+
+export async function saveSubmission(
+ challengeId: number,
+ userId: string,
+ code: string,
+ isSuccessful: boolean,
+) {
+ await prisma.solution.create({
+ data: {
+ challengeId,
+ userId,
+ code,
+ isSuccessful,
+ },
+ });
+}
diff --git a/src/components/challenge/save-submission.ts b/src/components/challenge/save-submission.ts
deleted file mode 100644
index 09e2d10b..00000000
--- a/src/components/challenge/save-submission.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-'use server';
-import { prisma } from '~/server/db';
-
-export async function saveSubmission(challengeId: number, userId: string, code: string) {
- await prisma.solution.upsert({
- where: {
- challengeId_userId: {
- challengeId,
- userId,
- },
- },
- update: {
- code,
- },
- create: {
- challengeId,
- userId,
- code,
- },
- });
-}
diff --git a/src/components/challenge/submissions.tsx b/src/components/challenge/submissions.tsx
new file mode 100644
index 00000000..a3d09e5e
--- /dev/null
+++ b/src/components/challenge/submissions.tsx
@@ -0,0 +1,82 @@
+'use client';
+
+import type { Solution } from '@prisma/client';
+import type { Challenge } from '.';
+import { getRelativeTime } from '~/utils/relativeTime';
+import clsx from 'clsx';
+import {
+ Select,
+ SelectTrigger,
+ SelectValue,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+} from '~/components/ui/select';
+import { useMemo, useState } from 'react';
+
+interface Props {
+ challenge: NonNullable<Challenge>;
+}
+
+type Status = 'all' | 'accepted' | 'rejected';
+export function Submissions({ challenge }: Props) {
+ const [selectedStatus, setSelectStatus] = useState<Status>('all');
+ const [submissions] = useState(challenge.Solution);
+
+ const filteredSubmissions = useMemo(() => {
+ const predicate = (submission: Solution) => {
+ if (selectedStatus === 'all') return true;
+ if (selectedStatus === 'accepted') return submission.isSuccessful;
+ return !submission.isSuccessful;
+ };
+ return submissions.filter(predicate);
+ }, [selectedStatus, submissions]);
+ return (
+ <div>
+ <div className="p-4">
+ <Select value={selectedStatus} onValueChange={(e) => setSelectStatus(e as Status)}>
+ <SelectTrigger className="w-[180px]">
+ <SelectValue placeholder="Select a fruit" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectGroup>
+ <SelectItem value="all">All Statuses</SelectItem>
+ <SelectItem value="accepted">Accepted</SelectItem>
+ <SelectItem value="rejected">Rejected</SelectItem>
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ </div>
+ <div>
+ <ul className="relative flex flex-col">
+ {filteredSubmissions.map((submission) => {
+ return <SubmissionRow submission={submission} key={submission.id} />;
+ })}
+ </ul>
+ </div>
+ </div>
+ );
+}
+
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
+function SubmissionRow({ submission }: { submission: Solution }) {
+ const handleClick = () => {
+ console.log('navigate to some route and overlay submission on top of editor');
+ };
+ return (
+ <li
+ className="p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-neutral-700"
+ onClick={handleClick}
+ >
+ <div
+ className={clsx({
+ ' text-emerald-600 dark:text-emerald-400': submission.isSuccessful,
+ ' text-rose-600 dark:text-rose-400': !submission.isSuccessful,
+ })}
+ >
+ {submission.isSuccessful ? 'Accepted' : 'Rejected'}
+ </div>
+ <div>{getRelativeTime(submission.createdAt)}</div>
+ </li>
+ );
+}
diff --git a/src/components/explore/explore-card.tsx b/src/components/explore/explore-card.tsx
index be454b49..482d442a 100644
--- a/src/components/explore/explore-card.tsx
+++ b/src/components/explore/explore-card.tsx
@@ -6,7 +6,7 @@ import { type Challenge } from '.';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../ui/card';
import { DifficultyBadge } from './difficulty-badge';
import { Markdown } from '../ui/markdown';
-import getRelativeTime from '~/utils/relativeTime';
+import { getRelativeTime } from '~/utils/relativeTime';
interface Props {
challenge: Pick<
diff --git a/src/utils/relativeTime.ts b/src/utils/relativeTime.ts
index 1606fd22..c9e7e0b2 100644
--- a/src/utils/relativeTime.ts
+++ b/src/utils/relativeTime.ts
@@ -13,7 +13,7 @@ const rtf = new Intl.RelativeTimeFormat('en', {
style: 'short',
});
-const getRelativeTime = (date: Date) => {
+export const getRelativeTime = (date: Date) => {
const elapsed = date.getTime() - Date.now();
for (const [unit, ms] of Object.entries(units)) {
@@ -23,4 +23,3 @@ const getRelativeTime = (date: Date) => {
}
};
-export default getRelativeTime;