feat: 시도, 시군구 zustand Store로 메인 화면에서 호출 후 SearchModal.tsx에서 사용
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useState, useMemo, useEffect } from "react";
|
||||
import { SelectBox } from '@common/business_components/ui'
|
||||
import { Label } from '@common/components';
|
||||
import { getGeoData } from '@/components/lib/api';
|
||||
import useStore from '../../store/useStore';
|
||||
|
||||
interface Option {
|
||||
id: number;
|
||||
@@ -28,38 +28,10 @@ export function SidoSigunguSelectBox({
|
||||
className = "",
|
||||
}: SidoSigunguSelectBoxProps) {
|
||||
|
||||
const [ctpvList, setCtpvList] = useState<Option[]>([]);
|
||||
const [sggList, setSggList] = useState<Option[]>([]);
|
||||
|
||||
// 선택된 시도에 따라 시군구 리스트를 가져옴
|
||||
const fetchCtpvData = useMemo(() => (
|
||||
async () => {
|
||||
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
|
||||
.map((feature: any) => ({
|
||||
id: feature.properties.mctpv_cd.trim(),
|
||||
name: feature.properties.mctpv_korn_nm
|
||||
}))
|
||||
.sort((a: Option, b: Option) => a.name.localeCompare(b.name, 'ko')); // 가나다순 정렬
|
||||
setCtpvList(ctpvList);
|
||||
}
|
||||
), []);
|
||||
|
||||
// 선택된 시도에 따라 시군구 리스트를 가져옴
|
||||
const fetchSggData = useMemo(() => (
|
||||
async () => {
|
||||
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 }))
|
||||
.sort((a: Option, b: Option) => a.name.localeCompare(b.name, 'ko')); // 가나다순 정렬
|
||||
setSggList(sggList);
|
||||
}
|
||||
), []);
|
||||
const { ctpv, sgg} = useStore();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
await fetchCtpvData();
|
||||
await fetchSggData();
|
||||
onSidoChange(0);
|
||||
onSigunguChange(0);
|
||||
if (setCtpvName && setSggName) {
|
||||
@@ -79,13 +51,13 @@ export function SidoSigunguSelectBox({
|
||||
label=""
|
||||
selectArray={
|
||||
[{ val: 0, text: '전체' },
|
||||
...ctpvList.map(opt => ({ val: opt.id, text: opt.name }))
|
||||
...ctpv.map(opt => ({ val: opt.id, text: opt.name }))
|
||||
]
|
||||
}
|
||||
value={sido}
|
||||
onSelectValue={val => {
|
||||
onSidoChange(Number(val));
|
||||
if (setCtpvName) setCtpvName(ctpvList.find(opt => Number(opt.id) === Number(val))?.name || "");
|
||||
if (setCtpvName) setCtpvName(ctpv.find(opt => Number(opt.id) === Number(val))?.name || "");
|
||||
}}
|
||||
selectContentClass="z-[101]"
|
||||
/>
|
||||
@@ -96,7 +68,7 @@ export function SidoSigunguSelectBox({
|
||||
label=""
|
||||
selectArray={
|
||||
[{ val: 0, text: '전체' },
|
||||
...sggList
|
||||
...sgg
|
||||
.filter(opt => String(opt.id).slice(0, 2) === String(sido))
|
||||
.map(opt => ({ val: opt.id, text: opt.name }))
|
||||
]
|
||||
@@ -104,7 +76,7 @@ export function SidoSigunguSelectBox({
|
||||
value={sigungu}
|
||||
onSelectValue={val => {
|
||||
onSigunguChange(Number(val))
|
||||
if (setSggName) setSggName(sggList.find(opt => Number(opt.id) === Number(val))?.name || "");
|
||||
if (setSggName) setSggName(sgg.find(opt => Number(opt.id) === Number(val))?.name || "");
|
||||
}}
|
||||
selectContentClass="z-[101]"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user