fix: fix data not loading error when navigating PDF pages without not login

- 로그인 하지 않고 pdf 링크로 이동 시 데이터 로드 되지 않는 문제 해결
- 로그인 하지 않았을 때 pdf 다운로드 이후 메인 페이지로 이동하도록 수정
This commit is contained in:
Dayoung 2025-06-09 10:37:48 +09:00
parent a0beb8eca3
commit 763c9d37cf
6 changed files with 41 additions and 30 deletions

View File

@ -15,6 +15,7 @@ interface SessionParams {
storeId: string | null
builderId: string | null
isLoggedIn: string | null
isPdf: boolean | null
}
const checkT01Role = (survey: Survey): boolean => survey.SRL_NO !== '一時保存'
@ -74,6 +75,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
storeId: searchParams.get('storeId'),
builderId: searchParams.get('builderId'),
isLoggedIn: searchParams.get('isLoggedIn'),
isPdf: searchParams.get('isPdf') === 'true' ? true : false,
}
// @ts-ignore
const survey = await prisma.SD_SURVEY_SALES_BASIC_INFO.findFirst({
@ -84,7 +86,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
DETAIL_INFO: true,
},
})
if (checkRole(survey, sessionParams)) {
if (sessionParams.isPdf || checkRole(survey, sessionParams)) {
return NextResponse.json(survey)
} else {
return NextResponse.json({ error: '該当物件の照会権限がありません。' }, { status: 403 })

View File

@ -6,20 +6,27 @@ import { useParams, useRouter } from 'next/navigation'
import { useSurvey } from '@/hooks/useSurvey'
import { radioEtcData, roofMaterial, selectBoxOptions, supplementaryFacilities } from '../survey-sale/detail/RoofForm'
import { useSpinnerStore } from '@/store/spinnerStore'
import { useSessionStore } from '@/store/session'
export default function SurveySaleDownloadPdf() {
const params = useParams()
const id = params.id
const router = useRouter()
const { surveyDetail, isLoadingSurveyDetail } = useSurvey(Number(id))
const { surveyDetail, isLoadingSurveyDetail } = useSurvey(Number(id), true)
const { setIsShow } = useSpinnerStore()
const { session } = useSessionStore()
const targetRef = useRef<HTMLDivElement>(null)
const isGeneratedRef = useRef(false)
useEffect(() => {
if (isLoadingSurveyDetail || !surveyDetail || isGeneratedRef.current) return
if (isLoadingSurveyDetail || isGeneratedRef.current) return
if (surveyDetail === null) {
alert('データが見つかりません。')
router.replace('/')
return
}
isGeneratedRef.current = true
handleDownPdf()
}, [surveyDetail?.id, isLoadingSurveyDetail])
@ -48,11 +55,19 @@ export default function SurveySaleDownloadPdf() {
},
}
generatePDF(targetRef, options).then(() => {
setIsShow(false)
router.replace(`/survey-sale/${id}`)
alert('PDFの生成が完了しました。 ポップアップウィンドウからダウンロードしてください。')
})
generatePDF(targetRef, options)
.then(() => {
setIsShow(false)
if (session?.isLoggedIn) {
router.replace(`/survey-sale/${id}`)
} else {
router.replace('/')
}
alert('PDFの生成が完了しました。 ポップアップウィンドウからダウンロードしてください。')
})
.catch((error: any) => {
console.error('error', error)
})
}
return (

View File

@ -4,15 +4,12 @@ import { useSurvey } from '@/hooks/useSurvey'
import { useParams, useRouter } from 'next/navigation'
import { useEffect } from 'react'
import DetailForm from './DetailForm'
import { useSessionStore } from '@/store/session'
export default function DataTable() {
const params = useParams()
const id = params.id
const router = useRouter()
const { session } = useSessionStore()
useEffect(() => {
if (Number.isNaN(Number(id))) {
alert('間違ったアプローチです。')

View File

@ -74,7 +74,7 @@ export default function DetailForm() {
const modeset = Number(routeId) ? 'READ' : idParam ? 'EDIT' : 'CREATE'
const id = Number(routeId) ? Number(routeId) : Number(idParam)
const { surveyDetail, isLoadingSurveyDetail, validateSurveyDetail } = useSurvey(Number(id))
const { surveyDetail, isLoadingSurveyDetail } = useSurvey(Number(id))
const { session } = useSessionStore()
const [mode, setMode] = useState<Mode>(modeset)
@ -103,9 +103,8 @@ export default function DetailForm() {
}))
}, [session?.isLoggedIn])
// 설문 데이터 로딩 및 업데이트
useEffect(() => {
if (isLoadingSurveyDetail || !session?.isLoggedIn) return
if (isLoadingSurveyDetail) return
if (surveyDetail && (mode === 'EDIT' || mode === 'READ')) {
const { id, uptDt, regDt, detailInfo, ...rest } = surveyDetail
setBasicInfoData((prev) => ({
@ -116,12 +115,9 @@ export default function DetailForm() {
if (detailInfo) {
const { id, uptDt, regDt, basicInfoId, ...rest } = detailInfo
setRoofInfoData(rest)
if (validateSurveyDetail(rest).trim() !== '') {
// validation logic here if needed
}
}
}
}, [mode, session?.isLoggedIn, isLoadingSurveyDetail])
}, [mode, isLoadingSurveyDetail, surveyDetail])
const data = {
basic: basicInfoData,

View File

@ -27,12 +27,7 @@ export default function ListTable() {
}, [pathname])
useEffect(() => {
if (!session.isLoggedIn || isLoadingSurveyList) return
// if ('status' in surveyList && surveyList.status === 403) {
// alert('権限がありません。')
// router.push('/survey-sale')
// return
// }
if (isLoadingSurveyList) return
if ('count' in surveyList && surveyList.count > 0) {
if (offset > 0) {
setHeldSurveyList((prev) => [...prev, ...surveyList.data])
@ -44,7 +39,7 @@ export default function ListTable() {
setHeldSurveyList([])
setHasMore(false)
}
}, [surveyList, offset, session.isLoggedIn])
}, [surveyList, offset, isLoadingSurveyList])
const handleDetailClick = (id: number) => {
router.push(`/survey-sale/${id}`)

View File

@ -55,7 +55,10 @@ type ZipCode = {
kana3: string
}
export function useSurvey(id?: number): {
export function useSurvey(
id?: number,
isPdf?: boolean,
): {
surveyList: { data: SurveyBasicInfo[]; count: number } | {}
surveyDetail: SurveyBasicInfo | null
isLoadingSurveyList: boolean
@ -102,6 +105,9 @@ export function useSurvey(id?: number): {
return false
}
}
if (isPdf) {
return true
}
alert('ログインしていません。')
return false
}
@ -114,7 +120,7 @@ export function useSurvey(id?: number): {
queryKey: ['survey', 'list', keyword, searchOption, isMySurvey, sort, offset, session?.storeNm, session?.builderId, session?.role],
queryFn: async () => {
if (!checkSession()) {
router.replace('/')
router.replace('/login')
return { data: [], count: 0 }
}
const resp = await axiosInstance(null).get<{ data: SurveyBasicInfo[]; count: number }>('/api/survey-sales', {
@ -131,7 +137,6 @@ export function useSurvey(id?: number): {
})
return resp.data
},
enabled: session?.isLoggedIn,
})
const surveyData = useMemo(() => {
if (!surveyListData) return { count: 0, data: [] }
@ -144,7 +149,7 @@ export function useSurvey(id?: number): {
queryKey: ['survey', id],
queryFn: async () => {
if (!checkSession()) {
router.replace('/survey-sale')
router.replace('/login')
return null
}
if (id === 0 || id === undefined) return null
@ -155,6 +160,7 @@ export function useSurvey(id?: number): {
storeId: session?.storeId,
builderId: session?.builderId,
isLoggedIn: session?.isLoggedIn,
isPdf: isPdf,
},
})
return resp.data
@ -164,7 +170,7 @@ export function useSurvey(id?: number): {
return null
}
},
enabled: id !== 0 && id !== undefined && session?.isLoggedIn,
enabled: id !== 0 && id !== undefined,
})
const { mutateAsync: createSurvey, isPending: isCreatingSurvey } = useMutation({