diff --git a/src/_apis/review/review-apis.ts b/src/_apis/review/review-apis.ts new file mode 100644 index 00000000..0483a541 --- /dev/null +++ b/src/_apis/review/review-apis.ts @@ -0,0 +1,20 @@ +import { fetchApi } from '@/src/utils/api'; + +export interface PostReviewParams { + gatheringId?: number; + point: number; + reviewText: string; +} + +export async function postReview(params: PostReviewParams): Promise { + const { gatheringId, point, reviewText } = params; + + const response = await fetchApi(`/api/review/${gatheringId}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ rate: point, comment: reviewText }), + }); + return response; +} diff --git a/src/components/my-page/reviewing-modal/review-form.tsx b/src/components/my-page/reviewing-modal/review-form.tsx index 441e1162..a942ba14 100644 --- a/src/components/my-page/reviewing-modal/review-form.tsx +++ b/src/components/my-page/reviewing-modal/review-form.tsx @@ -1,6 +1,9 @@ import { useState } from 'react'; -import { useForm } from 'react-hook-form'; +import { useController, useForm } from 'react-hook-form'; import { Button } from '@mantine/core'; +import { useMutation } from '@tanstack/react-query'; +import { PostReviewParams, postReview } from '@/src/_apis/review/review-apis'; +import { ApiError } from '@/src/utils/api'; import Textarea from '@/src/components/common/input/textarea'; import ButtonHearts from './button-hearts'; @@ -9,32 +12,61 @@ type FormValues = { score: number; }; -interface ReviewProps { +interface ReviewFormProps { + gatheringId?: number; onCancel: () => void; } -export default function ReviewForm({ onCancel }: ReviewProps) { - const { register, handleSubmit } = useForm(); +export default function ReviewForm({ gatheringId, onCancel }: ReviewFormProps) { + const { register, handleSubmit, control } = useForm(); const [textReview, setTextReview] = useState(''); - const [point, setPoint] = useState(0); - // TODO : 주석 부분(api 연결) 수정 - // TODO : form에 넣기: onSubmit={handleSubmit(clickSubmit)} + const { + field: { value: scoreValue, onChange: setScore }, + } = useController({ name: 'score', control, defaultValue: 0 }); const handleTextChange = (e: React.ChangeEvent) => { - setTextReview(e.target.value); + if (e.target.value.length <= 300) { + setTextReview(e.target.value); + } }; const handleScoreChange = (newScore: number) => { - setPoint(newScore); + setScore(newScore); + }; + + const mutation = useMutation({ + mutationFn: (params: PostReviewParams) => + postReview({ + gatheringId: params.gatheringId, + point: params.point, + reviewText: params.reviewText, + }), + + onSuccess: () => { + onCancel(); + }, + onError: (error: ApiError) => { + // eslint-disable-next-line no-console + console.error(error); + }, + }); + + const clickSubmit = (data: FormValues) => { + // eslint-disable-next-line no-console + console.log(data); + mutation.mutate({ gatheringId, point: data.score, reviewText: data.reviewText }); }; return ( -
+