From 41963ef28bf608c43eefcc8fa23e7b8cc96fedbd Mon Sep 17 00:00:00 2001 From: Terence Ho <> Date: Wed, 19 Oct 2022 12:47:56 +0800 Subject: [PATCH] [resumes][refactor] abstract comment votes fetching from comments --- .../comments/ResumeCommentListItem.tsx | 44 ++++++++++++------- apps/portal/src/server/router/index.ts | 6 ++- .../router/resumes/resumes-comments-router.ts | 13 ------ .../resumes/resumes-comments-votes-router.ts | 38 ++++++++++++++++ ... => resumes-comments-votes-user-router.ts} | 8 +--- apps/portal/src/types/resume-comments.d.ts | 7 ++- 6 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 apps/portal/src/server/router/resumes/resumes-comments-votes-router.ts rename apps/portal/src/server/router/resumes/{resumes-comments-upvotes-user-router.ts => resumes-comments-votes-user-router.ts} (82%) diff --git a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx index 1649c52f..fee44c97 100644 --- a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx +++ b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx @@ -54,25 +54,32 @@ export default function ResumeCommentListItem({ }, }, ); - const commentUpvoteUpsertMutation = trpc.useMutation( - 'resumes.comments.upvotes.user.upsert', + + // COMMENT VOTES + const commentVotesQuery = trpc.useQuery([ + 'resumes.comments.votes.list', + { commentId: comment.id }, + ]); + const commentVotesUpsertMutation = trpc.useMutation( + 'resumes.comments.votes.user.upsert', { onSuccess: () => { // Comment updated, invalidate query to trigger refetch - trpcContext.invalidateQueries(['resumes.comments.list']); + trpcContext.invalidateQueries(['resumes.comments.votes.list']); }, }, ); - const commentUpvoteDeleteMutation = trpc.useMutation( - 'resumes.comments.upvotes.user.delete', + const commentVotesDeleteMutation = trpc.useMutation( + 'resumes.comments.votes.user.delete', { onSuccess: () => { // Comment updated, invalidate query to trigger refetch - trpcContext.invalidateQueries(['resumes.comments.list']); + trpcContext.invalidateQueries(['resumes.comments.votes.list']); }, }, ); + // FORM ACTIONS const onCancel = () => { reset({ description: comment.description }); setIsEditingComment(false); @@ -98,14 +105,13 @@ export default function ResumeCommentListItem({ }; const onVote = async (value: Vote) => { - if (comment.userVote?.value === value) { - return commentUpvoteDeleteMutation.mutate({ + if (commentVotesQuery.data?.userVote?.value === value) { + return commentVotesDeleteMutation.mutate({ commentId: comment.id, }); } - return commentUpvoteUpsertMutation.mutate({ + return commentVotesUpsertMutation.mutate({ commentId: comment.id, - id: comment.userVote?.id, value, }); }; @@ -190,15 +196,16 @@ export default function ResumeCommentListItem({ -
{comment.numVotes}
+
+ {commentVotesQuery.data?.numVotes ?? 0} +