import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult'; import { zodResolver } from '@hookform/resolvers/zod'; import { FunctionComponent } from 'react'; import { useForm, SubmitHandler } from 'react-hook-form'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments'; import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; import toast from 'react-hot-toast'; import createErrorToast from '@/util/createErrorToast'; import { sendCreateBeerCommentRequest } from '@/requests/comments/beer-comment'; import CommentForm from '../ui/CommentForm'; interface BeerCommentFormProps { beerPost: z.infer; mutate: ReturnType['mutate']; } const BeerCommentForm: FunctionComponent = ({ beerPost, mutate, }) => { const { register, handleSubmit, formState, watch, reset, setValue } = useForm< z.infer >({ defaultValues: { rating: 0 }, resolver: zodResolver(CreateCommentValidationSchema), }); const onSubmit: SubmitHandler> = async ( data, ) => { const loadingToast = toast.loading('Posting a new comment...'); try { await sendCreateBeerCommentRequest({ body: data, beerPostId: beerPost.id }); reset(); toast.remove(loadingToast); toast.success('Comment posted successfully.'); await mutate(); } catch (error) { await mutate(); toast.remove(loadingToast); createErrorToast(error); reset(); } }; return ( ); }; export default BeerCommentForm;