From a2bf45ad5607f093e9ccdd74914a9987ea5dd6d6 Mon Sep 17 00:00:00 2001 From: Zhang Ziqing <69516975+ziqing26@users.noreply.github.com> Date: Mon, 31 Oct 2022 00:53:44 +0800 Subject: [PATCH] [offers][feat] delete comment (#464) * [offers][feat] delete comment * [offers][style] arrange loading spinner in profile --- .../offers/profile/comments/CommentCard.tsx | 84 ++++++++++++++++++- .../comments/ExpandableCommentCard.tsx | 1 + .../pages/offers/profile/[offerProfileId].tsx | 12 ++- 3 files changed, 91 insertions(+), 6 deletions(-) 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 && ( + <> +