diff --git a/apps/portal/src/components/offers/profile/comments/CommentCard.tsx b/apps/portal/src/components/offers/profile/comments/CommentCard.tsx index f0ce4f6d..c94d7b5a 100644 --- a/apps/portal/src/components/offers/profile/comments/CommentCard.tsx +++ b/apps/portal/src/components/offers/profile/comments/CommentCard.tsx @@ -1,7 +1,10 @@ import { signIn, useSession } from 'next-auth/react'; import { useState } from 'react'; -import { ChatBubbleBottomCenterIcon } from '@heroicons/react/24/outline'; -import { Button, HorizontalDivider, TextArea } from '@tih/ui'; +import { + ChatBubbleBottomCenterIcon, + TrashIcon, +} from '@heroicons/react/24/outline'; +import { Button, Dialog, HorizontalDivider, TextArea, useToast } from '@tih/ui'; import { timeSinceNow } from '~/utils/offers/time'; @@ -25,12 +28,15 @@ export default function CommentCard({ handleExpanded, isExpanded, profileId, - token = '', replyLength = 0, + token = '', }: Props) { const { data: session, status } = useSession(); const [isReplying, setIsReplying] = useState(false); const [currentReply, setCurrentReply] = useState(''); + const [isDialogOpen, setIsDialogOpen] = useState(false); + const { showToast } = useToast(); + const deletable: boolean = token.length > 0 || user?.id === session?.user?.id; const trpcContext = trpc.useContext(); const createCommentMutation = trpc.useMutation(['offers.comments.create'], { @@ -91,6 +97,33 @@ export default function CommentCard({ } } + const deleteCommentMutation = trpc.useMutation(['offers.comments.delete'], { + onSuccess() { + trpcContext.invalidateQueries([ + 'offers.comments.getComments', + { profileId }, + ]); + }, + }); + function handleDelete() { + deleteCommentMutation.mutate( + { + id, + profileId, + token, + userId: session?.user?.id, + }, + { + onError: () => { + showToast({ title: `Server Error`, variant: 'failure' }); + }, + onSuccess: () => { + showToast({ title: `Deleted comment`, variant: 'success' }); + }, + }, + ); + } + return ( <>
@@ -122,6 +155,47 @@ export default function CommentCard({ />
)} + {deletable && ( + <> +