Skip to content

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

Can see submissions now (#134)

Sean Boult42d33ea75122
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 8cf3f4f4..8a7aff15 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -136,6 +136,7 @@ model Vote {
@@index([solutionId])
}
+/// This is a public solution created by a user for a challenge
model SharedSolution {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
@@ -152,6 +153,7 @@ model SharedSolution {
@@index([challengeId])
}
+/// This is a submission of a challenge
model Solution {
id Int @id @default(autoincrement())
code String @db.MediumText
diff --git a/prisma/seed.ts b/prisma/seed.ts
index f74aeb95..26f57484 100644
--- a/prisma/seed.ts
+++ b/prisma/seed.ts
@@ -40,6 +40,20 @@ try {
name: 'g',
challenge: challenges(),
+ solution: {
+ create: {
+ code: 'const a: string = "hello world"',
+ isSuccessful: true,
+ challengeId: 1,
+ },
+ },
+ sharedSolution: {
+ create: {
+ challengeId: 1,
+ title: 'This is a solution',
+ description: 'this is a description',
+ },
+ },
},
});
diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsx
index 10bede37..93d27d4d 100644
--- a/src/components/challenge/index.tsx
+++ b/src/components/challenge/index.tsx
@@ -63,6 +63,9 @@ async function getChallenge(id: string, session: Session | null) {
where: {
challengeId: +id,
},
+ include: {
+ user: true
+ }
},
},
});
diff --git a/src/components/challenge/solutions/index.tsx b/src/components/challenge/solutions/index.tsx
index a7a50157..607363ef 100644
--- a/src/components/challenge/solutions/index.tsx
+++ b/src/components/challenge/solutions/index.tsx
@@ -6,6 +6,7 @@ import { Button } from '~/components/ui/button';
import type { Challenge } from '..';
import { SolutionEditor } from './solution-editor';
import { useState } from 'react';
+import { SharedSolution } from '@prisma/client';
interface Props {
challenge: NonNullable<Challenge>;
@@ -14,6 +15,8 @@ export function Solutions({ challenge }: Props) {
const hasSolution = challenge.solution?.length > 0;
const [openSolutionEditor, setOpenSolutionEditor] = useState(false);
+ const sharedSolution = challenge.sharedSolution;
+
const handleClick = () => {
setOpenSolutionEditor(true);
};
@@ -41,29 +44,28 @@ export function Solutions({ challenge }: Props) {
<Plus className="mr-2 h-4 w-4" /> Solution
</Button>
</div>
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
- <SolutionRow />
+
+ {sharedSolution.map((solution) => (
+ <SolutionRow key={solution.id} solution={solution} />
+ ))}
</>
)}
</div>
);
}
-// eslint-disable-next-line @typescript-eslint/no-unused-vars
-function SolutionRow() {
+function SolutionRow({
+ solution,
+}: {
+ solution: NonNullable<NonNullable<Challenge>['sharedSolution'][0]>;
+}) {
const date = new Date();
return (
<div className="group flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-neutral-700">
- <h3 className="font-bold">🔥🔥Some | LeetCodeLooking✅ | Title🔥🔥</h3>
+ <h3 className="font-bold">{solution.title}</h3>
<div className="flex items-center gap-2 text-sm text-neutral-500">
<div className="rounded-full bg-neutral-200 px-2 py-1 text-xs font-bold text-neutral-600 dark:bg-neutral-700 dark:text-neutral-400">
- @&nbsp;username
+ {solution.user?.name}
</div>
<div>{date.toLocaleString()}</div>
</div>
diff --git a/src/components/challenge/solutions/solution-editor.tsx b/src/components/challenge/solutions/solution-editor.tsx
index 4fa4de29..3a50481e 100644
--- a/src/components/challenge/solutions/solution-editor.tsx
+++ b/src/components/challenge/solutions/solution-editor.tsx
@@ -10,6 +10,7 @@ import { postSolution } from './post-solution.action';
import type { Challenge } from '..';
import { useSession } from 'next-auth/react';
import { useTheme } from 'next-themes';
+import { useRouter } from 'next/navigation';
const getDefaultMarkdown = (solution: string) => `
## Thoughts
@@ -39,6 +40,7 @@ interface Props {
export function SolutionEditor({ setOpen, challenge }: Props) {
const session = useSession();
+ const router = useRouter();
const form = useForm<FormSchema>({
resolver: zodResolver(formSchema),
defaultValues: {
@@ -55,6 +57,8 @@ export function SolutionEditor({ setOpen, challenge }: Props) {
title: data.title,
userId: session?.data?.user?.id as string,
});
+
+ router.refresh();
toast({
variant: 'success',
title: 'Your solution has been posted!',
@@ -67,6 +71,7 @@ export function SolutionEditor({ setOpen, challenge }: Props) {
} finally {
setOpen(false);
}
+
};
const { theme } = useTheme();