Skip to content

Commit 2936d63

Browse files
committed
feat: delete comment
1 parent 58b33d9 commit 2936d63

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

app/lib/comment-item.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { Auth } from "@supabase/ui";
12
import Link from "next/link";
3+
import { useRouter } from "next/router";
24
import React from "react";
5+
import { useMutation } from "urql";
36

47
import { DocumentType, gql } from "../gql";
5-
import { CalendarIcon, CommentIcon, PointIcon } from "./icons";
68
import { timeAgo } from "./time-ago";
79

810
const CommentItem_CommentFragment = gql(/* GraphQL */ `
@@ -20,20 +22,51 @@ const CommentItem_CommentFragment = gql(/* GraphQL */ `
2022
}
2123
`);
2224

25+
const CommentItem_DeleteCommentFragment = gql(/* GraphQL */ `
26+
mutation CommentItem_DeleteComment($commentId: BigInt!) {
27+
deleteFromCommentCollection(atMost: 1, filter: { id: { eq: $commentId } }) {
28+
affectedCount
29+
}
30+
}
31+
`);
32+
2333
export function CommentItem(props: {
2434
comment: DocumentType<typeof CommentItem_CommentFragment>;
2535
}) {
36+
const router = useRouter();
37+
const { user } = Auth.useUser();
38+
const [deleteCommentMutation, deleteComment] = useMutation(
39+
CommentItem_DeleteCommentFragment
40+
);
2641
const createdAt = React.useMemo(
2742
() => timeAgo.format(new Date(props.comment.createdAt)),
2843
[props.comment.createdAt]
2944
);
45+
46+
React.useEffect(() => {
47+
if (deleteCommentMutation.data) {
48+
router.reload();
49+
}
50+
}, [deleteCommentMutation.data]);
3051
return (
3152
<div className="pb-4">
3253
<div className="text-sm">
3354
<Link href={`/profile/${props.comment.profile?.id}`}>
3455
<a className="font-bold">{props.comment.profile?.username} </a>
3556
</Link>
3657
<span>{createdAt}</span>
58+
{user?.id && user.id === props.comment.profile?.id ? (
59+
<button
60+
className="ml-2 text-xs text-gray-400 hover:text-gray-900"
61+
onClick={() => {
62+
deleteComment({
63+
commentId: props.comment.id,
64+
});
65+
}}
66+
>
67+
delete
68+
</button>
69+
) : null}
3770
</div>
3871
<div className="flex-1 md:flex-grow">{props.comment.message}</div>
3972
</div>

0 commit comments

Comments
 (0)