From 671484147e9274a5f6cfa3a1ce11561f3d4cc4f6 Mon Sep 17 00:00:00 2001 From: Terence Ho <> Date: Fri, 21 Oct 2022 09:43:56 +0800 Subject: [PATCH] [resumes][refactor] Abstract comment edit form and votes to their components --- .../comments/ResumeCommentListItem.tsx | 215 +----------------- .../comment/ResumeCommentEditForm.tsx | 106 +++++++++ .../comment/ResumeCommentVoteButtons.tsx | 129 +++++++++++ 3 files changed, 242 insertions(+), 208 deletions(-) create mode 100644 apps/portal/src/components/resumes/comments/comment/ResumeCommentEditForm.tsx create mode 100644 apps/portal/src/components/resumes/comments/comment/ResumeCommentVoteButtons.tsx diff --git a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx index 833c53c2..9a871070 100644 --- a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx +++ b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx @@ -1,18 +1,8 @@ -import clsx from 'clsx'; -import type { Dispatch, SetStateAction } from 'react'; import { useState } from 'react'; -import type { SubmitHandler } from 'react-hook-form'; -import { useForm } from 'react-hook-form'; -import { - ArrowDownCircleIcon, - ArrowUpCircleIcon, -} from '@heroicons/react/20/solid'; import { FaceSmileIcon } from '@heroicons/react/24/outline'; -import { Vote } from '@prisma/client'; -import { Button, TextArea } from '@tih/ui'; - -import { trpc } from '~/utils/trpc'; +import ResumeCommentEditForm from './comment/ResumeCommentEditForm'; +import ResumeCommentVoteButtons from './comment/ResumeCommentVoteButtons'; import ResumeUserBadges from '../badges/ResumeUserBadges'; import ResumeExpandableText from '../shared/ResumeExpandableText'; @@ -23,10 +13,6 @@ type ResumeCommentListItemProps = { userId: string | undefined; }; -type ICommentInput = { - description: string; -}; - export default function ResumeCommentListItem({ comment, userId, @@ -34,108 +20,6 @@ export default function ResumeCommentListItem({ const isCommentOwner = userId === comment.user.userId; const [isEditingComment, setIsEditingComment] = useState(false); - const [upvoteAnimation, setUpvoteAnimation] = useState(false); - const [downvoteAnimation, setDownvoteAnimation] = useState(false); - - const { - register, - handleSubmit, - setValue, - formState: { errors, isDirty }, - reset, - } = useForm({ - defaultValues: { - description: comment.description, - }, - }); - - const trpcContext = trpc.useContext(); - const commentUpdateMutation = trpc.useMutation( - 'resumes.comments.user.update', - { - onSuccess: () => { - // Comment updated, invalidate query to trigger refetch - trpcContext.invalidateQueries(['resumes.comments.list']); - }, - }, - ); - - // 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.votes.list']); - }, - }, - ); - const commentVotesDeleteMutation = trpc.useMutation( - 'resumes.comments.votes.user.delete', - { - onSuccess: () => { - // Comment updated, invalidate query to trigger refetch - trpcContext.invalidateQueries(['resumes.comments.votes.list']); - }, - }, - ); - - // FORM ACTIONS - const onCancel = () => { - reset({ description: comment.description }); - setIsEditingComment(false); - }; - - const onSubmit: SubmitHandler = async (data) => { - const { id } = comment; - return commentUpdateMutation.mutate( - { - id, - ...data, - }, - { - onSuccess: () => { - setIsEditingComment(false); - }, - }, - ); - }; - - const setFormValue = (value: string) => { - setValue('description', value.trim(), { shouldDirty: true }); - }; - - const onVote = async ( - value: Vote, - setAnimation: Dispatch>, - ) => { - setAnimation(true); - - if (commentVotesQuery.data?.userVote?.value === value) { - return commentVotesDeleteMutation.mutate( - { - commentId: comment.id, - }, - { - onSettled: async () => setAnimation(false), - }, - ); - } - return commentVotesUpsertMutation.mutate( - { - commentId: comment.id, - value, - }, - { - onSettled: async () => setAnimation(false), - }, - ); - }; - return (
@@ -174,102 +58,17 @@ export default function ResumeCommentListItem({ {/* Description */} {isEditingComment ? ( -
-
-