-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/115/reviewing form api #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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<ResponseType> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { gatheringId, point, reviewText } = params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await fetchApi<ResponseType>(`/api/review/${gatheringId}`, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Content-Type': 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: JSON.stringify({ rate: point, comment: reviewText }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion API ์์ฒญ ๋ณธ๋ฌธ์ ํ๋๋ช ๋ถ์ผ์น ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ ๊ฐ์ ํ์
๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค: export async function postReview(params: PostReviewParams): Promise<ResponseType> {
const { gatheringId, point, reviewText } = params;
+ try {
const response = await fetchApi<ResponseType>(`/api/review/${gatheringId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
- body: JSON.stringify({ rate: point, comment: reviewText }),
+ body: JSON.stringify({ point, reviewText }),
});
return response;
+ } catch (error) {
+ console.error('๋ฆฌ๋ทฐ ๋ฑ๋ก ์ค ์ค๋ฅ ๋ฐ์:', error);
+ throw new Error('๋ฆฌ๋ทฐ๋ฅผ ๋ฑ๋กํ๋ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.');
+ }
}๋ํ, API ์๋ต ํ์
์ ๋ช
์์ ์ผ๋ก ์ ์ํ๊ณ ๐ Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ์ญ์์ค: interface ReviewFormProps {
- gatheringId?: number;
+ gatheringId: number;
onCancel: () => void;
}
|
||
| onCancel: () => void; | ||
| } | ||
|
|
||
| export default function ReviewForm({ onCancel }: ReviewProps) { | ||
| const { register, handleSubmit } = useForm<FormValues>(); | ||
| export default function ReviewForm({ gatheringId, onCancel }: ReviewFormProps) { | ||
| const { register, handleSubmit, control } = useForm<FormValues>(); | ||
| const [textReview, setTextReview] = useState<string>(''); | ||
| const [point, setPoint] = useState<number>(0); | ||
|
|
||
| // TODO : ์ฃผ์ ๋ถ๋ถ(api ์ฐ๊ฒฐ) ์์ | ||
| // TODO : form์ ๋ฃ๊ธฐ: onSubmit={handleSubmit(clickSubmit)} | ||
| const { | ||
| field: { value: scoreValue, onChange: setScore }, | ||
| } = useController({ name: 'score', control, defaultValue: 0 }); | ||
|
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ ์(score) ํ๋๊ฐ ํผ์ ์ฌ๋ฐ๋ฅด๊ฒ ๋ฑ๋ก๋์ง ์์์ต๋๋ค ํ์ฌ ์์ ์ ์ํด ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ์ญ์์ค: - const {
- field: { value: scoreValue, onChange: setScore },
- } = useController({ name: 'score', control, defaultValue: 0 });
- <input type="hidden" value={scoreValue} />
+ const { field } = useController({ name: 'score', control, defaultValue: 0 });
+ <input type="hidden" {...field} />Also applies to: 88-88 |
||
|
|
||
| const handleTextChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
| 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<ResponseType, ApiError, PostReviewParams>({ | ||
| 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); | ||
| }, | ||
|
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion ์๋ฌ ๋ฐ์ ์ ์ฌ์ฉ์์๊ฒ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ํ์ญ์์ค ํ์ฌ ์๋ฌ ๋ฐ์ ์ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ฌ์ฉ์์๊ฒ ํ์ํ๊ธฐ ์ํด ์๋ฆผ ๋๋ ์๋ฌ ํ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ๊ฒฝํ์ ํฅ์์ํฌ ์ ์์ต๋๋ค. |
||
| }); | ||
|
|
||
| const clickSubmit = (data: FormValues) => { | ||
| // eslint-disable-next-line no-console | ||
| console.log(data); | ||
| mutation.mutate({ gatheringId, point: data.score, reviewText: data.reviewText }); | ||
| }; | ||
|
|
||
| return ( | ||
| <form className="flex h-[308px] w-[472px] flex-col justify-between gap-[24px]"> | ||
| <form | ||
| onSubmit={handleSubmit(clickSubmit)} | ||
| className="flex h-auto w-[288px] flex-col justify-between gap-[20px] md:h-[302px] md:w-[472px]" | ||
| > | ||
| <ButtonHearts onChange={handleScoreChange} /> | ||
| <Textarea | ||
| placeholder="๋จ๊ฒจ์ฃผ์ ๋ฆฌ๋ทฐ๋ ํ๋ก๊ทธ๋จ ์ด์ ๋ฐ ๋ค๋ฅธ ํ์ ๋ถ๋ค๊ป ํฐ ๋์์ด ๋ฉ๋๋ค." | ||
| inputClassNames="w-[471px] h-[120px] bg-gray-50 text-gray-400" | ||
| inputClassNames="md:w-[472px] md:h-[120px] bg-gray-50 text-gray-900 w-[288px] h-[240px] rounded-[12px]" | ||
| value={textReview} | ||
| onChange={handleTextChange} | ||
| register={register('reviewText')} | ||
|
|
@@ -53,15 +85,18 @@ export default function ReviewForm({ onCancel }: ReviewProps) { | |
| }, | ||
| }} | ||
| /> | ||
| <input type="hidden" value={point} {...register('score')} /> | ||
| <div className="font-base flex justify-between gap-[16px] font-semibold"> | ||
| <input type="hidden" value={scoreValue} /> | ||
| <div className="font-base flex w-full justify-between gap-[8px] font-semibold md:gap-[16px]"> | ||
| <Button | ||
| onClick={onCancel} | ||
| className="h-[44px] w-[228px] border border-blue-500 bg-white text-blue-500" | ||
| className="h-[44px] w-full rounded-[12px] border border-blue-500 bg-white text-blue-500" | ||
| > | ||
| ์ทจ์ | ||
| </Button> | ||
| <Button type="submit" className="h-[44px] w-[228px] border-none bg-blue-500 text-white"> | ||
| <Button | ||
| type="submit" | ||
| className="h-[44px] w-full rounded-[12px] border-none bg-blue-500 text-white" | ||
| > | ||
| ๋ฆฌ๋ทฐ ๋ฑ๋ก | ||
| </Button> | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gatheringId๋ฅผ ํ์ ๋งค๊ฐ๋ณ์๋ก ๋ณ๊ฒฝํด์ผ ํฉ๋๋คgatheringId๊ฐ ์ ํ์ ๋งค๊ฐ๋ณ์(optional)๋ก ์ ์๋์ด ์์ง๋ง, API ์๋ํฌ์ธํธ URL์์๋ ํ์๋ก ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ ๋ฐํ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค.๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
export interface PostReviewParams { - gatheringId?: number; + gatheringId: number; point: number; reviewText: string; }๐ Committable suggestion