119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
import { SelectBox } from "@common/business_components/ui";
|
|
import { Label } from "@common/components/ui";
|
|
|
|
import useOnsemiroAddrStore from "@/store/useOnsemiroAddrStore";
|
|
|
|
const DEFAULT_OPTION = { text: "전체", val: "0" };
|
|
|
|
export default function Page() {
|
|
const { govArea, city, sgg, fetchGovArea, fetchCity, fetchSgg } =
|
|
useOnsemiroAddrStore();
|
|
const [selectedGovArea, setSelectedGovArea] = useState("0");
|
|
const [selectedCity, setSelectedCity] = useState("0");
|
|
const [selectedSgg, setSelectedSgg] = useState("0");
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
useEffect(() => {
|
|
let mounted = true;
|
|
|
|
const load = async () => {
|
|
setLoading(true);
|
|
setError("");
|
|
try {
|
|
await Promise.all([fetchGovArea(), fetchCity(), fetchSgg()]);
|
|
} catch (nextError) {
|
|
if (!mounted) return;
|
|
setError(
|
|
nextError instanceof Error
|
|
? nextError.message
|
|
: "주소 공통코드를 조회하지 못했습니다.",
|
|
);
|
|
} finally {
|
|
if (mounted) setLoading(false);
|
|
}
|
|
};
|
|
|
|
void load();
|
|
|
|
return () => {
|
|
mounted = false;
|
|
};
|
|
}, [fetchCity, fetchGovArea, fetchSgg]);
|
|
|
|
useEffect(() => {
|
|
if (selectedCity === "0" || selectedSgg === "0") return;
|
|
if (selectedSgg.startsWith(selectedCity.slice(0, 2))) return;
|
|
setSelectedSgg("0");
|
|
}, [selectedCity, selectedSgg]);
|
|
|
|
const filteredSgg = useMemo(() => {
|
|
if (selectedCity === "0") return sgg;
|
|
const cityPrefix = selectedCity.slice(0, 2);
|
|
return sgg.filter((item) => String(item.val).startsWith(cityPrefix));
|
|
}, [selectedCity, sgg]);
|
|
|
|
return (
|
|
<section className="space-y-6 p-6">
|
|
<div>
|
|
<h1 className="text-2xl font-semibold">Onsemiro 주소 Store 테스트</h1>
|
|
<p className="mt-2 text-sm text-label">
|
|
`sewerage-service`의 환경청, 시도, 시군구 조회 결과를 확인하는 임시
|
|
화면입니다.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="grid gap-4 rounded-lg border bg-background p-6 md:grid-cols-3">
|
|
<div>
|
|
<Label className="mb-2 block text-base text-label">환경청</Label>
|
|
<SelectBox
|
|
label=""
|
|
className="w-full"
|
|
selectClass="w-full rounded-lg bg-background"
|
|
selectArray={[DEFAULT_OPTION, ...govArea]}
|
|
value={selectedGovArea}
|
|
onSelectValue={(value) => setSelectedGovArea(String(value))}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label className="mb-2 block text-base text-label">시도</Label>
|
|
<SelectBox
|
|
label=""
|
|
className="w-full"
|
|
selectClass="w-full rounded-lg bg-background"
|
|
selectArray={[DEFAULT_OPTION, ...city]}
|
|
value={selectedCity}
|
|
onSelectValue={(value) => setSelectedCity(String(value))}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label className="mb-2 block text-base text-label">시군구</Label>
|
|
<SelectBox
|
|
label=""
|
|
className="w-full"
|
|
selectClass="w-full rounded-lg bg-background"
|
|
selectArray={[DEFAULT_OPTION, ...filteredSgg]}
|
|
value={selectedSgg}
|
|
onSelectValue={(value) => setSelectedSgg(String(value))}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="rounded-lg border bg-secondary p-4 text-sm">
|
|
<p>로딩: {loading ? "Y" : "N"}</p>
|
|
<p>환경청 건수: {govArea.length}</p>
|
|
<p>시도 건수: {city.length}</p>
|
|
<p>시군구 건수: {sgg.length}</p>
|
|
<p>선택 환경청: {selectedGovArea}</p>
|
|
<p>선택 시도: {selectedCity}</p>
|
|
<p>선택 시군구: {selectedSgg}</p>
|
|
{error ? <p className="text-red-600">오류: {error}</p> : null}
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|