Skip to content

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

adds basic ability to bookmark

Chris Bautista3df60d805060
diff --git a/src/components/challenge/bookmark.action.ts b/src/components/challenge/bookmark.action.ts
new file mode 100644
index 00000000..08a405ee
--- /dev/null
+++ b/src/components/challenge/bookmark.action.ts
@@ -0,0 +1,30 @@
+'use server';
+import { prisma } from '~/server/db';
+
+export async function addOrRemoveBookmark(
+ challengeId: number,
+ userId: string,
+ shouldBookmark: boolean,
+) {
+ const bookmarkExists = await prisma.bookmark.findFirst({
+ where: {
+ challengeId,
+ userId,
+ },
+ });
+ if (!bookmarkExists && shouldBookmark) {
+ await prisma.bookmark.create({
+ data: {
+ challengeId,
+ userId,
+ },
+ });
+ }
+ if (bookmarkExists && !shouldBookmark) {
+ await prisma.bookmark.delete({
+ where: {
+ id: bookmarkExists.id,
+ },
+ });
+ }
+}
diff --git a/src/components/challenge/description-panel.tsx b/src/components/challenge/description-panel.tsx
index 12ebbadd..0b6fa402 100644
--- a/src/components/challenge/description-panel.tsx
+++ b/src/components/challenge/description-panel.tsx
@@ -1,12 +1,12 @@
'use client';
-import type { Challenge, Vote } from '@prisma/client';
+import type { Bookmark, Challenge, Vote } from '@prisma/client';
import { clsx } from 'clsx';
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';
import remarkGfm from 'remark-gfm';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { debounce } from 'lodash';
-import { Bookmark, Share, ThumbsUp } from 'lucide-react';
+import { Bookmark as BookmarkIcon, Share, ThumbsUp } from 'lucide-react';
import { useSession } from 'next-auth/react';
import { useRef, useState } from 'react';
import ReactMarkdown from 'react-markdown';
@@ -17,10 +17,12 @@ import { DifficultyBadge } from '../explore/difficulty-badge';
import { Button } from '../ui/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '../ui/dialog';
import { ShareForm } from './share-form';
+import { addOrRemoveBookmark } from './bookmark.action';
interface Props {
challenge: Challenge & {
Vote: Vote[];
+ Bookmark: Bookmark[];
_count: {
Vote: number;
};
@@ -29,6 +31,7 @@ interface Props {
export function DescriptionPanel({ challenge }: Props) {
const [votes, setVotes] = useState(challenge._count.Vote);
const [hasVoted, setHasVoted] = useState(challenge.Vote.length > 0);
+ const [hasBookmarked, setHasBookmarked] = useState(challenge.Bookmark.length > 0);
const session = useSession();
const debouncedSearch = useRef(
debounce(async (challengeId: number, userId: string, shouldIncrement: boolean) => {
@@ -39,6 +42,20 @@ export function DescriptionPanel({ challenge }: Props) {
}, 500),
).current;
+ console.log({ challenge });
+
+ const debouncedBookmark = useRef(
+ debounce(async (challengeId: number, userId: string, shouldBookmark: boolean) => {
+ try {
+ await addOrRemoveBookmark(challengeId, userId, shouldBookmark);
+ setHasBookmarked(shouldBookmark);
+ } catch (e) {
+ console.error(e);
+ // it errored so reverse the intended changes
+ setHasBookmarked(!shouldBookmark);
+ }
+ }, 500),
+ ).current;
return (
<div className="flex-1 overflow-y-auto rounded-md bg-white dark:bg-zinc-800">
<Tabs defaultValue="description" className="w-full">
@@ -55,47 +72,78 @@ export function DescriptionPanel({ challenge }: Props) {
<TypographyH3 className="mb-2 font-medium">{challenge.name}</TypographyH3>
<div className="mb-6 flex items-center gap-6">
<DifficultyBadge difficulty={challenge.difficulty} />
- {session?.data?.user?.id && (
- <Button
- className="gap-2 p-1"
- variant="ghost"
- onClick={(): void => {
- let shouldIncrement: boolean;
- if (hasVoted) {
- setVotes((v) => v - 1);
- shouldIncrement = false;
- setHasVoted(false);
- } else {
- setVotes((v) => v + 1);
- shouldIncrement = true;
- setHasVoted(true);
- }
- debouncedSearch(challenge.id, session?.data?.user?.id, shouldIncrement)?.catch(
- (e) => {
- console.error(e);
- },
- );
- }}
- >
- <ThumbsUp
- size={20}
- className={clsx({
+ <Button
+ className="gap-2 p-1"
+ variant="ghost"
+ disabled={!session?.data?.user?.id}
+ onClick={(): void => {
+ let shouldIncrement = false;
+ if (hasVoted) {
+ setVotes((v) => v - 1);
+ shouldIncrement = false;
+ setHasVoted(false);
+ } else {
+ setVotes((v) => v + 1);
+ shouldIncrement = true;
+ setHasVoted(true);
+ }
+ debouncedSearch(
+ challenge.id,
+ session?.data?.user?.id as string,
+ shouldIncrement,
+ )?.catch((e) => {
+ console.error(e);
+ });
+ }}
+ >
+ <ThumbsUp
+ size={20}
+ className={clsx(
+ {
'fill-green-700 stroke-green-700': hasVoted,
'stroke-gray-500': !hasVoted,
- })}
- />
- <span className="self-end text-lg text-gray-500">{votes}</span>
- </Button>
- )}
- <Button variant="ghost" className="p-1">
- <Bookmark size={20} className="stroke-gray-500 hover:stroke-gray-400" />
+ },
+ 'hover:stroke-gray-400',
+ )}
+ />
+ <span className="self-end text-lg text-gray-500">{votes}</span>
+ </Button>
+ <Button
+ variant="ghost"
+ className="p-1"
+ disabled={!session?.data?.user?.id}
+ onClick={(): void => {
+ let shouldBookmark = false;
+ if (hasBookmarked) {
+ shouldBookmark = false;
+ setHasBookmarked(false);
+ } else {
+ shouldBookmark = true;
+ setHasBookmarked(true);
+ }
+ debouncedBookmark(
+ challenge.id,
+ session?.data?.user?.id as string,
+ shouldBookmark,
+ )?.catch((e) => {
+ console.error(e);
+ });
+ }}
+ >
+ <BookmarkIcon
+ size={20}
+ className={clsx(
+ {
+ 'fill-blue-500 stroke-blue-500': hasBookmarked,
+ 'stroke-gray-500': !hasBookmarked,
+ },
+ 'hover:stroke-gray-400',
+ )}
+ />
</Button>
<Dialog>
<DialogTrigger>
- <Share
- size={20}
- className="stroke-gray-500 hover:stroke-gray-400"
- />
+ <Share size={20} className="stroke-gray-500 hover:stroke-gray-400" />
</DialogTrigger>
<DialogContent className="w-[200px]">
<DialogHeader>
diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsx
index 064e75b0..1bd55309 100644
--- a/src/components/challenge/index.tsx
+++ b/src/components/challenge/index.tsx
@@ -24,10 +24,16 @@ export async function Challenge({ id }: Props) {
challengeId: +id,
},
},
+ Bookmark: {
+ where: {
+ userId: session?.user.id,
+ challengeId: +id,
+ },
+ },
},
});
- if (!challenge || typeof challenge.prompt !== "string") {
+ if (!challenge || typeof challenge.prompt !== 'string') {
notFound();
}