[resumes][feat] add delete comment

pull/537/head
Terence Ho 3 years ago
parent 811e6fc761
commit 11377a5eee

@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "ResumesComment" DROP CONSTRAINT "ResumesComment_parentId_fkey";
-- AddForeignKey
ALTER TABLE "ResumesComment" ADD CONSTRAINT "ResumesComment_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "ResumesComment"("id") ON DELETE CASCADE ON UPDATE CASCADE;

@ -187,7 +187,7 @@ model ResumesComment {
resume ResumesResume @relation(fields: [resumeId], references: [id], onDelete: Cascade) resume ResumesResume @relation(fields: [resumeId], references: [id], onDelete: Cascade)
votes ResumesCommentVote[] votes ResumesCommentVote[]
user User @relation(fields: [userId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Cascade)
parent ResumesComment? @relation("parentComment", fields: [parentId], references: [id]) parent ResumesComment? @relation("parentComment", fields: [parentId], references: [id], onDelete: Cascade)
children ResumesComment[] @relation("parentComment") children ResumesComment[] @relation("parentComment")
} }

@ -120,7 +120,7 @@ export default function ResumeCommentListItem({
</button> </button>
)} )}
{isCommentOwner && !isDeletingComment && ( {isCommentOwner && (
<button <button
className="-my-1 rounded-md px-2 py-1 text-xs font-medium text-slate-500 hover:bg-slate-100 hover:text-red-600" className="-my-1 rounded-md px-2 py-1 text-xs font-medium text-slate-500 hover:bg-slate-100 hover:text-red-600"
type="button" type="button"
@ -136,6 +136,7 @@ export default function ResumeCommentListItem({
{/* Delete comment form */} {/* Delete comment form */}
{isDeletingComment && ( {isDeletingComment && (
<ResumeCommentDeleteForm <ResumeCommentDeleteForm
id={comment.id}
isDeletingComment={isDeletingComment} isDeletingComment={isDeletingComment}
setIsDeletingComment={setIsDeletingComment} setIsDeletingComment={setIsDeletingComment}
/> />

@ -1,15 +1,49 @@
import { Button, Dialog } from '@tih/ui'; import { Button, Dialog } from '@tih/ui';
import { useGoogleAnalytics } from '~/components/global/GoogleAnalytics';
import { trpc } from '~/utils/trpc';
type ResumeCommentDeleteFormProps = Readonly<{ type ResumeCommentDeleteFormProps = Readonly<{
id: string;
isDeletingComment: boolean; isDeletingComment: boolean;
setIsDeletingComment: (value: boolean) => void; setIsDeletingComment: (value: boolean) => void;
}>; }>;
export default function ResumeCommentDeleteForm({ export default function ResumeCommentDeleteForm({
id,
isDeletingComment, isDeletingComment,
setIsDeletingComment, setIsDeletingComment,
}: ResumeCommentDeleteFormProps) { }: ResumeCommentDeleteFormProps) {
const onDelete = () => { const { event: gaEvent } = useGoogleAnalytics();
setIsDeletingComment(false);
const trpcContext = trpc.useContext();
const commentDeleteMutation = trpc.useMutation(
'resumes.comments.user.delete',
{
onSuccess: () => {
// Comments updated, invalidate query to trigger refetch
trpcContext.invalidateQueries(['resumes.comments.list']);
},
},
);
const onDelete = async () => {
return commentDeleteMutation.mutate(
{
id,
},
{
onSuccess: () => {
setIsDeletingComment(false);
gaEvent({
action: 'resumes.comment_delete',
category: 'engagement',
label: 'Delete comment',
});
},
},
);
}; };
const onCancel = () => { const onCancel = () => {
@ -21,7 +55,9 @@ export default function ResumeCommentDeleteForm({
isShown={isDeletingComment} isShown={isDeletingComment}
primaryButton={ primaryButton={
<Button <Button
disabled={commentDeleteMutation.isLoading}
display="block" display="block"
isLoading={commentDeleteMutation.isLoading}
label="Delete" label="Delete"
variant="danger" variant="danger"
onClick={onDelete} onClick={onDelete}
@ -29,6 +65,7 @@ export default function ResumeCommentDeleteForm({
} }
secondaryButton={ secondaryButton={
<Button <Button
disabled={commentDeleteMutation.isLoading}
display="block" display="block"
label="Cancel" label="Cancel"
variant="tertiary" variant="tertiary"
@ -37,7 +74,10 @@ export default function ResumeCommentDeleteForm({
} }
title="Are you sure?" title="Are you sure?"
onClose={() => setIsDeletingComment(false)}> onClose={() => setIsDeletingComment(false)}>
<div>Note that this is irreversible!</div> <div>
Note that deleting this comment will delete all its replies as well.
This action is also irreversible! Please check before confirming!
</div>
</Dialog> </Dialog>
); );
} }

@ -99,4 +99,16 @@ export const resumesCommentsUserRouter = createProtectedRouter()
}, },
}); });
}, },
})
.mutation('delete', {
input: z.object({ id: z.string() }),
async resolve({ ctx, input }) {
const { id } = input;
return await ctx.prisma.resumesComment.delete({
where: {
id,
},
});
},
}); });

Loading…
Cancel
Save