[resumes][feat] add padding and hide scrollbar for comments (#404)

* [resumes][feat] add padding and hide scrollbar for comments

* [resumes][feat] add findUserTopUpvotedCommentCount query
This commit is contained in:
Keane Chan
2022-10-21 15:25:14 +08:00
committed by GitHub
parent fc93596c39
commit 22d5f54a47
6 changed files with 83 additions and 19 deletions

View File

@ -17,12 +17,15 @@ export default function ResumeUserBadges({ userId }: Props) {
'resumes.resume.findUserMaxResumeUpvoteCount',
{ userId },
]);
const userTopUpvotedCommentCountQuery = trpc.useQuery([
'resumes.resume.findUserTopUpvotedCommentCount',
{ userId },
]);
// TODO: Add other badges in
const payload: BadgePayload = {
maxResumeUpvoteCount: userMaxResumeUpvoteCountQuery.data ?? 0,
reviewedResumesCount: userReviewedResumeCountQuery.data ?? 0,
topUpvotedCommentCount: 20,
topUpvotedCommentCount: userTopUpvotedCommentCountQuery.data ?? 0,
};
return (

View File

@ -56,6 +56,7 @@ export default function ResumeCommentsList({
}
return (
<Button
className="-mb-2"
display="block"
label="Add your review"
variant="tertiary"
@ -73,7 +74,7 @@ export default function ResumeCommentsList({
<Spinner display="block" size="lg" />
</div>
) : (
<div className="m-2 flow-root h-[calc(100vh-20rem)] w-full flex-col space-y-4 overflow-y-auto overflow-x-hidden">
<div className="scrollbar-hide m-2 flow-root h-[calc(100vh-20rem)] w-full flex-col space-y-4 overflow-y-auto overflow-x-hidden pt-14 pb-6">
{RESUME_COMMENTS_SECTIONS.map(({ label, value }) => {
const comments = commentsQuery.data
? commentsQuery.data.filter((comment: ResumeComment) => {

View File

@ -1,4 +1,5 @@
import { z } from 'zod';
import { Vote } from '@prisma/client';
import { createRouter } from '../context';
@ -149,11 +150,16 @@ export const resumesRouter = createRouter()
async resolve({ ctx, input }) {
return await ctx.prisma.resumesResume.count({
where: {
// User has commented on this resume
comments: {
some: {
userId: input.userId,
},
},
// Not user's own resume
userId: {
not: input.userId,
},
},
});
},
@ -164,18 +170,18 @@ export const resumesRouter = createRouter()
}),
async resolve({ ctx, input }) {
const highestUpvotedResume = await ctx.prisma.resumesResume.findFirst({
include: {
orderBy: {
stars: {
_count: 'desc',
},
},
select: {
_count: {
select: {
stars: true,
},
},
},
orderBy: {
stars: {
_count: 'desc',
},
},
where: {
userId: input.userId,
},
@ -183,14 +189,61 @@ export const resumesRouter = createRouter()
return highestUpvotedResume?._count?.stars ?? 0;
},
})
.query('findUserTopUpvotedCommentCount', {
input: z.object({
userId: z.string(),
}),
async resolve({ ctx, input }) {
const resumes = await ctx.prisma.resumesResume.findMany({
select: {
comments: {
select: {
userId: true,
votes: {
select: {
value: true,
},
},
},
},
},
});
let topUpvotedCommentCount = 0;
for (const resume of resumes) {
let highestVoteCount = 1;
// Get Map of {userId, voteCount} for each comment
const commentUpvotePairs = [];
for (const comment of resume.comments) {
const { userId, votes } = comment;
let voteCount = 0;
for (const vote of votes) {
if (vote.value === Vote.UPVOTE) {
voteCount++;
} else {
voteCount--;
}
}
if (voteCount >= highestVoteCount) {
highestVoteCount = voteCount;
commentUpvotePairs.push({ userId, voteCount });
}
}
// Filter to get the userIds with the highest vote counts
const userIds = commentUpvotePairs
.filter((pair) => pair.voteCount === highestVoteCount)
.map((pair) => pair.userId);
// Increment if input userId is the highest voted comment
if (userIds.includes(input.userId)) {
topUpvotedCommentCount++;
}
}
return topUpvotedCommentCount;
},
});
// .query('findUserTopUpvotedCommentCount', {
// input: z.object({
// userId: z.string(),
// }),
// async resolve({ ctx, input }) {
// const highestUpvotedResume = await ctx.prisma.resumesComment.groupBy({
// by: ['resumeId'],
// })
// },
// });