diff --git a/app/(Main)/[username]/[url]/page.tsx b/app/(Main)/[username]/[url]/page.tsx index 858263b..d44047a 100644 --- a/app/(Main)/[username]/[url]/page.tsx +++ b/app/(Main)/[username]/[url]/page.tsx @@ -4,9 +4,7 @@ import { notFound } from "next/navigation" import postgres from "@/lib/postgres" import Post from "@/components/blog/post" import { cookies } from 'next/headers' -import { ObjectId } from 'bson'; -const id = new ObjectId().toHexString(); export default async function PostView({ params, searchParams }: { params: { username: string, url: string }, searchParams: { [key: string]: string | string[] | undefined } }) { const commentsOpen = typeof searchParams.commentsOpen === 'string' ? searchParams.commentsOpen : undefined @@ -96,7 +94,6 @@ export default async function PostView({ params, searchParams }: { params: { use if (!hasReaded) { await postgres.readingHistory.create({ data: { - id, postId: post?.id, userId: sessionUser?.id } @@ -114,8 +111,6 @@ export default async function PostView({ params, searchParams }: { params: { use } } return ( - <> - - + ) } \ No newline at end of file diff --git a/app/api/comments/[id]/like/route.ts b/app/api/comments/[id]/like/route.ts new file mode 100644 index 0000000..77a1a5e --- /dev/null +++ b/app/api/comments/[id]/like/route.ts @@ -0,0 +1,47 @@ +import { getSessionUser } from "@/components/get-session-user"; +import postgres from "@/lib/postgres"; + +export async function POST(req: Request) { + try { + const { commentId } = await req.json(); + const user = await getSessionUser(); + if (!user) { + console.log("No session user"); + return new Response(null, { status: 401 }); + } + const comment = await postgres.comment.findUnique({ + where: { + id: commentId, + }, + }); + if (!comment) { + return new Response(null, { status: 404 }); + } + const isLiked = await postgres.commentLike.findFirst({ + where: { + commentId, + authorId: user.id, + }, + }); + if (isLiked) { + await postgres.commentLike.delete({ + where: { + id: isLiked.id, + }, + }); + console.log("Deleted like"); + } else { + await postgres.commentLike.create({ + data: { + commentId, + authorId: user.id, + }, + }); + console.log("Created like"); + } + return new Response(null, { status: 200 }); + } catch (error) { + console.error(error); + return new Response(null, { status: 500 }); + } +} diff --git a/components/blog/comments/comment-card.tsx b/components/blog/comments/comment-card.tsx index fdea8f1..1a7fd1f 100644 --- a/components/blog/comments/comment-card.tsx +++ b/components/blog/comments/comment-card.tsx @@ -24,6 +24,8 @@ import { Separator } from "@/components/ui/separator"; import { formatNumberWithSuffix } from "@/components/format-numbers"; import { getComment } from "@/lib/prisma/get-comment"; import MarkdownCard from "@/components/markdown-card"; +import { Comment } from "@prisma/client"; +import { validate } from "@/lib/revalidate"; export function dateFormat(dateString: string | number | Date) { const date = new Date(dateString); @@ -56,15 +58,22 @@ export function dateFormat(dateString: string | number | Date) { } } -async function fetchComment(commentId: number) { +async function fetchComment(commentId: Comment['id']) { return await getComment(commentId); } export default function CommentCard({ comment: initialComment, post, session, ...props }: React.ComponentPropsWithoutRef & { comment: any, post: any, session: any }) { const pathname = usePathname(); - const like = async (commentId: number) => { - await handleCommentLike({ commentId, path: pathname }); + const like = async (commentId: Comment['id']) => { + await fetch(`/api/comment/${commentId}/like`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ commentId }), + }); + await validate(pathname) } const [comment, setComment] = React.useState(initialComment); React.useEffect(() => { diff --git a/components/blog/navbar.tsx b/components/blog/navbar.tsx index 09c065d..ad7adfa 100644 --- a/components/blog/navbar.tsx +++ b/components/blog/navbar.tsx @@ -82,8 +82,8 @@ export default function PostTabs({ post: initialPost, className, session, author
{ session ? ( - ) : ( diff --git a/components/like.ts b/components/like.ts index 1edaab0..2e7c996 100644 --- a/components/like.ts +++ b/components/like.ts @@ -64,7 +64,6 @@ export const handleCommentLike = async ({ commentId, path} : {commentId: Comment if (!sessionUser) { console.log("No session user") } - console.log(path, commentId) try { const liked = await postgres.commentLike.findFirst({ where: {