Files
frontend/apps/sn/app/store/index.ts
T

115 lines
3.9 KiB
TypeScript

/**
* 📄 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 "@/components/lib/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);
}
};