102 lines
3.7 KiB
TypeScript
102 lines
3.7 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useMounted } from "@kri/common/hooks";
|
|
import AppPageTitle from "@/components/app-shell/AppPageTitle";
|
|
import { LoadingOverlayAdapter } from "@/components/app-shell/LoadingOverlayAdapter";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useSelectedProject } from "@/hooks/useSelectedProject";
|
|
import { useProjectInfoQuery, useSurveyRedirectUrlQuery } from "@/query/project";
|
|
import { SurveyType } from "@/types/project";
|
|
import { SURVEY_TYPE_LABEL } from "@/const/projectConst";
|
|
|
|
const SurveyProgressPage = () => {
|
|
const mounted = useMounted();
|
|
const { selectedProject } = useSelectedProject();
|
|
const openedForRef = useRef<string | null>(null);
|
|
|
|
const prjctIdx = selectedProject?.prjctIdx ?? 0;
|
|
const enabled = mounted && prjctIdx > 0;
|
|
|
|
const projectInfoQuery = useProjectInfoQuery(prjctIdx, enabled);
|
|
const surveys = projectInfoQuery.data?.surveys ?? [];
|
|
|
|
const [activeSurveyIdx, setActiveSurveyIdx] = useState<number | null>(null);
|
|
const [openedUrl, setOpenedUrl] = useState<string | null>(null);
|
|
|
|
const selectedSurvey = surveys.find((s) => s.prjctSurveyIdx === activeSurveyIdx) ?? null;
|
|
const surveyTy = selectedSurvey?.surveyTy ?? SurveyType.NONE;
|
|
|
|
const isKonKiwi = surveyTy === SurveyType.KON || surveyTy === SurveyType.KIWI;
|
|
const isKiwi = surveyTy === SurveyType.KIWI ? 1 : 0;
|
|
const pkey = selectedSurvey?.surveyPrjctCd ?? "";
|
|
|
|
const redirectUrlEnabled = enabled && !!selectedSurvey && isKonKiwi && !!pkey;
|
|
const redirectUrlQuery = useSurveyRedirectUrlQuery(isKiwi, pkey, redirectUrlEnabled);
|
|
|
|
const targetUrl = !selectedSurvey
|
|
? null
|
|
: isKonKiwi
|
|
? (redirectUrlQuery.data ?? null)
|
|
: surveyTy === SurveyType.EXTERNAL
|
|
? (selectedSurvey.surveyDashboardUrl || null)
|
|
: null;
|
|
|
|
useEffect(() => {
|
|
if (!targetUrl || openedForRef.current === targetUrl) return;
|
|
openedForRef.current = targetUrl;
|
|
window.open(targetUrl, "_blank");
|
|
setOpenedUrl(targetUrl);
|
|
}, [targetUrl]);
|
|
|
|
const isLoading = !mounted || projectInfoQuery.isLoading || (redirectUrlEnabled && redirectUrlQuery.isLoading);
|
|
|
|
if (isLoading) return <LoadingOverlayAdapter force />;
|
|
|
|
return (
|
|
<div className="space-y-6 text-sm">
|
|
<AppPageTitle titleText="설문 현황" />
|
|
|
|
{surveys.length === 0 ? (
|
|
<p className="text-muted-foreground">연결된 설문이 없습니다.</p>
|
|
) : (
|
|
<div className="max-w-[600px] space-y-2">
|
|
{surveys.map((survey) => {
|
|
const hasReport = survey.surveyTy !== SurveyType.NONE;
|
|
return (
|
|
<div
|
|
key={survey.prjctSurveyIdx ?? survey.surveyOrder}
|
|
className="flex items-center justify-between rounded-lg border bg-white p-4"
|
|
>
|
|
<div>
|
|
<p className="font-semibold">{survey.surveyName}</p>
|
|
<p className="text-xs text-muted-foreground">{SURVEY_TYPE_LABEL[survey.surveyTy]}</p>
|
|
</div>
|
|
<Button
|
|
type="button"
|
|
disabled={!hasReport}
|
|
onClick={() => setActiveSurveyIdx(survey.prjctSurveyIdx ?? null)}
|
|
>
|
|
바로가기
|
|
</Button>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{openedUrl ? (
|
|
<p>
|
|
새 창에서 열립니다. 열리지 않았다면{" "}
|
|
<a href={openedUrl} target="_blank" rel="noreferrer" className="text-blue-600 underline">
|
|
여기
|
|
</a>
|
|
를 클릭해 주세요.
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SurveyProgressPage;
|