-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from School-of-Company/api/create-form
🔀 폼 생성 api 통신 진행
- Loading branch information
Showing
4 changed files
with
114 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('폼 생성에 실패했습니다.'); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters