-
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
Changes from all 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,32 @@ | ||
import { AxiosError } from 'axios'; | ||
import { cookies } from 'next/headers'; | ||
import { NextResponse } from 'next/server'; | ||
import { apiClient } from '@/shared/libs/apiClient'; | ||
|
||
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 }); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import axios from 'axios'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+3
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 ๊ตฌํ์ ๋ํ ๊ฐ์ ํ์ ๋ค์ ์ฌํญ๋ค์ ๊ฐ์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค:
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
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'; | ||
import { toast } from 'react-toastify'; | ||
import { createForm } from '../api/createForm'; | ||
import { formCreateRouter } from './formCreateRouter'; | ||
|
||
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 }), | ||
onSuccess: () => { | ||
toast.success('ํผ์ด ์์ฑ๋์์ต๋๋ค.'); | ||
formCreateRouter({ id, navigation, router }); | ||
queryClient.resetQueries({ queryKey: ['createForm', id, navigation] }); | ||
}, | ||
onError: () => { | ||
toast.error('ํผ ์์ฑ์ ์คํจํ์ต๋๋ค.'); | ||
}, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ import { FormValues, Option } from '@/shared/types/create-form/type'; | |
import { Button, PageHeader } from '@/shared/ui'; | ||
import FormContainer from '@/widgets/create-form/ui/FormContainer'; | ||
import { Header } from '@/widgets/layout'; | ||
import { formCreateRouter } from '../../model/formCreateRouter'; | ||
import { selectOptionData } from '../../model/selectOptionData'; | ||
import { useCreateForm } from '../../model/useCreateForm'; | ||
|
||
const CreateForm = ({ id }: { id: string }) => { | ||
const router = useRouter(); | ||
|
@@ -28,21 +28,30 @@ const CreateForm = ({ id }: { id: string }) => { | |
name: 'questions', | ||
}); | ||
|
||
const onSubmit = (data: FormValues) => { | ||
const formattedData = 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>, | ||
), | ||
})); | ||
const { | ||
mutate: createForm, | ||
isPending, | ||
isSuccess, | ||
} = useCreateForm(id, navigation, router); | ||
|
||
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); | ||
Comment on lines
+37
to
+54
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. ํผ ์ ์ถ ๋ก์ง ๊ฐ์ ํ์ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ์ ๋ค์ด ๋ฐ๊ฒฌ๋์์ต๋๋ค:
- 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);
};
|
||
}; | ||
|
||
const navigationTitles: Record<string, string> = { | ||
|
@@ -85,7 +94,11 @@ const CreateForm = ({ id }: { id: string }) => { | |
append({ title: '', formType: 'SENTENCE', options: [] }); | ||
}} | ||
/> | ||
<Button type="submit" text="๋ค์" /> | ||
<Button | ||
type="submit" | ||
text={isPending ? '์ ์ถ ์ค...' : '๋ค์'} | ||
disabled={isPending || isSuccess} | ||
/> | ||
</form> | ||
</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.
๐ ๏ธ Refactor suggestion
API ์๋ํฌ์ธํธ ๋ณด์ ๋ฐ ํ์ ์์ ์ฑ ๊ฐ์ ํ์
๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ฌํญ์ ์ ์ํฉ๋๋ค:
๐ Committable suggestion