Skip to content

Commit

Permalink
Fix comment like functionality and remove unused
Browse files Browse the repository at this point in the history
code
  • Loading branch information
thebkht committed Nov 26, 2023
1 parent 602ff2d commit d9c021d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
7 changes: 1 addition & 6 deletions app/(Main)/[username]/[url]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -114,8 +111,6 @@ export default async function PostView({ params, searchParams }: { params: { use
}
}
return (
<>
<Post post={post} author={author} sessionUser={sessionUser} tags={post.tags} comments={Boolean(commentsOpen)} published={published} />
</>
<Post post={post} author={author} sessionUser={sessionUser} tags={post.tags} comments={Boolean(commentsOpen)} published={published} />
)
}
47 changes: 47 additions & 0 deletions app/api/comments/[id]/like/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
15 changes: 12 additions & 3 deletions components/blog/comments/comment-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<typeof Card> & { 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<any>(initialComment);
React.useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions components/blog/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export default function PostTabs({ post: initialPost, className, session, author
<div className="flex items-center gap-1.5">
{
session ? (
<Button className="h-10 w-10 mr-0.5 rounded-full hover:bg-primary hover:text-primary-foreground" size={"icon"} variant={"ghost"} >
<Bookmark className={`w-5 h-5 ${isSaved && 'fill-current'}`} strokeWidth={2} onClick={() => save(post.id)} />
<Button className="h-10 w-10 mr-0.5 rounded-full hover:bg-primary hover:text-primary-foreground" size={"icon"} variant={"ghost"} onClick={() => save(post.id)} >
<Bookmark className={`w-5 h-5 ${isSaved && 'fill-current'}`} strokeWidth={2} />
<span className="sr-only">Save</span>
</Button>
) : (
Expand Down
1 change: 0 additions & 1 deletion components/like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit d9c021d

Please sign in to comment.