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[]>>(new Map()); const [isLoading, setIsLoading] = useState(false); const [activeTab, setActiveTab] = useState(ComplaintInpColumn.tabKey); const lastSearchValuesRef = useRef(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[]>(); 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 (
{isLoading && (
)}
void handleActiveTabChange(v)} className="w-full" variant="default"> {columnInfo.map((info) => ( {info.tabLabel || info.tabKey} ))} {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 ( { 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"; }} /> ); })}
); }; export default SewerageServiceLevelTot;