From 632e79e9c6fd4a04e16c9376c0ff5008a66caf21 Mon Sep 17 00:00:00 2001 From: Keane Chan Date: Fri, 28 Oct 2022 11:49:40 +0800 Subject: [PATCH] [resumes][feat] auto-resolve resume when comments >= 5 --- .../resumes/badges/ResumeUserBadges.tsx | 35 ++++++++++++------- .../resumes/browse/ResumeListItem.tsx | 13 ++++++- .../resumes/comments/ResumeCommentsForm.tsx | 26 ++++++++++++-- apps/portal/src/pages/resumes/browse.tsx | 2 ++ .../resumes/resumes-comments-user-router.ts | 12 ++++++- 5 files changed, 71 insertions(+), 17 deletions(-) diff --git a/apps/portal/src/components/resumes/badges/ResumeUserBadges.tsx b/apps/portal/src/components/resumes/badges/ResumeUserBadges.tsx index 1622dd9e..3dfe413b 100644 --- a/apps/portal/src/components/resumes/badges/ResumeUserBadges.tsx +++ b/apps/portal/src/components/resumes/badges/ResumeUserBadges.tsx @@ -8,19 +8,30 @@ type Props = Readonly<{ userId: string; }>; +const STALE_TIME = 60; + export default function ResumeUserBadges({ userId }: Props) { - const userReviewedResumeCountQuery = trpc.useQuery([ - 'resumes.resume.findUserReviewedResumeCount', - { userId }, - ]); - const userMaxResumeUpvoteCountQuery = trpc.useQuery([ - 'resumes.resume.findUserMaxResumeUpvoteCount', - { userId }, - ]); - const userTopUpvotedCommentCountQuery = trpc.useQuery([ - 'resumes.resume.findUserTopUpvotedCommentCount', - { userId }, - ]); + const userReviewedResumeCountQuery = trpc.useQuery( + ['resumes.resume.findUserReviewedResumeCount', { userId }], + { + retry: false, + staleTime: STALE_TIME, + }, + ); + const userMaxResumeUpvoteCountQuery = trpc.useQuery( + ['resumes.resume.findUserMaxResumeUpvoteCount', { userId }], + { + retry: false, + staleTime: STALE_TIME, + }, + ); + const userTopUpvotedCommentCountQuery = trpc.useQuery( + ['resumes.resume.findUserTopUpvotedCommentCount', { userId }], + { + retry: false, + staleTime: STALE_TIME, + }, + ); const payload: BadgePayload = { maxResumeUpvoteCount: userMaxResumeUpvoteCountQuery.data ?? 0, diff --git a/apps/portal/src/components/resumes/browse/ResumeListItem.tsx b/apps/portal/src/components/resumes/browse/ResumeListItem.tsx index 9db65f49..599bec71 100644 --- a/apps/portal/src/components/resumes/browse/ResumeListItem.tsx +++ b/apps/portal/src/components/resumes/browse/ResumeListItem.tsx @@ -34,7 +34,18 @@ export default function ResumeListItem({ href, resumeInfo }: Props) {
- {resumeInfo.title} +
+

{resumeInfo.title}

+ {resumeInfo.isResolved ? ( +

+ Reviewed +

+ ) : ( +

+ Unreviewed +

+ )} +
{ + trpcContext.invalidateQueries(['resumes.resume.findOne']); + trpcContext.invalidateQueries(['resumes.resume.findAll']); + trpcContext.invalidateQueries(['resumes.resume.user.findUserStarred']); + trpcContext.invalidateQueries(['resumes.resume.user.findUserCreated']); + }; + + const resolveMutation = trpc.useMutation('resumes.resume.user.resolve', { + onSuccess() { + invalidateResumeQueries(); + }, + }); // TODO: Give a feedback to the user if the action succeeds/fails - const onSubmit: SubmitHandler = async (data) => { + const onSubmit: SubmitHandler = async (formData) => { return await commentCreateMutation.mutate( { resumeId, - ...data, + ...formData, }, { - onSuccess: () => { + onSuccess: (data) => { // Redirect back to comments section setShowCommentsForm(false); + const { prevCount, newCount } = data; + // Auto mark resume as resolved once the total comments passes the 5 threshold + if (newCount >= 5 && prevCount < 5) { + resolveMutation.mutate({ + id: resumeId, + val: true, + }); + } }, }, ); diff --git a/apps/portal/src/pages/resumes/browse.tsx b/apps/portal/src/pages/resumes/browse.tsx index bbbe7fc4..de2b13c6 100644 --- a/apps/portal/src/pages/resumes/browse.tsx +++ b/apps/portal/src/pages/resumes/browse.tsx @@ -471,6 +471,7 @@ export default function ResumeHomePage() {
+ {/* Quick Access Section */}

Quick access

@@ -489,6 +490,7 @@ export default function ResumeHomePage() { ))} + {/* Filter Section */}

Explore these filters

diff --git a/apps/portal/src/server/router/resumes/resumes-comments-user-router.ts b/apps/portal/src/server/router/resumes/resumes-comments-user-router.ts index d061a4c2..e33a399b 100644 --- a/apps/portal/src/server/router/resumes/resumes-comments-user-router.ts +++ b/apps/portal/src/server/router/resumes/resumes-comments-user-router.ts @@ -45,9 +45,19 @@ export const resumesCommentsUserRouter = createProtectedRouter() }; }); - return await ctx.prisma.resumesComment.createMany({ + const prevCommentCount = await ctx.prisma.resumesComment.count({ + where: { + resumeId, + }, + }); + const result = await ctx.prisma.resumesComment.createMany({ data: comments, }); + + return { + newCount: Number(prevCommentCount) + result.count, + prevCount: prevCommentCount, + }; }, }) .mutation('update', {