162 lines
5.1 KiB
TypeScript
162 lines
5.1 KiB
TypeScript
"use client";
|
|
|
|
import React, { useEffect, useMemo, useState } from "react";
|
|
import { Table, TableBody, TableCell, TableRow } from "@common/components";
|
|
import { fetchSwtrmtPrcsZoneAnalysis } from "./swtrmtPrcsZoneAnalysisApi";
|
|
import type { SwtrmtPrcsZoneAnalysisMetric, SwtrmtPrcsZoneAnalysisResponse } from "./swtrmtPrcsZoneAnalysisApi";
|
|
|
|
const analysisRows: { key: keyof NonNullable<SwtrmtPrcsZoneAnalysisResponse["analysis"]>; title: string }[] = [
|
|
{ key: "total", title: "전체" },
|
|
{ key: "combined", title: "합류관" },
|
|
{ key: "rain", title: "우수관" },
|
|
{ key: "sewage", title: "오수관" },
|
|
];
|
|
|
|
const formatValue = (value: unknown, unit = "") => {
|
|
if (value === null || value === undefined || value === "") return "데이터 없음";
|
|
|
|
const numberValue = typeof value === "number" ? value : Number(String(value).replaceAll(",", ""));
|
|
if (!Number.isNaN(numberValue) && Number.isFinite(numberValue)) {
|
|
const formatted = numberValue.toLocaleString("ko-KR", { maximumFractionDigits: 2 });
|
|
return unit ? `${formatted}${unit}` : formatted;
|
|
}
|
|
|
|
return String(value);
|
|
};
|
|
|
|
const getMetric = (
|
|
analysis: SwtrmtPrcsZoneAnalysisResponse["analysis"] | undefined,
|
|
key: keyof NonNullable<SwtrmtPrcsZoneAnalysisResponse["analysis"]>
|
|
): SwtrmtPrcsZoneAnalysisMetric => analysis?.[key] ?? {};
|
|
|
|
const metricGroups: {
|
|
title: string;
|
|
field: keyof SwtrmtPrcsZoneAnalysisMetric;
|
|
}[] = [
|
|
{ title: "평균매설년도(년)", field: "avgInstlYear" },
|
|
{ title: "최장매설년도(년)", field: "oldestInstlYear" },
|
|
{ title: "최신매설년도(년)", field: "latestInstlYear" },
|
|
];
|
|
|
|
const MetricVerticalGrid = ({
|
|
title,
|
|
field,
|
|
analysis,
|
|
isLoading,
|
|
}: {
|
|
title: string;
|
|
field: keyof SwtrmtPrcsZoneAnalysisMetric;
|
|
analysis: SwtrmtPrcsZoneAnalysisResponse["analysis"] | undefined;
|
|
isLoading?: boolean;
|
|
}) => (
|
|
<div className="mb-4 last:mb-0">
|
|
<div className="text-xl mx-3 mb-2 mt-1 font-medium text-label">{title}</div>
|
|
<Table variant="secondary">
|
|
<TableBody className="text-center">
|
|
{analysisRows.map((row) => {
|
|
const metric = getMetric(analysis, row.key);
|
|
return (
|
|
<TableRow key={`${title}-${row.key}`}>
|
|
<TableCell className="font-bold bg-blue-100 text-[13px] w-[120px] whitespace-nowrap">
|
|
{row.title}
|
|
</TableCell>
|
|
<TableCell className="text-[13px]">{isLoading ? "" : formatValue(metric[field])}</TableCell>
|
|
</TableRow>
|
|
);
|
|
})}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
);
|
|
|
|
const SwtrmtPrcsZoneAnalysis = ({
|
|
featureData,
|
|
rawFeatureData,
|
|
}: {
|
|
featureData: any;
|
|
rawFeatureData?: any;
|
|
}) => {
|
|
const [analysisData, setAnalysisData] = useState<SwtrmtPrcsZoneAnalysisResponse | null>(null);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
const requestKey = useMemo(() => {
|
|
const rawValues = rawFeatureData?.values_ ?? rawFeatureData ?? {};
|
|
return [
|
|
rawFeatureData?.getId?.(),
|
|
rawFeatureData?.id_,
|
|
rawFeatureData?.fid,
|
|
rawValues?.fid,
|
|
rawValues?.swtrmt_zone_sn,
|
|
rawValues?.swtrmt_fclt_cd,
|
|
featureData?.fid,
|
|
featureData?.swtrmtZoneSn,
|
|
featureData?.swtrmt_zone_sn,
|
|
featureData?.swtrmtFcltCd,
|
|
featureData?.swtrmt_fclt_cd,
|
|
].filter(Boolean).join("|");
|
|
}, [featureData, rawFeatureData]);
|
|
|
|
useEffect(() => {
|
|
let isMounted = true;
|
|
|
|
const fetchAnalysis = async () => {
|
|
setIsLoading(true);
|
|
setErrorMessage("");
|
|
setAnalysisData(null);
|
|
|
|
try {
|
|
const data = await fetchSwtrmtPrcsZoneAnalysis(rawFeatureData, featureData);
|
|
if (!isMounted) return;
|
|
|
|
if (!data) {
|
|
setErrorMessage("분석정보 조회에 필요한 처리구역 식별값이 없습니다.");
|
|
return;
|
|
}
|
|
|
|
setAnalysisData(data);
|
|
} catch (error) {
|
|
if (!isMounted) return;
|
|
console.error("하수처리구역 분석정보 로딩 중 오류:", error);
|
|
setErrorMessage("분석정보를 불러오지 못했습니다.");
|
|
} finally {
|
|
if (isMounted) setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchAnalysis();
|
|
|
|
return () => {
|
|
isMounted = false;
|
|
};
|
|
}, [featureData, rawFeatureData, requestKey]);
|
|
|
|
return (
|
|
<>
|
|
<div className="card-header flex items-center justify-between">
|
|
<h3 className="text-2xl mx-3 font-medium text-label">처리구역내 관로분석정보</h3>
|
|
</div>
|
|
<div className="card !mx-[1.2rem] !mb-[1.2rem] !mt-1 !p-0">
|
|
<div className="px-3 pb-3 pt-1">
|
|
{!isLoading && errorMessage && (
|
|
<div className="py-6 text-center text-[13px] text-gray-500">{errorMessage}</div>
|
|
)}
|
|
{(isLoading || !errorMessage) && (
|
|
metricGroups.map((group) => (
|
|
<MetricVerticalGrid
|
|
key={group.field}
|
|
title={group.title}
|
|
field={group.field}
|
|
analysis={analysisData?.analysis}
|
|
isLoading={isLoading}
|
|
/>
|
|
))
|
|
)}
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default SwtrmtPrcsZoneAnalysis;
|