/** * ๐Ÿ“„ useSearchModal Hook * ๊ฒ€์ƒ‰ ๋ชจ๋‹ฌ ํ›… * * - ํƒญ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค ๊ด€๋ฆฌ (๊ธฐ๋ณธ์ •๋ณด, ์œ ์ง€๊ด€๋ฆฌ ์ •๋ณด, ๋ณด๊ณ ์„œ) * - ํ•„ํ„ฐ๋ง๋œ ๋ฐ์ดํ„ฐ ์กฐํšŒ ๋ฐ ํฌ๋งทํŒ… * - ์กฐํšŒ ๋ฒ„ํŠผ์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ๊ฑฐ * - ์—ฌ๋Ÿฌ ์‹œ์„ค ๋ณด๊ธฐ ๊ธฐ๋Šฅ * - Zustand๋ฅผ ํ†ตํ•œ ์ƒํƒœ ๊ด€๋ฆฌ ๋ฐ ๋กœ๋”ฉ ์ฒ˜๋ฆฌ * - ๋ฐ์ดํ„ฐ ๋งคํ•‘ ๋ฐ ์ฝ”๋“œ ๋ณ€ํ™˜ (์‚ฌ์—… ์œ ํ˜• ๋“ฑ) * * ๐Ÿ”— ์‚ฌ์šฉ API * - ์œ ์ง€๊ด€๋ฆฌ ์ •๋ณด ๋ชฉ๋ก ์กฐํšŒ: GET /api/v1/bizInfo (sewerage-service) */ import { getData } from "@/components/lib/api"; import { formatDisplayData } from '@/components/lib/utils/formatDisplayValue'; import useStore from "@/store/useStore"; import GisApp from "@common/business_components/ui/map/lib/gis.core"; import useSearchModalStore from "@/store/useSearchModalStore"; import useBasicTab from './useBasicTab'; import useOperationTab from './useOperationTab'; export default function useSearchModal() { const { activeTab, workType, tableData, detailTableData, curPage, var1, var2, yearFilter, todayYear, todayMonth, todayDate, setIsSearchModalOpen, setShowTable, setTableData, setDetailTableData, setTableCount, setDetailTableCount, setCurPage, setIsLoading, setIsResetPage, setIsResetDetailPage, setIsResetSelection, setIsResetDetailSelection, getDateString } = useSearchModalStore(); const { fetchBasicFilteredData } = useBasicTab(); const { OPERATION_TABLE_SIZE } = useOperationTab(); const { BIZ_TY_CD } = useStore(); const TABS = [ { val: "basic", text: "๊ธฐ๋ณธ ์ •๋ณด" }, { val: "operation", text: "์œ ์ง€๊ด€๋ฆฌ ์ •๋ณด" }, { val: "report", text: "ํ•˜์ˆ˜๋„ ์ •๋น„ ๊ธฐ๋ณธ๊ณ„ํš ๋ณด๊ณ ์„œ" }, ]; // ๊ฒ€์ƒ‰ API ํ˜ธ์ถœ ํ•จ์ˆ˜ const fetchFilteredData = async () => { setIsLoading(true); // ๋กœ๋”ฉ ์‹œ์ž‘ setIsResetSelection(true); // ์„ ํƒ ์ดˆ๊ธฐํ™” setIsResetDetailSelection(true); // ์„ ํƒ ์ดˆ๊ธฐํ™” try { if (activeTab === 'basic') { fetchBasicFilteredData(); } else if (activeTab === 'operation') { const RequestDTO: Record = { page: `${curPage - 1}`, size: `${OPERATION_TABLE_SIZE}`, }; if (var1) RequestDTO.bizInfoNm = var1; if (var2 != '0' && var2) RequestDTO.bizTypeCd = var2; // ์ˆ˜ํ–‰๊ธฐ๊ฐ„ ๋ฒ”์œ„ const startDate = getDateString( yearFilter.start, '', '', "19000101", ); const endDate = getDateString( yearFilter.end, '', '', `${todayYear}${todayMonth}${todayDate}`, ); RequestDTO.fromflfmtEndYmd = startDate; RequestDTO.toflfmtEndYmd = endDate; // JSON.parse ์‚ฌ์šฉ์œผ๋กœ ๋ฌธ์ž์—ด -> ๋ฐฐ์—ด ๋ณ€ํ™˜ RequestDTO.workTypes = JSON.parse(workType.replace(/'/g, '"')); const params = new URLSearchParams(RequestDTO).toString(); const res = await getData(`v1/bizInfo?${params}`); const mappedContent = (res.content || []).map((item: any, index: number) => { const formattedItem = formatDisplayData(item); const matched = BIZ_TY_CD.find(code => code.lwpoCd === item.bizTypeCd); return { ...formattedItem, rowNumber: (curPage - 1) * OPERATION_TABLE_SIZE + index + 1, // 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ผ๋ จ๋ฒˆํ˜ธ ์ถ”๊ฐ€ bizTypeCd: matched ? matched.lwpoCdNm : item.bizTypeCd, }; }); setTableData(mappedContent || []); setTableCount(res.totalElements || 0); setDetailTableData([]); setDetailTableCount(0); } setShowTable(true); setIsResetSelection(false); } catch (err) { console.error('API ERROR:', err); setTableData([]); setIsResetSelection(false); } finally { setIsLoading(false); // ๋กœ๋”ฉ ์ข…๋ฃŒ setIsResetSelection(false); // setIsResetDetailSelection(false); } }; // ์กฐํšŒ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ 1ํŽ˜์ด์ง€๋กœ ์ด๋™ ๋ฐ ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™” const handleSearch = () => { setCurPage(1); // ํŽ˜์ด์ง€๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™” fetchFilteredData(); setIsResetPage(true); // ํŽ˜์ด์ง€๋ฅผ ์ดˆ๊ธฐํ™” setIsResetDetailPage(true); // ๋””ํ…Œ์ผ ํŽ˜์ด์ง€๋ฅผ ์ดˆ๊ธฐํ™” setIsResetSelection(false); // ์„ ํƒ ์ดˆ๊ธฐํ™” setIsResetDetailSelection(false); // ์„ ํƒ ์ดˆ๊ธฐํ™” setShowTable(true); }; // ์—ฌ๋Ÿฌ ์‹œ์„ค ๋ณด๊ธฐ const viewMultipleFeature = async () => { if (activeTab === 'basic') { if (!(tableData.length === 0)) { setIsLoading(true); const row = tableData[0]; const layerNm = row.lyrTypeNm; const fids = tableData.map((item) => item.fid); const core = GisApp.Module.layer as any; await core.showMultipleSearchResultsFromGeoJSON(layerNm, fids); setIsLoading(false); setIsSearchModalOpen(false); } } else if (activeTab === 'operation') { if (!(detailTableData.length === 0)) { setIsLoading(true); const row = detailTableData[0]; const layerNm = row.lyrTypeNm; const fids = detailTableData.map((item) => item.fid); const core = GisApp.Module.layer as any; await core.showMultipleSearchResultsFromGeoJSON(layerNm, fids); setIsLoading(false); setIsSearchModalOpen(false); } } } return { TABS, getDateString, handleSearch, viewMultipleFeature, fetchFilteredData }; };