diff --git a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx index b331e7f6..66fd6004 100644 --- a/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx +++ b/apps/portal/src/components/resumes/comments/ResumeCommentListItem.tsx @@ -1,8 +1,14 @@ +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 { Button, TextArea } from '@tih/ui'; + +import { trpc } from '~/utils/trpc'; import ResumeExpandableText from '../shared/ResumeExpandableText'; @@ -13,11 +19,63 @@ type ResumeCommentListItemProps = { userId?: string; }; +type ICommentInput = { + description: string; +}; + export default function ResumeCommentListItem({ comment, userId, }: ResumeCommentListItemProps) { const isCommentOwner = userId === comment.user.userId; + const [isEditingComment, setIsEditingComment] = 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']); + }, + }, + ); + + const onCancel = () => { + reset({ description: comment.description }); + setIsEditingComment(false); + }; + + const onSubmit: SubmitHandler = async (data) => { + const { id } = comment; + return await commentUpdateMutation.mutate( + { + id, + ...data, + }, + { + onSuccess: () => { + setIsEditingComment(false); + }, + }, + ); + }; + + const setFormValue = (value: string) => { + setValue('description', value.trim(), { shouldDirty: true }); + }; return (
@@ -54,7 +112,45 @@ export default function ResumeCommentListItem({
{/* Description */} - + {isEditingComment ? ( +
+
+