- 조사매물 조회 시 로그인 여부 확인 로직 서버 사이드로 이동 - 에러 핸들링 추가 - 조사매물 수정/작성 페이지 컴포넌트 리팩토링 - 조사매물 작성 후 제출 버튼 클릭 시 node 삽입 오류 해결
149 lines
4.4 KiB
TypeScript
149 lines
4.4 KiB
TypeScript
import { InquiryList, Inquiry, InquirySaveResponse, CommonCode } from '@/types/Inquiry'
|
|
import { useAxios } from '@/hooks/useAxios'
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { useInquiryFilterStore } from '@/store/inquiryFilterStore'
|
|
import { useMemo } from 'react'
|
|
import { useSessionStore } from '@/store/session'
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
export function useInquiry(
|
|
qnoNo?: number,
|
|
compCd?: string,
|
|
): {
|
|
inquiryList: InquiryList[]
|
|
isLoadingInquiryList: boolean
|
|
inquiryDetail: Inquiry | null
|
|
isLoadingInquiryDetail: boolean
|
|
isSavingInquiry: boolean
|
|
saveInquiry: (formData: FormData) => Promise<InquirySaveResponse>
|
|
downloadFile: (encodeFileNo: number, srcFileNm: string) => Promise<Blob | null>
|
|
commonCodeList: CommonCode[]
|
|
} {
|
|
const queryClient = useQueryClient()
|
|
const { inquiryListRequest, offset } = useInquiryFilterStore()
|
|
const { session } = useSessionStore()
|
|
const { axiosInstance } = useAxios()
|
|
const router = useRouter()
|
|
|
|
const errorRouter = (error: any) => {
|
|
const status = error.response?.status
|
|
alert(error.response?.data.error)
|
|
switch (status) {
|
|
// session 없는 경우
|
|
case 401:
|
|
router.replace('/login')
|
|
break
|
|
// 조회 권한 없는 경우
|
|
case 403:
|
|
router.replace('/inquiry/list')
|
|
break
|
|
// 데이터 DB상 존재하지 않는 경우
|
|
case 404:
|
|
router.replace('/inquiry/list')
|
|
break
|
|
// 서버 오류
|
|
case 500:
|
|
router.back()
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
const { data: inquiryList, isLoading: isLoadingInquiryList } = useQuery({
|
|
queryKey: ['inquiryList', inquiryListRequest, offset],
|
|
queryFn: async () => {
|
|
try {
|
|
const resp = await axiosInstance(null).get<{ data: InquiryList[] }>(`/api/qna/list`, {
|
|
params: { inquiryListRequest, startRow: offset, endRow: offset + 9 },
|
|
})
|
|
return resp.data.data
|
|
} catch (error: any) {
|
|
errorRouter(error)
|
|
return []
|
|
}
|
|
},
|
|
enabled: !!inquiryListRequest,
|
|
})
|
|
|
|
const inquriyListData = useMemo(() => {
|
|
if (isLoadingInquiryList) {
|
|
return { inquiryList: [] }
|
|
}
|
|
return {
|
|
inquiryList: inquiryList ?? [],
|
|
}
|
|
}, [inquiryList, isLoadingInquiryList])
|
|
|
|
const { data: inquiryDetail, isLoading: isLoadingInquiryDetail } = useQuery({
|
|
queryKey: ['inquiryDetail', qnoNo, compCd, session?.userId],
|
|
queryFn: async () => {
|
|
try {
|
|
const resp = await axiosInstance(null).get<{ data: Inquiry }>(`/api/qna/detail`, {
|
|
params: { qnoNo, compCd, langCd: 'JA', loginId: session?.userId ?? '' },
|
|
})
|
|
return resp.data.data
|
|
} catch (error: any) {
|
|
errorRouter(error)
|
|
return null
|
|
}
|
|
},
|
|
enabled: qnoNo !== undefined && compCd !== undefined,
|
|
})
|
|
|
|
const { mutateAsync: saveInquiry, isPending: isSavingInquiry } = useMutation({
|
|
mutationFn: async (formData: FormData) => {
|
|
const resp = await axiosInstance(null).post<{ data: InquirySaveResponse }>('/api/qna/save', formData)
|
|
return resp.data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['inquiryList'] })
|
|
},
|
|
onError: (error: any) => {
|
|
errorRouter(error)
|
|
},
|
|
})
|
|
|
|
const downloadFile = async (encodeFileNo: number, srcFileNm: string) => {
|
|
try {
|
|
const resp = await fetch(`/api/qna/file?encodeFileNo=${encodeFileNo}&srcFileNm=${srcFileNm}`)
|
|
|
|
const blob = await resp.blob()
|
|
const url = URL.createObjectURL(blob)
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
a.download = srcFileNm
|
|
document.body.appendChild(a)
|
|
a.click()
|
|
document.body.removeChild(a)
|
|
URL.revokeObjectURL(url)
|
|
|
|
return blob
|
|
} catch (error) {
|
|
alert('ファイルのダウンロードに失敗しました')
|
|
return null
|
|
}
|
|
}
|
|
|
|
const { data: commonCodeList, isLoading: isLoadingCommonCodeList } = useQuery({
|
|
queryKey: ['commonCodeList'],
|
|
queryFn: async () => {
|
|
const resp = await axiosInstance(null).get<{ data: CommonCode[] }>(`/api/qna`)
|
|
return resp.data
|
|
},
|
|
staleTime: Infinity,
|
|
gcTime: Infinity,
|
|
})
|
|
|
|
return {
|
|
inquiryList: inquriyListData.inquiryList,
|
|
inquiryDetail: inquiryDetail ?? null,
|
|
isLoadingInquiryList,
|
|
isLoadingInquiryDetail,
|
|
isSavingInquiry,
|
|
saveInquiry,
|
|
downloadFile,
|
|
commonCodeList: commonCodeList?.data ?? [],
|
|
}
|
|
}
|