Skip to content

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

Order and add vote/comment count (#135)

Sean Boult16efcb831274
diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsx
index 93d27d4d..a72bfe67 100644
--- a/src/components/challenge/index.tsx
+++ b/src/components/challenge/index.tsx
@@ -63,9 +63,18 @@ async function getChallenge(id: string, session: Session | null) {
where: {
challengeId: +id,
},
+ orderBy: [
+ {
+ createdAt: 'desc',
+ },
+ ],
+ take: 5,
include: {
- user: true
- }
+ user: true,
+ _count: {
+ select: { vote: true, solutionComment: true },
+ },
+ },
},
},
});
diff --git a/src/components/challenge/solutions/index.tsx b/src/components/challenge/solutions/index.tsx
index 607363ef..266a742c 100644
--- a/src/components/challenge/solutions/index.tsx
+++ b/src/components/challenge/solutions/index.tsx
@@ -7,6 +7,7 @@ import type { Challenge } from '..';
import { SolutionEditor } from './solution-editor';
import { useState } from 'react';
import { SharedSolution } from '@prisma/client';
+import { getRelativeTime } from '~/utils/relativeTime';
interface Props {
challenge: NonNullable<Challenge>;
@@ -32,9 +33,7 @@ export function Solutions({ challenge }: Props) {
<div className="flex gap-2 rounded-xl bg-blue-600/10 px-4 py-1 text-blue-600 dark:bg-blue-400/10 dark:text-blue-400">
All
</div>
- <div className="flex gap-2 rounded-xl bg-black/5 px-4 py-1 text-neutral-500 dark:bg-white/5 dark:text-neutral-400">
- Tag 2
- </div>
+ <div className="flex gap-2 rounded-xl bg-black/5 px-4 py-1 text-neutral-500 dark:bg-white/5 dark:text-neutral-400">generics</div>
</div>
<Button
className="h-8 rounded-xl bg-emerald-600 hover:bg-emerald-500 dark:bg-emerald-400 dark:hover:bg-emerald-300"
@@ -67,14 +66,14 @@ function SolutionRow({
<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">
{solution.user?.name}
</div>
- <div>{date.toLocaleString()}</div>
+ <div>{getRelativeTime(solution.createdAt)}</div>
</div>
<div className="flex gap-3 text-neutral-600 duration-300 dark:text-neutral-400">
<button className="flex cursor-pointer items-center gap-2 rounded-full bg-emerald-600/10 px-2 py-1 text-sm text-emerald-600 duration-300 hover:bg-emerald-400/40 dark:bg-emerald-400/20 dark:text-emerald-400 dark:hover:bg-emerald-400/40">
- <ArrowUp className="h-4 w-4 stroke-1" /> 70
+ <ArrowUp className="h-4 w-4 stroke-1" /> {solution._count.vote}
</button>
<button className="flex cursor-pointer items-center gap-2 rounded-full bg-black/5 px-2 py-1 text-sm duration-300 hover:bg-black/20 dark:bg-white/5 dark:hover:bg-white/20">
- <MessageCircle className="h-4 w-4 stroke-1" /> 69
+ <MessageCircle className="h-4 w-4 stroke-1" /> {solution._count.solutionComment}
</button>
</div>
</div>
diff --git a/src/components/challenge/solutions/solution-editor.tsx b/src/components/challenge/solutions/solution-editor.tsx
index 3a50481e..8f2ac66d 100644
--- a/src/components/challenge/solutions/solution-editor.tsx
+++ b/src/components/challenge/solutions/solution-editor.tsx
@@ -58,11 +58,13 @@ export function SolutionEditor({ setOpen, challenge }: Props) {
userId: session?.data?.user?.id as string,
});
- router.refresh();
toast({
variant: 'success',
title: 'Your solution has been posted!',
});
+
+ // refresh the router so we see latest data
+ router.refresh();
} catch (error) {
toast({
variant: 'destructive',