194 lines
8.1 KiB
TypeScript
194 lines
8.1 KiB
TypeScript
import { useRef, useState } from "react";
|
|
|
|
import { useAlertStore } from "@/shared/lib";
|
|
import { statisticsTotalStatusApi } from "@/entities/statistics/api/statistics-total-service";
|
|
import { toGridRows } from "@/components/util/grid/gridFunction";
|
|
|
|
import SearchForm from "@/components/module/SearchForm";
|
|
import DataGrid from "@/components/module/DataGrid";
|
|
import { LoadingWithAnimation, Tabs, TabsContent, TabsList, TabsTrigger } from "@common/components";
|
|
|
|
import type { ColumnInfo } from "@/components/type/DataGrid.type";
|
|
import type { SearchFormValues } from "@/components/type/SearchForm.type";
|
|
import { ComplaintInpColumn } from "@/components/util/column/total/sewerageServiceLevel/ComplaintTotColumn";
|
|
import { ResidentFriendlyFactTotColumn } from "@/components/util/column/total/sewerageServiceLevel/ResidentFriendlyFactTotColumn";
|
|
|
|
const SewerageServiceLevelInpTabList = [
|
|
ComplaintInpColumn,
|
|
ResidentFriendlyFactTotColumn,
|
|
];
|
|
const SUPPORTED_TAB_KEYS = [ComplaintInpColumn.tabKey, ResidentFriendlyFactTotColumn.tabKey] as const;
|
|
|
|
const SewerageServiceLevelTot = () => {
|
|
const { setMessage: alert } = useAlertStore((state) => state);
|
|
const [rowDataMap, setRowDataMap] = useState<Map<string, Record<string, string | number>[]>>(new Map());
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
const [activeTab, setActiveTab] = useState(ComplaintInpColumn.tabKey);
|
|
const lastSearchValuesRef = useRef<SearchFormValues | null>(null);
|
|
|
|
const columnInfo = SewerageServiceLevelInpTabList;
|
|
|
|
const handleReset = () => {
|
|
setRowDataMap(new Map());
|
|
setActiveTab(ComplaintInpColumn.tabKey);
|
|
lastSearchValuesRef.current = null;
|
|
};
|
|
|
|
const loadSewerageServiceLevelTotTabData = async (
|
|
tabKey: string,
|
|
values: SearchFormValues,
|
|
mergeIntoExistingMap: boolean,
|
|
) => {
|
|
const param = {
|
|
ctpvNm: values.sido === "ALL" ? "" : values.sido,
|
|
sggNm: values.gugun === "ALL" ? "" : values.gugun,
|
|
sewerBzmnCd: values.bsnmCd,
|
|
statsYr: values.year,
|
|
rowDataYn: values.isDetail,
|
|
};
|
|
|
|
const fetcherMap = {
|
|
[ComplaintInpColumn.tabKey]: statisticsTotalStatusApi.getSewerageServiceLevelComplaint.bind(
|
|
statisticsTotalStatusApi,
|
|
),
|
|
[ResidentFriendlyFactTotColumn.tabKey]:
|
|
statisticsTotalStatusApi.getSewerageServiceLevelResidentFriendlyFact.bind(
|
|
statisticsTotalStatusApi,
|
|
),
|
|
};
|
|
const fetcher = fetcherMap[tabKey];
|
|
if (!fetcher) {
|
|
return false;
|
|
}
|
|
const response = await fetcher(param);
|
|
|
|
if (response.status === "success" && response.data) {
|
|
const rows = toGridRows(response.data as object[]);
|
|
if (mergeIntoExistingMap) {
|
|
setRowDataMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(tabKey, rows);
|
|
return next;
|
|
});
|
|
} else {
|
|
const next = new Map<string, Record<string, string | number>[]>();
|
|
next.set(tabKey, rows);
|
|
setRowDataMap(next);
|
|
}
|
|
return true;
|
|
}
|
|
alert(response.message || "데이터 조회 중 오류가 발생했습니다.");
|
|
return false;
|
|
};
|
|
|
|
const handleSearch = async (values: SearchFormValues) => {
|
|
try {
|
|
setIsLoading(true);
|
|
let ok = false;
|
|
if (SUPPORTED_TAB_KEYS.includes(activeTab)) {
|
|
ok = await loadSewerageServiceLevelTotTabData(activeTab, values, true);
|
|
}
|
|
if (ok) {
|
|
lastSearchValuesRef.current = values;
|
|
} else {
|
|
lastSearchValuesRef.current = null;
|
|
}
|
|
} catch {
|
|
alert("데이터 조회 중 오류가 발생했습니다.");
|
|
lastSearchValuesRef.current = null;
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleActiveTabChange = async (tabKey: string) => {
|
|
setActiveTab(tabKey);
|
|
|
|
const values = lastSearchValuesRef.current;
|
|
if (!values) return;
|
|
|
|
const existing = rowDataMap.get(tabKey);
|
|
const hasData = Array.isArray(existing) && existing.length > 0;
|
|
if (hasData) return;
|
|
|
|
if (SUPPORTED_TAB_KEYS.includes(tabKey)) {
|
|
try {
|
|
setIsLoading(true);
|
|
await loadSewerageServiceLevelTotTabData(tabKey, values, true);
|
|
} catch {
|
|
alert("데이터 조회 중 오류가 발생했습니다.");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="relative">
|
|
<SearchForm isDetail={true} onSubmit={handleSearch} onReset={handleReset} sidoSelectText="전체" />
|
|
{isLoading && (
|
|
<div className="absolute inset-0 z-50 [data-theme='black']:bg-transparent rounded-lg p-10 flex justify-center items-center">
|
|
<div className="relative size-[5rem]">
|
|
<LoadingWithAnimation color="primary" />
|
|
</div>
|
|
</div>
|
|
)}
|
|
<div className="flex flex-col gap-[0.6rem] mx-[0.6rem]">
|
|
<Tabs value={activeTab} onValueChange={(v) => void handleActiveTabChange(v)} className="w-full" variant="default">
|
|
<TabsList>
|
|
{columnInfo.map((info) => (
|
|
<TabsTrigger key={info.tabKey} value={info.tabKey}>
|
|
{info.tabLabel || info.tabKey}
|
|
</TabsTrigger>
|
|
))}
|
|
</TabsList>
|
|
{columnInfo.map((info) => {
|
|
const tabKey = info.tabKey;
|
|
if (!info.columns || !info.columnLayout) {
|
|
return null;
|
|
}
|
|
|
|
const rowData = rowDataMap.get(tabKey) ?? [];
|
|
const gridColumnInfo: ColumnInfo = {
|
|
columns: info.columns,
|
|
columnLayout: info.columnLayout,
|
|
};
|
|
|
|
return (
|
|
<TabsContent key={tabKey} value={tabKey}>
|
|
<DataGrid
|
|
columnInfo={gridColumnInfo}
|
|
rowStartNum={info.rowStartNum}
|
|
excelExportFileSuffix={info.tabLabel || tabKey}
|
|
rowData={rowData}
|
|
setRowData={(updater) => {
|
|
setRowDataMap((prev) => {
|
|
const next = new Map(prev);
|
|
const cur = next.get(tabKey) ?? [];
|
|
const n = typeof updater === "function" ? updater(cur) : updater;
|
|
next.set(tabKey, n);
|
|
return next;
|
|
});
|
|
}}
|
|
isExcelImport={false}
|
|
isExcelExport
|
|
showHeaderCheckbox={false}
|
|
fixedColumns={info.fixedColumns}
|
|
enableSort={false}
|
|
cellStyleCallback={({ row }) => {
|
|
const rowType = String(row.rowType ?? "");
|
|
if (rowType === "DETAIL") return undefined;
|
|
if (rowType === "BIZ_SUM") return "rg-cell-rowtype-biz-sum";
|
|
return "rg-cell-rowtype-other";
|
|
}}
|
|
/>
|
|
</TabsContent>
|
|
);
|
|
})}
|
|
</Tabs>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SewerageServiceLevelTot; |