1+ import { Auth } from "@supabase/ui" ;
12import Link from "next/link" ;
3+ import { useRouter } from "next/router" ;
24import React from "react" ;
5+ import { useMutation } from "urql" ;
36
47import { DocumentType , gql } from "../gql" ;
5- import { CalendarIcon , CommentIcon , PointIcon } from "./icons" ;
68import { timeAgo } from "./time-ago" ;
79
810const 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+
2333export 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