diff --git a/src/components/FormDialog.vue b/src/components/FormDialog.vue index 928934c6..ca4996e0 100644 --- a/src/components/FormDialog.vue +++ b/src/components/FormDialog.vue @@ -13,6 +13,11 @@ defineProps<{ close?: (...args: unknown[]) => void; }>(); +defineExpose({ + fetchDistrict, + fetchSubDistrict, +}); + const adrressStore = useAddressStore(); const modal = defineModel('modal', { default: false }); const address = defineModel('address', { default: '' }); @@ -48,39 +53,38 @@ async function fetchSubDistrict(id: string | undefined) { if (id) { const result = await adrressStore.fetchSubDistrictByProvinceId(id); if (result) { - console.log(addrOptions.value.subDistrictOps); addrOptions.value.subDistrictOps = result; } } } -onMounted(async () => { - await fetchProvince(); -}); - -watch(provinceId, async (v) => { - await fetchDistrict(v); -}); - -watch(districtId, async (v) => { +async function selectProvince(id: string) { + if (!id) return; + districtId.value = undefined; subDistrictId.value = undefined; - await fetchSubDistrict(v); -}); + addrOptions.value.districtOps = []; + addrOptions.value.subDistrictOps = []; + zipCode.value = ''; + await fetchDistrict(id); +} -watch(subDistrictId, async (v) => { +async function selectDistrict(id: string) { + if (!id) return; + subDistrictId.value = undefined; + zipCode.value = ''; + await fetchSubDistrict(id); +} + +async function selectSubDistrict(id: string) { + if (!id) return; zipCode.value = addrOptions.value.subDistrictOps - ?.filter((x) => x.id === v) + ?.filter((x) => x.id === id) .map((x) => x.zipCode)[0] ?? ''; -}); +} -watch(provinceId, (v) => { - if (v) { - addrOptions.value.districtOps = []; - addrOptions.value.subDistrictOps = []; - districtId.value = undefined; - subDistrictId.value = undefined; - } +onMounted(async () => { + await fetchProvince(); });