diff --git a/apps/sn/app/store/index.ts b/apps/sn/app/store/index.ts index 875f0489d..ea5171b44 100644 --- a/apps/sn/app/store/index.ts +++ b/apps/sn/app/store/index.ts @@ -1,4 +1,4 @@ -import { getGeoData, getCmsData } from "@/components/lib/api"; +import { getCmsData } from "@/components/lib/api"; export interface Option { id: number, name: string } @@ -11,35 +11,33 @@ export type BIZ_CD_TYPE = { prntLwpoCd: null }; -export const fetchCtpvData = async (set: (state: any) => void) => { +export const fetchCtpvSggData = async (set: (state: any) => void) => { try { - const ctpvData = await getGeoData('/hasudo/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=hasudo%3Atb_ms_ctpv&outputFormat=application%2Fjson'); - const ctpvList: Option[] = ctpvData.features + 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.properties.mctpv_cd.trim(), - name: feature.properties.mctpv_korn_nm + id: feature.lwpoCd, + name: feature.lwpoCdNm })) .sort((a: Option, b: Option) => a.name.localeCompare(b.name, 'ko')); - set({ ctpv: ctpvList }); - } catch (e: any) { - // alert("시도 데이터 로드 실패: " + e.message); - console.error("CTPV 데이터 로드 실패:", e); - } -}; -export const fetchSggData = async (set: (state: any) => void) => { - try { - const sggData = await getGeoData('/hasudo/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=hasudo%3Atb_ms_sgg&outputFormat=application%2Fjson'); - const sggList: Option[] = sggData.features - .map((feature: any) => ({ - id: feature.properties.sgg_cd, - name: feature.properties.sgg_korn_nm + 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({ sgg: sggList }); + + console.log("CTPV List:", ctpvList); + console.log("SGG List:", sggList); + + set({ ctpv: ctpvList, sgg: sggList }); } catch (e: any) { - // alert("시군구 데이터 로드 실패: " + e.message); - console.error("SGG 데이터 로드 실패:", e); + // alert("시도, 시군구 데이터 로드 실패: " + e.message); + console.error("CTPV 데이터 로드 실패:", e); } }; diff --git a/apps/sn/app/store/useStore.ts b/apps/sn/app/store/useStore.ts index 7775ca61e..b2ae014fa 100644 --- a/apps/sn/app/store/useStore.ts +++ b/apps/sn/app/store/useStore.ts @@ -1,6 +1,6 @@ import { create } from "zustand"; import { getGeoData, getCmsData } from "@/components/lib/api"; -import { fetchCtpvData, fetchSggData, fetchBIZ_SE_CD, fetchBIZ_TY_CD } from "./index"; +import { fetchCtpvSggData, fetchBIZ_SE_CD, fetchBIZ_TY_CD } from "./index"; interface Option { id: number, name: string } @@ -17,8 +17,7 @@ interface SnStoreState { sgg: Option[]; BIZ_SE_CD: BIZ_CD_TYPE[]; BIZ_TY_CD: BIZ_CD_TYPE[]; - fetchCtpvData: () => Promise; - fetchSggData: () => Promise; + fetchCtpvSggData: () => Promise; fetchBIZ_SE_CD: () => Promise; fetchBIZ_TY_CD: () => Promise; } @@ -28,8 +27,7 @@ const useStore = create((set) => ({ sgg: [], BIZ_SE_CD: [], BIZ_TY_CD: [], - fetchCtpvData: () => fetchCtpvData(set), - fetchSggData: () => fetchSggData(set), + fetchCtpvSggData: () => fetchCtpvSggData(set), fetchBIZ_SE_CD: () => fetchBIZ_SE_CD(set), fetchBIZ_TY_CD: () => fetchBIZ_TY_CD(set), })); diff --git a/apps/sn/src/shared/lib/index.ts b/apps/sn/src/shared/lib/index.ts index b8b4c52c0..061346946 100644 --- a/apps/sn/src/shared/lib/index.ts +++ b/apps/sn/src/shared/lib/index.ts @@ -2,5 +2,6 @@ export { useMutationWrapper } from "./use-mutation-wrapper"; export { useAlertStore } from "./use-alert-store"; export { useMenuStore } from "./use-menu-store"; export { useUserStore } from "./use-user-store"; +export { useDataStore } from "./use-data-store"; export { usePrompt } from "./use-prompt"; export { getCookie, setCookie, removeCookie } from "./cookie"; diff --git a/apps/sn/src/shared/lib/use-data-store.ts b/apps/sn/src/shared/lib/use-data-store.ts new file mode 100644 index 000000000..fc88ad3bb --- /dev/null +++ b/apps/sn/src/shared/lib/use-data-store.ts @@ -0,0 +1,14 @@ +import { useStore } from "zustand"; +import { useContext } from "react"; +import type { DataStore } from "../store/data-store"; +import { StoreContext } from "../providers/store-provider"; + +export const useDataStore = (selector: (store: DataStore) => T): T => { + const storeContext = useContext(StoreContext); + + if (!storeContext) { + throw new Error("useDataStore must be used within StoreProvider"); + } + + return useStore(storeContext.dataStore, selector); +}; diff --git a/apps/sn/src/shared/providers/store-provider.tsx b/apps/sn/src/shared/providers/store-provider.tsx index a0ec17b61..db01d81b4 100644 --- a/apps/sn/src/shared/providers/store-provider.tsx +++ b/apps/sn/src/shared/providers/store-provider.tsx @@ -6,6 +6,7 @@ import { createAttachmentStore, createMenuStore, createUserStore, + createDataStore, } from "../store"; type StoreContextType = { @@ -13,6 +14,7 @@ type StoreContextType = { attachmentStore: ReturnType; menuStore: ReturnType; userStore: ReturnType; + dataStore: ReturnType; }; export const StoreContext = createContext( @@ -27,6 +29,7 @@ export const StoreProvider = ({ children }: { children: React.ReactNode }) => { attachmentStore: createAttachmentStore(), menuStore: createMenuStore(), userStore: createUserStore(), + dataStore: createDataStore(), }; } diff --git a/apps/sn/src/shared/store/data-store.ts b/apps/sn/src/shared/store/data-store.ts new file mode 100644 index 000000000..fec870234 --- /dev/null +++ b/apps/sn/src/shared/store/data-store.ts @@ -0,0 +1,90 @@ +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; + fetchBIZ_SE_CD: () => Promise; + fetchBIZ_TY_CD: () => Promise; +}; + +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((set) => ({ + ...initState, + fetchCtpvSggData: () => fetchCtpvSggData(set), + fetchBIZ_SE_CD: () => fetchBIZ_SE_CD(set), + fetchBIZ_TY_CD: () => fetchBIZ_TY_CD(set), + })); +}; \ No newline at end of file diff --git a/apps/sn/src/shared/store/index.ts b/apps/sn/src/shared/store/index.ts index a41d6f928..8928b7269 100644 --- a/apps/sn/src/shared/store/index.ts +++ b/apps/sn/src/shared/store/index.ts @@ -2,3 +2,4 @@ export { createAlertStore } from "./alert-store"; export { createAttachmentStore } from "./attachment-store"; export { createMenuStore, type BbsType } from "./menu-store"; export { createUserStore, type IUserInfo } from "./user-store"; +export { createDataStore, type DataStore } from "./data-store"; \ No newline at end of file