11import { useState } from 'react' ;
2- import { useForm } from 'react-hook-form' ;
2+ import { useController , useForm } from 'react-hook-form' ;
33import { Button } from '@mantine/core' ;
4+ import { useMutation } from '@tanstack/react-query' ;
5+ import { PostReviewParams , postReview } from '@/src/_apis/review/review-apis' ;
6+ import { ApiError } from '@/src/utils/api' ;
47import Textarea from '@/src/components/common/input/textarea' ;
58import ButtonHearts from './button-hearts' ;
69
@@ -9,32 +12,61 @@ type FormValues = {
912 score : number ;
1013} ;
1114
12- interface ReviewProps {
15+ interface ReviewFormProps {
16+ gatheringId ?: number ;
1317 onCancel : ( ) => void ;
1418}
1519
16- export default function ReviewForm ( { onCancel } : ReviewProps ) {
17- const { register, handleSubmit } = useForm < FormValues > ( ) ;
20+ export default function ReviewForm ( { gatheringId , onCancel } : ReviewFormProps ) {
21+ const { register, handleSubmit, control } = useForm < FormValues > ( ) ;
1822 const [ textReview , setTextReview ] = useState < string > ( '' ) ;
19- const [ point , setPoint ] = useState < number > ( 0 ) ;
2023
21- // TODO : 주석 부분(api 연결) 수정
22- // TODO : form에 넣기: onSubmit={handleSubmit(clickSubmit)}
24+ const {
25+ field : { value : scoreValue , onChange : setScore } ,
26+ } = useController ( { name : 'score' , control, defaultValue : 0 } ) ;
2327
2428 const handleTextChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
25- setTextReview ( e . target . value ) ;
29+ if ( e . target . value . length <= 300 ) {
30+ setTextReview ( e . target . value ) ;
31+ }
2632 } ;
2733
2834 const handleScoreChange = ( newScore : number ) => {
29- setPoint ( newScore ) ;
35+ setScore ( newScore ) ;
36+ } ;
37+
38+ const mutation = useMutation < ResponseType , ApiError , PostReviewParams > ( {
39+ mutationFn : ( params : PostReviewParams ) =>
40+ postReview ( {
41+ gatheringId : params . gatheringId ,
42+ point : params . point ,
43+ reviewText : params . reviewText ,
44+ } ) ,
45+
46+ onSuccess : ( ) => {
47+ onCancel ( ) ;
48+ } ,
49+ onError : ( error : ApiError ) => {
50+ // eslint-disable-next-line no-console
51+ console . error ( error ) ;
52+ } ,
53+ } ) ;
54+
55+ const clickSubmit = ( data : FormValues ) => {
56+ // eslint-disable-next-line no-console
57+ console . log ( data ) ;
58+ mutation . mutate ( { gatheringId, point : data . score , reviewText : data . reviewText } ) ;
3059 } ;
3160
3261 return (
33- < form className = "flex h-[308px] w-[472px] flex-col justify-between gap-[24px]" >
62+ < form
63+ onSubmit = { handleSubmit ( clickSubmit ) }
64+ className = "flex h-auto w-[288px] flex-col justify-between gap-[20px] md:h-[302px] md:w-[472px]"
65+ >
3466 < ButtonHearts onChange = { handleScoreChange } />
3567 < Textarea
3668 placeholder = "남겨주신 리뷰는 프로그램 운영 및 다른 회원 분들께 큰 도움이 됩니다."
37- inputClassNames = "w-[471px] h-[120px] bg-gray-50 text-gray-400 "
69+ inputClassNames = "md: w-[472px] md: h-[120px] bg-gray-50 text-gray-900 w-[288px] h-[240px] rounded-[12px] "
3870 value = { textReview }
3971 onChange = { handleTextChange }
4072 register = { register ( 'reviewText' ) }
@@ -53,15 +85,18 @@ export default function ReviewForm({ onCancel }: ReviewProps) {
5385 } ,
5486 } }
5587 />
56- < input type = "hidden" value = { point } { ... register ( 'score' ) } />
57- < div className = "font-base flex justify-between gap-[16px ] font-semibold" >
88+ < input type = "hidden" value = { scoreValue } />
89+ < div className = "font-base flex w-full justify-between gap-[8px ] font-semibold md:gap-[16px] " >
5890 < Button
5991 onClick = { onCancel }
60- className = "h-[44px] w-[228px ] border border-blue-500 bg-white text-blue-500"
92+ className = "h-[44px] w-full rounded-[12px ] border border-blue-500 bg-white text-blue-500"
6193 >
6294 취소
6395 </ Button >
64- < Button type = "submit" className = "h-[44px] w-[228px] border-none bg-blue-500 text-white" >
96+ < Button
97+ type = "submit"
98+ className = "h-[44px] w-full rounded-[12px] border-none bg-blue-500 text-white"
99+ >
65100 리뷰 등록
66101 </ Button >
67102 </ div >
0 commit comments