refactor(sn): modularize map and features and standardize file naming
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* 📄 Store Index
|
||||
* Zustand 스토어 인덱스 파일
|
||||
*
|
||||
* - 시도 데이터 조회 및 커스텀 정렬 기능
|
||||
* - 시군구 데이터 조회 및 알파벳 정렬 기능
|
||||
* - 사업 구분 코드 조회 및 상태 관리
|
||||
* - 사업 유형 코드 조회 및 필터링 기능
|
||||
* - GeoServer WFS를 통한 지리 데이터 가져오기
|
||||
* - CMS 서비스를 통한 코드 데이터 가져오기
|
||||
*
|
||||
* 🔗 사용 API
|
||||
* - 시도 데이터 조회: GET /geoserver/hasudo/ows (GeoServer)
|
||||
* - 시군구 데이터 조회: GET /geoserver/hasudo/ows (GeoServer)
|
||||
* - 사업 구분 코드 조회: GET /api/v1/code/cache/BIZSECD (cms-service)
|
||||
* - 사업 유형 코드 조회: GET /api/v1/code/cache/BIZTYCD (cms-service)
|
||||
*/
|
||||
import { getCmsData } from "@/shared/api/external-service-api";
|
||||
|
||||
|
||||
export type BIZ_CD_TYPE = {
|
||||
lwpoCd: string,
|
||||
upCd: string,
|
||||
lwpoCdNm: string,
|
||||
unit: string,
|
||||
prntLwpoCd: null
|
||||
};
|
||||
|
||||
// 시도 데이터 조회
|
||||
export const fetchCtpv = async (set: (state: any) => void) => {
|
||||
const url = `/geoserver/hasudo/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=hasudo:tb_ms_ctpv&outputFormat=application/json&propertyName=mctpv_korn_nm,mctpv_cd`;
|
||||
const res = await fetch(url).then(res => res.json());
|
||||
|
||||
const list = res.features
|
||||
.map((f: any) => ({
|
||||
text: String(f.properties.mctpv_korn_nm ?? "").trim(),
|
||||
val: String(f.properties.mctpv_cd ?? "").trim(),
|
||||
}))
|
||||
.sort((a:any, b:any) => {
|
||||
// 커스텀 순서 정의
|
||||
const customOrder = [
|
||||
"서울특별시",
|
||||
"부산광역시",
|
||||
"대구광역시",
|
||||
"인천광역시",
|
||||
"광주광역시",
|
||||
"대전광역시",
|
||||
"울산광역시",
|
||||
"세종특별자치시",
|
||||
"경기도",
|
||||
"강원특별자치도", // 경기도 다음에 배치
|
||||
"충청북도",
|
||||
"충청남도",
|
||||
"전북특별자치도", // 충청남도 다음에 배치
|
||||
"전라남도",
|
||||
"경상북도",
|
||||
"경상남도",
|
||||
"제주특별자치도"
|
||||
];
|
||||
|
||||
const indexA = customOrder.indexOf(a.text);
|
||||
const indexB = customOrder.indexOf(b.text);
|
||||
|
||||
// 둘 다 커스텀 순서에 있으면 해당 순서로
|
||||
if (indexA !== -1 && indexB !== -1) {
|
||||
return indexA - indexB;
|
||||
}
|
||||
|
||||
// 하나만 커스텀 순서에 있으면 그것을 앞으로
|
||||
if (indexA !== -1) return -1;
|
||||
if (indexB !== -1) return 1;
|
||||
|
||||
// 둘 다 커스텀 순서에 없으면 기본 알파벳 순서
|
||||
return a.text.localeCompare(b.text, 'ko');
|
||||
});
|
||||
set({ ctpv: list });
|
||||
};
|
||||
|
||||
// 시군구 데이터 조회
|
||||
export const fetchSgg = async (set: (state: any) => void) => {
|
||||
const url = `/geoserver/hasudo/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=hasudo:tb_ms_sgg&outputFormat=application/json&propertyName=sgg_cd,sgg_korn_nm`;
|
||||
const res = await fetch(url).then(res => res.json());
|
||||
|
||||
const list = res.features
|
||||
.map((f: any) => ({
|
||||
text: String(f.properties.sgg_korn_nm ?? "").trim(),
|
||||
val: String(f.properties.sgg_cd ?? "").trim(),
|
||||
}))
|
||||
.sort((a:any, b:any) => a.text.localeCompare(b.text, 'ko'));
|
||||
|
||||
set({ sgg: list });
|
||||
};
|
||||
|
||||
export const fetchBIZ_SE_CD = async (set: (state: any) => void) => {
|
||||
try {
|
||||
const res = await getCmsData(`/api/v1/code/cache/BIZSECD`);
|
||||
set({ BIZ_SE_CD: res });
|
||||
} catch (e: any) {
|
||||
// alert("BIZ_SE_CD 데이터 로드 실패: " + e.message);
|
||||
console.error("BIZ_SE_CD 데이터 로드 실패:", e);
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchBIZ_TY_CD = async (set: (state: any) => void) => {
|
||||
try {
|
||||
const res = await getCmsData(`/api/v1/code/cache/BIZTYCD`);
|
||||
const filtered: BIZ_CD_TYPE[] = Array.isArray(res)
|
||||
? (res as BIZ_CD_TYPE[]).filter(item => item?.lwpoCd !== "AQAAA")
|
||||
: [];
|
||||
set({ BIZ_TY_CD: filtered });
|
||||
} catch (e: any) {
|
||||
// alert("BIZ_TY_CD 데이터 로드 실패: " + e.message);
|
||||
console.error("BIZ_TY_CD 데이터 로드 실패:", e);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,164 @@
|
||||
"use client";
|
||||
|
||||
import { create } from "zustand";
|
||||
|
||||
import { getRawJson } from "@/shared/api/sewerage-api";
|
||||
|
||||
import type { SelectDataType } from "@common/business_components";
|
||||
|
||||
type OnsemiroAddrStoreState = {
|
||||
govArea: SelectDataType[];
|
||||
city: SelectDataType[];
|
||||
sgg: SelectDataType[];
|
||||
fetchGovArea: () => Promise<void>;
|
||||
fetchCity: () => Promise<void>;
|
||||
fetchSgg: () => Promise<void>;
|
||||
};
|
||||
|
||||
/** 시도 SelectBox에 적용할 행정구역 표준 표시 순서다. */
|
||||
const CITY_SORT_ORDER = [
|
||||
"서울특별시",
|
||||
"부산광역시",
|
||||
"대구광역시",
|
||||
"인천광역시",
|
||||
"광주광역시",
|
||||
"대전광역시",
|
||||
"울산광역시",
|
||||
"세종특별자치시",
|
||||
"경기도",
|
||||
"강원특별자치도",
|
||||
"충청북도",
|
||||
"충청남도",
|
||||
"전북특별자치도",
|
||||
"전라남도",
|
||||
"경상북도",
|
||||
"경상남도",
|
||||
"제주특별자치도",
|
||||
];
|
||||
|
||||
/** 공통 응답 래퍼와 배열 응답을 동일한 목록으로 정규화한다. */
|
||||
const toResponseList = (response: unknown) => {
|
||||
if (Array.isArray(response)) return response;
|
||||
if (response && typeof response === "object") {
|
||||
const data = (response as { data?: unknown }).data;
|
||||
if (Array.isArray(data)) return data;
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
/** ONSEMIRO 주소 원본 필드를 공통 SelectBox 옵션으로 변환한다. */
|
||||
const toSelectOption = (item: Record<string, unknown>) => {
|
||||
const textCandidate = [
|
||||
item.text,
|
||||
item.name,
|
||||
item.label,
|
||||
item.govAreaNm,
|
||||
item.govAreaName,
|
||||
item.gugun,
|
||||
item.sggNm,
|
||||
item.sggName,
|
||||
item.lclgvrnmntNm,
|
||||
item.sido,
|
||||
item.cityNm,
|
||||
item.cityName,
|
||||
item.korNm,
|
||||
].find((value) => typeof value === "string" && value.trim());
|
||||
|
||||
const valueCandidate = [
|
||||
item.val,
|
||||
item.code,
|
||||
item.id,
|
||||
item.govAreaCd,
|
||||
item.govAreaCode,
|
||||
item.adstDstrCd,
|
||||
item.sggCd,
|
||||
item.sggCode,
|
||||
item.lclgvrnmntCd,
|
||||
item.cityCd,
|
||||
item.cityCode,
|
||||
].find(
|
||||
(value) => value !== null && value !== undefined && String(value).trim(),
|
||||
);
|
||||
|
||||
if (!textCandidate || valueCandidate === undefined) return null;
|
||||
|
||||
return {
|
||||
text: String(textCandidate).trim(),
|
||||
val: String(valueCandidate).trim(),
|
||||
};
|
||||
};
|
||||
|
||||
/** 시도 옵션을 표준 순서로 정렬하고 나머지는 한글명으로 정렬한다. */
|
||||
const compareCityOptions = (left: SelectDataType, right: SelectDataType) => {
|
||||
const leftIndex = CITY_SORT_ORDER.indexOf(left.text);
|
||||
const rightIndex = CITY_SORT_ORDER.indexOf(right.text);
|
||||
|
||||
if (leftIndex !== -1 && rightIndex !== -1) return leftIndex - rightIndex;
|
||||
if (leftIndex !== -1) return -1;
|
||||
if (rightIndex !== -1) return 1;
|
||||
return left.text.localeCompare(right.text, "ko");
|
||||
};
|
||||
|
||||
/** 주소 API를 호출해 공통 SelectBox 옵션 목록을 만든다. */
|
||||
const loadSelectOptions = async (url: string) => {
|
||||
const response = await getRawJson(url);
|
||||
return toResponseList(response)
|
||||
.map((item) =>
|
||||
item && typeof item === "object"
|
||||
? toSelectOption(item as Record<string, unknown>)
|
||||
: null,
|
||||
)
|
||||
.filter((item): item is SelectDataType => Boolean(item));
|
||||
};
|
||||
|
||||
/** ONSEMIRO 환경청 응답을 코드/환경청명 기준으로 SelectBox 옵션으로 변환한다. */
|
||||
const loadGovAreaOptions = async () => {
|
||||
const response = await getRawJson(
|
||||
"/sewerage-service/api/v1/onsemiro/addr/govArea",
|
||||
);
|
||||
return toResponseList(response)
|
||||
.map((item) => {
|
||||
if (!item || typeof item !== "object") return null;
|
||||
const { lclgvrnmntCd, lclgvrnmntNm } = item as Record<string, unknown>;
|
||||
const code = String(lclgvrnmntCd ?? "").trim();
|
||||
const name = String(lclgvrnmntNm ?? "").trim();
|
||||
return code && name ? { text: name, val: code } : null;
|
||||
})
|
||||
.filter((item): item is SelectDataType => Boolean(item));
|
||||
};
|
||||
|
||||
/** ONSEMIRO 환경청·시도·시군구 선택목록을 공유하는 상태 저장소다. */
|
||||
const useOnsemiroAddrStore = create<OnsemiroAddrStoreState>((set) => ({
|
||||
govArea: [],
|
||||
city: [],
|
||||
sgg: [],
|
||||
/** 환경청 선택목록을 조회해 한글명 순으로 저장한다. */
|
||||
fetchGovArea: async () => {
|
||||
const govArea = await loadGovAreaOptions();
|
||||
set({
|
||||
govArea: govArea.sort((left, right) =>
|
||||
left.text.localeCompare(right.text, "ko"),
|
||||
),
|
||||
});
|
||||
},
|
||||
/** 시도 선택목록을 조회해 표준 행정구역 순서로 저장한다. */
|
||||
fetchCity: async () => {
|
||||
const city = await loadSelectOptions(
|
||||
"/sewerage-service/api/v1/onsemiro/addr/city",
|
||||
);
|
||||
set({
|
||||
city: city.sort(compareCityOptions),
|
||||
});
|
||||
},
|
||||
/** 시군구 선택목록을 조회해 한글명 순으로 저장한다. */
|
||||
fetchSgg: async () => {
|
||||
const sgg = await loadSelectOptions(
|
||||
"/sewerage-service/api/v1/onsemiro/addr/sgg",
|
||||
);
|
||||
set({
|
||||
sgg: sgg.sort((left, right) => left.text.localeCompare(right.text, "ko")),
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
export default useOnsemiroAddrStore;
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 📄 useStore
|
||||
* SN 앱 Zustand 스토어
|
||||
*
|
||||
* - 시도 데이터 상태 관리 및 조회 기능
|
||||
* - 시군구 데이터 상태 관리 및 조회 기능
|
||||
* - 사업 구분 코드 상태 관리 및 조회 기능
|
||||
* - 사업 유형 코드 상태 관리 및 조회 기능
|
||||
* - Zustand를 통한 중앙 집중식 상태 관리
|
||||
* - 데이터 변경 감지 및 불필요한 재렌더링 방지
|
||||
*/
|
||||
import { create } from "zustand";
|
||||
import { fetchCtpv, fetchSgg, fetchBIZ_SE_CD, fetchBIZ_TY_CD } from "@/entities/reference-data/api/reference-data-api";
|
||||
import { type SelectDataType } from "@common/business_components";
|
||||
|
||||
export type BIZ_CD_TYPE = {
|
||||
lwpoCd: string,
|
||||
upCd: string,
|
||||
lwpoCdNm: string,
|
||||
unit: string,
|
||||
prntLwpoCd: null
|
||||
};
|
||||
|
||||
interface SnStoreState {
|
||||
ctpv: SelectDataType[];
|
||||
sgg: SelectDataType[];
|
||||
BIZ_SE_CD: BIZ_CD_TYPE[];
|
||||
BIZ_TY_CD: BIZ_CD_TYPE[];
|
||||
fetchCtpv: () => Promise<void>;
|
||||
fetchSgg: () => Promise<void>;
|
||||
fetchBIZ_SE_CD: () => Promise<void>;
|
||||
fetchBIZ_TY_CD: () => Promise<void>;
|
||||
}
|
||||
|
||||
const useStore = create<SnStoreState>((set) => ({
|
||||
ctpv: [],
|
||||
sgg: [],
|
||||
BIZ_SE_CD: [],
|
||||
BIZ_TY_CD: [],
|
||||
fetchCtpv: () => fetchCtpv(set),
|
||||
fetchSgg: () => fetchSgg(set),
|
||||
fetchBIZ_SE_CD: () => fetchBIZ_SE_CD(set),
|
||||
fetchBIZ_TY_CD: () => fetchBIZ_TY_CD(set),
|
||||
}));
|
||||
|
||||
export default useStore;
|
||||
Reference in New Issue
Block a user