-
Notifications
You must be signed in to change notification settings - Fork 0
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
๐ ํผ ์์ฑ api ํต์ ์งํ #85
Conversation
๋ณ๊ฒฝ ์ฌํญ ๋ถ์๊ฐ์์ด ํ ๋ฆฌํ์คํธ๋ ํผ ์์ฑ ๋ฐ ์ ์ถ ํ๋ก์ธ์ค์ ๊ด๋ จ๋ ์ฌ๋ฌ ํ์ผ์ ๋ํ ์ค์ํ ๋ณ๊ฒฝ ์ฌํญ์ ํฌํจํ๊ณ ์์ต๋๋ค. ์๋ก์ด API ๋ผ์ฐํธ, ํผ ์์ฑ API ํจ์, ์ปค์คํ React ํ , ๊ทธ๋ฆฌ๊ณ ํผ ์ ์ถ ์ปดํฌ๋ํธ์ ๋ก์ง์ด ์ ๋ฐ์ดํธ๋์์ต๋๋ค. ๋ณ๊ฒฝ ๋ด์ฉ
๊ด๋ จ ๊ฐ๋ฅ์ฑ ์๋ PR
์ถ์ฒ ๋ฆฌ๋ทฐ์ด
์ํ์ค ๋ค์ด์ด๊ทธ๋จsequenceDiagram
participant Client
participant CreateFormComponent
participant useCreateForm
participant CreateFormAPI
participant BackendServer
Client->>CreateFormComponent: ํผ ๋ฐ์ดํฐ ์
๋ ฅ
CreateFormComponent->>useCreateForm: createForm ๋ฎคํ
์ด์
ํธ์ถ
useCreateForm->>CreateFormAPI: ํผ ๋ฐ์ดํฐ ์ ์ก
CreateFormAPI->>BackendServer: POST ์์ฒญ
BackendServer-->>CreateFormAPI: ์๋ต
CreateFormAPI-->>useCreateForm: ์ฑ๊ณต/์คํจ ์ฒ๋ฆฌ
useCreateForm-->>CreateFormComponent: ์๋ฆผ ๋ฐ ๋ผ์ฐํ
์ (ํ ๋ผ์ ๋ ธ๋)
โจ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? ๐ชง TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
๐งน Nitpick comments (2)
src/views/create-form/model/useCreateForm.ts (1)
7-34
: Mutation ํ ๊ฐ์ ํ์๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ฌํญ์ ์ ์ํฉ๋๋ค:
export const useCreateForm = ( id: string, navigation: string | null, router: AppRouterInstance, ) => { const queryClient = useQueryClient(); return useMutation({ mutationKey: ['createForm', id, navigation], mutationFn: (formattedData: { informationImage: string; participantType: string; dynamicForm: { title: string; formType: string; jsonData: Record<string, string>; }[]; }) => createForm({ data: formattedData, id }), + retry: 2, + retryDelay: 1000, onSuccess: () => { toast.success('ํผ์ด ์์ฑ๋์์ต๋๋ค.'); formCreateRouter({ id, navigation, router }); queryClient.resetQueries({ queryKey: ['createForm', id, navigation] }); }, - onError: () => { - toast.error('ํผ ์์ฑ์ ์คํจํ์ต๋๋ค.'); + onError: (error) => { + if (axios.isAxiosError(error)) { + toast.error(error.response?.data?.message || 'ํผ ์์ฑ์ ์คํจํ์ต๋๋ค.'); + } else { + toast.error('๋คํธ์ํฌ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.'); + } }, }); };src/views/create-form/ui/createForm/index.tsx (1)
97-101
: ๋ก๋ฉ ์ํ UI ๊ฐ์ ํ์์ ์ถ ๋ฒํผ์ ๋ก๋ฉ ์ํ ํ์๋ฅผ ๊ฐ์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
<Button type="submit" text={isPending ? '์ ์ถ ์ค...' : '๋ค์'} - disabled={isPending || isSuccess} + disabled={isPending || isSuccess || fields.length === 0} + className={isPending ? 'opacity-70 cursor-not-allowed' : ''} />
๐ Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
๐ Files selected for processing (4)
src/app/api/form/[expo_id]/route.ts
(1 hunks)src/views/create-form/api/createForm.ts
(1 hunks)src/views/create-form/model/useCreateForm.ts
(1 hunks)src/views/create-form/ui/createForm/index.tsx
(3 hunks)
export const createForm = async ({ | ||
data, | ||
id, | ||
}: { | ||
data: { | ||
informationImage: string; | ||
participantType: string; | ||
dynamicForm: { | ||
title: string; | ||
formType: string; | ||
jsonData: Record<string, string>; | ||
}[]; | ||
}; | ||
id: string; | ||
}) => { | ||
const response = await axios.post(`/api/form/${id}`, data); | ||
return response; | ||
}; |
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.
๐ ๏ธ Refactor suggestion
API ๊ตฌํ์ ๋ํ ๊ฐ์ ํ์
๋ค์ ์ฌํญ๋ค์ ๊ฐ์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค:
- ์๋ฌ ์ฒ๋ฆฌ ๋ก์ง ์ถ๊ฐ
- ์๋ต ํ์ ์ ์
- ์์ฒญ ํ์์์ ์ค์
- ์์ฒญ ์ทจ์ ์ฒ๋ฆฌ
import axios from 'axios';
+import { AxiosResponse } from 'axios';
+
+interface FormResponse {
+ // TODO: ์๋ต ํ์
์ ์
+}
export const createForm = async ({
data,
id,
}: {
data: {
informationImage: string;
participantType: string;
dynamicForm: {
title: string;
formType: string;
jsonData: Record<string, string>;
}[];
};
id: string;
-}) => {
- const response = await axios.post(`/api/form/${id}`, data);
+}): Promise<AxiosResponse<FormResponse>> => {
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 10000);
+
+ try {
+ const response = await axios.post<FormResponse>(`/api/form/${id}`, data, {
+ signal: controller.signal,
+ });
+ return response;
+ } catch (error) {
+ if (axios.isAxiosError(error)) {
+ throw error;
+ }
+ throw new Error('์ ์ ์๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.');
+ } finally {
+ clearTimeout(timeoutId);
+ }
- return response;
};
๐ Committable suggestion
โผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export const createForm = async ({ | |
data, | |
id, | |
}: { | |
data: { | |
informationImage: string; | |
participantType: string; | |
dynamicForm: { | |
title: string; | |
formType: string; | |
jsonData: Record<string, string>; | |
}[]; | |
}; | |
id: string; | |
}) => { | |
const response = await axios.post(`/api/form/${id}`, data); | |
return response; | |
}; | |
import axios from 'axios'; | |
import { AxiosResponse } from 'axios'; | |
interface FormResponse { | |
// TODO: ์๋ต ํ์ ์ ์ | |
} | |
export const createForm = async ({ | |
data, | |
id, | |
}: { | |
data: { | |
informationImage: string; | |
participantType: string; | |
dynamicForm: { | |
title: string; | |
formType: string; | |
jsonData: Record<string, string>; | |
}[]; | |
}; | |
id: string; | |
}): Promise<AxiosResponse<FormResponse>> => { | |
const controller = new AbortController(); | |
const timeoutId = setTimeout(() => controller.abort(), 10000); | |
try { | |
const response = await axios.post<FormResponse>(`/api/form/${id}`, data, { | |
signal: controller.signal, | |
}); | |
return response; | |
} catch (error) { | |
if (axios.isAxiosError(error)) { | |
throw error; | |
} | |
throw new Error('์ ์ ์๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.'); | |
} finally { | |
clearTimeout(timeoutId); | |
} | |
}; |
export async function POST( | ||
request: Request, | ||
{ params }: { params: { expo_id: number } }, | ||
) { | ||
const body = await request.json(); | ||
const { expo_id } = params; | ||
const cookieStore = cookies(); | ||
const accessToken = cookieStore.get('accessToken')?.value; | ||
const config = accessToken | ||
? { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
: {}; | ||
try { | ||
const response = await apiClient.post(`/form/${expo_id}`, body, config); | ||
return NextResponse.json(response.data); | ||
} catch (error) { | ||
const axiosError = error as AxiosError<{ message: string }>; | ||
|
||
const status = axiosError.response?.status; | ||
const message = axiosError.response?.data?.message; | ||
|
||
return NextResponse.json({ error: message }, { status }); | ||
} | ||
} |
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.
๐ ๏ธ Refactor suggestion
API ์๋ํฌ์ธํธ ๋ณด์ ๋ฐ ํ์ ์์ ์ฑ ๊ฐ์ ํ์
๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ฌํญ์ ์ ์ํฉ๋๋ค:
+import { z } from 'zod';
+
+const FormDataSchema = z.object({
+ informationImage: z.string(),
+ participantType: z.string(),
+ dynamicForm: z.array(
+ z.object({
+ title: z.string(),
+ formType: z.string(),
+ jsonData: z.record(z.string())
+ })
+ )
+});
+
export async function POST(
request: Request,
{ params }: { params: { expo_id: number } },
) {
const body = await request.json();
+
+ const validationResult = FormDataSchema.safeParse(body);
+ if (!validationResult.success) {
+ return NextResponse.json(
+ { error: '์๋ชป๋ ์์ฒญ ๋ฐ์ดํฐ์
๋๋ค.' },
+ { status: 400 }
+ );
+ }
+
const { expo_id } = params;
const cookieStore = cookies();
const accessToken = cookieStore.get('accessToken')?.value;
+
+ if (!accessToken) {
+ return NextResponse.json(
+ { error: '์ธ์ฆ์ด ํ์ํฉ๋๋ค.' },
+ { status: 401 }
+ );
+ }
+
const config = accessToken
? {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
: {};
try {
const response = await apiClient.post(`/form/${expo_id}`, body, config);
return NextResponse.json(response.data);
} catch (error) {
const axiosError = error as AxiosError<{ message: string }>;
const status = axiosError.response?.status;
- const message = axiosError.response?.data?.message;
+ const message = axiosError.response?.status === 500
+ ? '์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.'
+ : axiosError.response?.data?.message;
return NextResponse.json({ error: message }, { status });
}
}
๐ Committable suggestion
โผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export async function POST( | |
request: Request, | |
{ params }: { params: { expo_id: number } }, | |
) { | |
const body = await request.json(); | |
const { expo_id } = params; | |
const cookieStore = cookies(); | |
const accessToken = cookieStore.get('accessToken')?.value; | |
const config = accessToken | |
? { | |
headers: { | |
Authorization: `Bearer ${accessToken}`, | |
}, | |
} | |
: {}; | |
try { | |
const response = await apiClient.post(`/form/${expo_id}`, body, config); | |
return NextResponse.json(response.data); | |
} catch (error) { | |
const axiosError = error as AxiosError<{ message: string }>; | |
const status = axiosError.response?.status; | |
const message = axiosError.response?.data?.message; | |
return NextResponse.json({ error: message }, { status }); | |
} | |
} | |
import { z } from 'zod'; | |
const FormDataSchema = z.object({ | |
informationImage: z.string(), | |
participantType: z.string(), | |
dynamicForm: z.array( | |
z.object({ | |
title: z.string(), | |
formType: z.string(), | |
jsonData: z.record(z.string()) | |
}) | |
) | |
}); | |
export async function POST( | |
request: Request, | |
{ params }: { params: { expo_id: number } }, | |
) { | |
const body = await request.json(); | |
const validationResult = FormDataSchema.safeParse(body); | |
if (!validationResult.success) { | |
return NextResponse.json( | |
{ error: '์๋ชป๋ ์์ฒญ ๋ฐ์ดํฐ์ ๋๋ค.' }, | |
{ status: 400 } | |
); | |
} | |
const { expo_id } = params; | |
const cookieStore = cookies(); | |
const accessToken = cookieStore.get('accessToken')?.value; | |
if (!accessToken) { | |
return NextResponse.json( | |
{ error: '์ธ์ฆ์ด ํ์ํฉ๋๋ค.' }, | |
{ status: 401 } | |
); | |
} | |
const config = accessToken | |
? { | |
headers: { | |
Authorization: `Bearer ${accessToken}`, | |
}, | |
} | |
: {}; | |
try { | |
const response = await apiClient.post(`/form/${expo_id}`, body, config); | |
return NextResponse.json(response.data); | |
} catch (error) { | |
const axiosError = error as AxiosError<{ message: string }>; | |
const status = axiosError.response?.status; | |
const message = axiosError.response?.status === 500 | |
? '์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.' | |
: axiosError.response?.data?.message; | |
return NextResponse.json({ error: message }, { status }); | |
} | |
} |
const onSubmit = (data: FormValues) => { | ||
const formattedData = { | ||
informationImage: '', | ||
participantType: navigation?.toUpperCase() || 'STANDARD', | ||
dynamicForm: data.questions.map((question) => ({ | ||
title: question.title, | ||
formType: question.formType, | ||
jsonData: question.options.reduce( | ||
(acc, option, index) => { | ||
acc[(index + 1).toString()] = option.value; | ||
return acc; | ||
}, | ||
{} as Record<string, string>, | ||
), | ||
})), | ||
}; | ||
console.log(formattedData); | ||
formCreateRouter({ id, navigation, router }); | ||
createForm(formattedData); |
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.
ํผ ์ ์ถ ๋ก์ง ๊ฐ์ ํ์
๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ์ ๋ค์ด ๋ฐ๊ฒฌ๋์์ต๋๋ค:
- ํ๋ก๋์ ์ฝ๋์ console.log ์กด์ฌ
- ํผ ์ ํจ์ฑ ๊ฒ์ฌ ๋ถ์ฌ
- informationImage๊ฐ ๋น ๋ฌธ์์ด๋ก ์ ์ก๋จ
- ๋ก๋ฉ ์ํ์ ๋ํ UI ํผ๋๋ฐฑ ๋ถ์กฑ
- const onSubmit = (data: FormValues) => {
+ const onSubmit = async (data: FormValues) => {
+ if (data.questions.length === 0) {
+ toast.error('์ต์ ํ๋์ ์ง๋ฌธ์ ์ถ๊ฐํด์ฃผ์ธ์.');
+ return;
+ }
+
const formattedData = {
- informationImage: '',
+ informationImage: '๊ธฐ๋ณธ ์ด๋ฏธ์ง URL', // TODO: ์ด๋ฏธ์ง ์
๋ก๋ ๊ธฐ๋ฅ ๊ตฌํ ํ์
participantType: navigation?.toUpperCase() || 'STANDARD',
dynamicForm: data.questions.map((question) => ({
title: question.title,
formType: question.formType,
jsonData: question.options.reduce(
(acc, option, index) => {
acc[(index + 1).toString()] = option.value;
return acc;
},
{} as Record<string, string>,
),
})),
};
- console.log(formattedData);
createForm(formattedData);
};
Committable suggestion skipped: line range outside the PR's diff.
๐ก ๋ฐฐ๊ฒฝ ๋ฐ ๊ฐ์
ํผ ์์ฑ api ํต์ ์งํ
๐ ์์ ๋ด์ฉ
๐ธ ๊ธฐํ
Summary by CodeRabbit
์๋ก์ด ๊ธฐ๋ฅ
๋ฒ๊ทธ ์์
๊ฐ์ ์ฌํญ