import { zodResolver } from '@hookform/resolvers/zod'; import { FC, useState, Dispatch, SetStateAction, useContext } from 'react'; import { Rating } from 'react-daisyui'; import { useForm, SubmitHandler } from 'react-hook-form'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments'; import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResult'; import CreateCommentValidationSchema from '@/services/types/CommentSchema/CreateCommentValidationSchema'; import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments'; import ToastContext from '@/contexts/ToastContext'; import FormError from '../ui/forms/FormError'; import FormInfo from '../ui/forms/FormInfo'; import FormLabel from '../ui/forms/FormLabel'; import FormSegment from '../ui/forms/FormSegment'; import FormTextArea from '../ui/forms/FormTextArea'; interface EditCommentBodyProps { comment: z.infer; setInEditMode: Dispatch>; mutate: ReturnType< typeof useBeerPostComments | typeof useBreweryPostComments >['mutate']; handleDeleteRequest: (id: string) => Promise; handleEditRequest: ( id: string, data: z.infer, ) => Promise; } const EditCommentBody: FC = ({ comment, setInEditMode, mutate, handleDeleteRequest, handleEditRequest, }) => { const { register, handleSubmit, formState, setValue, watch } = useForm< z.infer >({ defaultValues: { content: comment.content, rating: comment.rating }, resolver: zodResolver(CreateCommentValidationSchema), }); const { toast } = useContext(ToastContext); const { errors } = formState; const [isDeleting, setIsDeleting] = useState(false); const onDelete = async () => { setIsDeleting(true); await handleDeleteRequest(comment.id); await mutate(); }; const onEdit: SubmitHandler> = async ( data, ) => { setInEditMode(true); await handleEditRequest(comment.id, data); await mutate(); toast.success('Submitted edits'); setInEditMode(false); }; return (
Edit your comment {errors.content?.message}
Change your rating {errors.rating?.message} { setValue('rating', value); }} > {Array.from({ length: 5 }).map((val, index) => ( ))}
); }; export default EditCommentBody;