"use client"; import { useContext, useEffect, useState } from "react"; import { LocationContext } from "../locationContext"; type Props = { provinces: Province[]; }; type Province = { id: number; name: string; zones: Zone[]; }; type Zone = { id: number; name: string; province: number; }; export default function LocationSelector({ provinces }: Props) { const [provinceId, setProvinceId] = useState(undefined); const [amphurList, setAmphurList] = useState(undefined); const [amphurId, setAmphurId] = useState(undefined); const locationContext = useContext(LocationContext); function setProvince(_id: string) { const id = parseInt(_id); setProvinceId(id); const province = provinces.find((p) => p.id == id); if (province == undefined) return; setAmphurList(province.zones); setAmphurId(undefined); } function setAmphur(_id: string) { const id = parseInt(_id); setAmphurId(id); } useEffect(() => { if (locationContext == undefined) return; if (amphurId == undefined || provinceId == undefined) { locationContext.zone[1](undefined); locationContext.province[1](undefined); return; } locationContext.zone[1](amphurId); locationContext.province[1](provinceId); }, [amphurId, locationContext, provinceId]); return (
จังหวัด:
{amphurList && (
อำเภอ:
)}
); }