From 452bd8e27e97d17e92e2c45ff3d2b21f9ceddf8b Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Mon, 10 Oct 2022 22:11:36 +0800 Subject: [PATCH] [questions][feat] add upvote, downvotes --- ...ListItem.tsx => AnswerCommentListItem.tsx} | 21 ++- .../components/questions/VotingButtons.tsx | 22 ++- .../components/questions/card/AnswerCard.tsx | 23 ++- .../questions/card/FullQuestionCard.tsx | 88 +++------ .../questions/card/QuestionCard.tsx | 22 +-- .../answer/[answerId]/[answerSlug]/index.tsx | 6 +- .../[questionId]/[questionSlug]/index.tsx | 15 +- apps/portal/src/pages/questions/index.tsx | 6 +- .../router/questions-answer-comment-router.ts | 4 +- .../server/router/questions-answer-router.ts | 4 +- .../questions-question-comment-router.ts | 4 +- .../router/questions-question-router.ts | 4 +- apps/portal/src/utils/questions/useVote.ts | 170 ++++++++++++++++-- 13 files changed, 250 insertions(+), 139 deletions(-) rename apps/portal/src/components/questions/{CommentListItem.tsx => AnswerCommentListItem.tsx} (63%) diff --git a/apps/portal/src/components/questions/CommentListItem.tsx b/apps/portal/src/components/questions/AnswerCommentListItem.tsx similarity index 63% rename from apps/portal/src/components/questions/CommentListItem.tsx rename to apps/portal/src/components/questions/AnswerCommentListItem.tsx index 1da9c7de..c65a379f 100644 --- a/apps/portal/src/components/questions/CommentListItem.tsx +++ b/apps/portal/src/components/questions/AnswerCommentListItem.tsx @@ -1,8 +1,11 @@ import { format } from 'date-fns'; +import { useAnswerCommentVote } from '~/utils/questions/useVote'; + import VotingButtons from './VotingButtons'; -export type CommentListItemProps = { +export type AnswerCommentListItemProps = { + answerCommentId: string; authorImageUrl: string; authorName: string; content: string; @@ -10,16 +13,26 @@ export type CommentListItemProps = { upvoteCount: number; }; -export default function CommentListItem({ +export default function AnswerCommentListItem({ authorImageUrl, authorName, content, createdAt, upvoteCount, -}: CommentListItemProps) { + answerCommentId, +}: AnswerCommentListItemProps) { + const { handleDownvote, handleUpvote, vote } = + useAnswerCommentVote(answerCommentId); + return (
- +
void; onUpvote: () => void; - voteState: VoteState; + vote: BackendVote | null; }; export type VotingButtonsProps = VotingButtonsCallbackProps & { @@ -17,16 +21,16 @@ export type VotingButtonsProps = VotingButtonsCallbackProps & { }; export default function VotingButtons({ - voteState, + vote, onDownvote, onUpvote, upvoteCount, size = 'md', }: VotingButtonsProps) { - const upvoteButtonVarient = - voteState === VoteState.UPVOTE ? 'secondary' : 'tertiary'; - const downvoteButtonVarient = - voteState === VoteState.DOWNVOTE ? 'secondary' : 'tertiary'; + const upvoteButtonVariant = + vote?.vote === 'UPVOTE' ? 'secondary' : 'tertiary'; + const downvoteButtonVariant = + vote?.vote === 'DOWNVOTE' ? 'secondary' : 'tertiary'; return (