Skip to content

Commit 59eaf95

Browse files
authored
Merge pull request #161 from CodeitFESI4-Team1/Feat/115/reviewing-form-api
Feat/115/reviewing form api
2 parents 078c749 + 9d5a6ef commit 59eaf95

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

src/_apis/review/review-apis.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fetchApi } from '@/src/utils/api';
2+
3+
export interface PostReviewParams {
4+
gatheringId?: number;
5+
point: number;
6+
reviewText: string;
7+
}
8+
9+
export async function postReview(params: PostReviewParams): Promise<ResponseType> {
10+
const { gatheringId, point, reviewText } = params;
11+
12+
const response = await fetchApi<ResponseType>(`/api/review/${gatheringId}`, {
13+
method: 'POST',
14+
headers: {
15+
'Content-Type': 'application/json',
16+
},
17+
body: JSON.stringify({ rate: point, comment: reviewText }),
18+
});
19+
return response;
20+
}

src/components/my-page/reviewing-modal/review-form.tsx

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useState } from 'react';
2-
import { useForm } from 'react-hook-form';
2+
import { useController, useForm } from 'react-hook-form';
33
import { 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';
47
import Textarea from '@/src/components/common/input/textarea';
58
import 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>

src/components/my-page/reviewing-modal/reviewing-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function ReviewingModal({ gatheringId, opened, close }: Reviewing
3131
},
3232
}}
3333
>
34-
<ReviewForm onCancel={close} />
34+
<ReviewForm gatheringId={gatheringId} onCancel={close} />
3535
</Modal>
3636
);
3737
}

0 commit comments

Comments
 (0)