diff --git a/src/app/api/survey-sales/[id]/route.ts b/src/app/api/survey-sales/[id]/route.ts index 882079c..6cc460a 100644 --- a/src/app/api/survey-sales/[id]/route.ts +++ b/src/app/api/survey-sales/[id]/route.ts @@ -87,11 +87,11 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{ if (checkRole(survey, sessionParams)) { return NextResponse.json(survey) } else { - return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) + return NextResponse.json({ error: '権限がありません。' }, { status: 403 }) } - } catch (error) { + } catch (error: any) { console.error('Error fetching survey:', error) - return NextResponse.json({ error: 'Failed to fetch survey' }, { status: 500 }) + return NextResponse.json({ error: 'データの取得に失敗しました。' }, { status: 500 }) } } diff --git a/src/components/survey-sale/detail/DetailForm.tsx b/src/components/survey-sale/detail/DetailForm.tsx index 59f9864..2cadf6c 100644 --- a/src/components/survey-sale/detail/DetailForm.tsx +++ b/src/components/survey-sale/detail/DetailForm.tsx @@ -5,7 +5,7 @@ import { useEffect, useState } from 'react' import ButtonForm from './ButtonForm' import BasicForm from './BasicForm' import RoofForm from './RoofForm' -import { useParams, useSearchParams, useRouter } from 'next/navigation' +import { useParams, useSearchParams } from 'next/navigation' import { useSurvey } from '@/hooks/useSurvey' import { useSessionStore } from '@/store/session' @@ -70,7 +70,6 @@ const basicInfoForm: SurveyBasicRequest = { export default function DetailForm() { const idParam = useSearchParams().get('id') const routeId = useParams().id - const router = useRouter() const modeset = Number(routeId) ? 'READ' : idParam ? 'EDIT' : 'CREATE' const id = Number(routeId) ? Number(routeId) : Number(idParam) @@ -102,18 +101,11 @@ export default function DetailForm() { constructionPoint: session.builderNm ?? null, constructionPointId: session.builderNo ?? null, })) - }, [session]) + }, [session?.isLoggedIn]) // 설문 데이터 로딩 및 업데이트 useEffect(() => { if (isLoadingSurveyDetail || !session?.isLoggedIn) return - - if (surveyDetail === null && mode !== 'CREATE') { - alert('権限がありません。') - router.replace('/survey-sale') - return - } - if (surveyDetail && (mode === 'EDIT' || mode === 'READ')) { const { id, uptDt, regDt, detailInfo, ...rest } = surveyDetail setBasicInfoData((prev) => ({ @@ -129,7 +121,7 @@ export default function DetailForm() { } } } - }, [surveyDetail, mode, session?.isLoggedIn, isLoadingSurveyDetail]) + }, [mode, session?.isLoggedIn, isLoadingSurveyDetail]) const data = { basic: basicInfoData, diff --git a/src/hooks/useSurvey.ts b/src/hooks/useSurvey.ts index 0fbf69a..9abb7b8 100644 --- a/src/hooks/useSurvey.ts +++ b/src/hooks/useSurvey.ts @@ -1,10 +1,11 @@ import type { SurveyBasicInfo, SurveyDetailRequest, SurveyRegistRequest } from '@/types/Survey' -import { useMemo } from 'react' +import { useMemo, useEffect } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useSurveyFilterStore } from '@/store/surveyFilterStore' import { useSessionStore } from '@/store/session' import { useAxios } from './useAxios' import { queryStringFormatter } from '@/utils/common-utils' +import { useRouter } from 'next/navigation' export const requiredFields = [ { @@ -75,6 +76,7 @@ export function useSurvey(id?: number): { const { keyword, searchOption, isMySurvey, sort, offset } = useSurveyFilterStore() const { session } = useSessionStore() const { axiosInstance } = useAxios() + const router = useRouter() const { data: surveyListData, @@ -109,9 +111,8 @@ export function useSurvey(id?: number): { const { data: surveyDetail, isLoading: isLoadingSurveyDetail } = useQuery({ queryKey: ['survey', id], queryFn: async () => { - if (id === undefined) throw new Error('id is required') - if (id === null || isNaN(id)) return null - if (session?.isLoggedIn) { + if (!session?.isLoggedIn || id === 0 || id === undefined) return null + try { const resp = await axiosInstance(null).get(`/api/survey-sales/${id}`, { params: { role: session?.role, @@ -121,10 +122,13 @@ export function useSurvey(id?: number): { }, }) return resp.data + } catch (error: any) { + alert(error.response?.data.error) + router.replace('/survey-sale') + return null } - return null }, - enabled: id !== undefined, + enabled: id !== 0 && id !== undefined && session?.isLoggedIn, }) const { mutateAsync: createSurvey, isPending: isCreatingSurvey } = useMutation({