90 lines
2.5 KiB
TypeScript
90 lines
2.5 KiB
TypeScript
import { create } from "zustand";
|
|
import { getCmsData } from "@/components/lib/api";
|
|
|
|
export interface Option { id: number, name: string }
|
|
|
|
export type BIZ_CD_TYPE = {
|
|
lwpoCd: string,
|
|
upCd: string,
|
|
lwpoCdNm: string,
|
|
unit: string,
|
|
prntLwpoCd: null
|
|
};
|
|
|
|
export type DataState = {
|
|
ctpv: Option[];
|
|
sgg: Option[];
|
|
BIZ_SE_CD: BIZ_CD_TYPE[];
|
|
BIZ_TY_CD: BIZ_CD_TYPE[];
|
|
}
|
|
|
|
export type FetchData = {
|
|
fetchCtpvSggData: () => Promise<void>;
|
|
fetchBIZ_SE_CD: () => Promise<void>;
|
|
fetchBIZ_TY_CD: () => Promise<void>;
|
|
};
|
|
|
|
export type DataStore = DataState & FetchData;
|
|
|
|
export const defaultInitState: DataState = {
|
|
ctpv: [],
|
|
sgg: [],
|
|
BIZ_SE_CD: [],
|
|
BIZ_TY_CD: [],
|
|
};
|
|
|
|
const fetchCtpvSggData = async (set: (state: any) => void) => {
|
|
try {
|
|
const ctpvData = await getCmsData('/api/v1/code/cache/ADSTDSTRCD');
|
|
console.log("CTPV Data:", ctpvData);
|
|
const ctpvList: Option[] = ctpvData
|
|
.filter((item: any) => typeof item.lwpoCd === 'string' && item.lwpoCd.length === 2)
|
|
.map((feature: any) => ({
|
|
id: feature.lwpoCd,
|
|
name: feature.lwpoCdNm
|
|
}))
|
|
.sort((a: Option, b: Option) => a.name.localeCompare(b.name, 'ko'));
|
|
|
|
const sggList: Option[] = ctpvData
|
|
.filter((item: any) => typeof item.lwpoCd === 'string' && item.lwpoCd.length !== 2)
|
|
.map((item: any) => ({
|
|
id: item.lwpoCd,
|
|
name: item.lwpoCdNm
|
|
}))
|
|
.sort((a: Option, b: Option) => a.name.localeCompare(b.name, 'ko'));
|
|
|
|
set({ ctpv: ctpvList, sgg: sggList });
|
|
} catch (e: any) {
|
|
// alert("시도, 시군구 데이터 로드 실패: " + e.message);
|
|
console.error("CTPV 데이터 로드 실패:", e);
|
|
}
|
|
};
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
const fetchBIZ_TY_CD = async (set: (state: any) => void) => {
|
|
try {
|
|
const res = await getCmsData(`/api/v1/code/cache/BIZTYCD`);
|
|
set({ BIZ_TY_CD: res });
|
|
} catch (e: any) {
|
|
// alert("BIZ_TY_CD 데이터 로드 실패: " + e.message);
|
|
console.error("BIZ_TY_CD 데이터 로드 실패:", e);
|
|
}
|
|
};
|
|
|
|
export const createDataStore = (initState: DataState = defaultInitState) => {
|
|
return create<DataStore>((set) => ({
|
|
...initState,
|
|
fetchCtpvSggData: () => fetchCtpvSggData(set),
|
|
fetchBIZ_SE_CD: () => fetchBIZ_SE_CD(set),
|
|
fetchBIZ_TY_CD: () => fetchBIZ_TY_CD(set),
|
|
}));
|
|
}; |