diff --git a/src/components/CreateNewPostcodeForm.vue b/src/components/CreateNewPostcodeForm.vue index 44338f186..5599c9b4f 100644 --- a/src/components/CreateNewPostcodeForm.vue +++ b/src/components/CreateNewPostcodeForm.vue @@ -10,6 +10,7 @@ import VnInput from 'src/components/common/VnInput.vue'; import CreateNewCityForm from './CreateNewCityForm.vue'; import VnSelectDialog from 'components/common/VnSelectDialog.vue'; import FormModelPopup from './FormModelPopup.vue'; +import ClaimFilter from 'src/pages/Claim/Card/ClaimFilter'; const emit = defineEmits(['onDataSaved']); @@ -22,13 +23,13 @@ const postcodeFormData = reactive({ townFk: null, }); -const townsFetchDataRef = ref(null); -const provincesFetchDataRef = ref(null); +const townsFetchDataRef = ref(false); +const provincesFetchDataRef = ref(false); const countriesOptions = ref([]); const provincesOptions = ref([]); const townsOptions = ref([]); const town = ref({}); - +const townFilter = ref({}); function onDataSaved(formData) { const newPostcode = { ...formData, @@ -56,12 +57,37 @@ async function onCityCreated(newTown, formData) { } function setTown(newTown, data) { - data.provinceFk = newTown.provinceFk; - data.countryFk = newTown.province.countryFk; - if (!newTown) return; town.value = newTown; + data.provinceFk = newTown?.provinceFk ?? newTown; + data.countryFk = newTown?.province?.countryFk ?? newTown; +} +async function setCountry(countryFk, data) { + // town.value = newTown; + // data.provinceFk = newTown?.provinceFk ?? newTown; + data.countryFk = countryFk; + // if (countryFk) { + // await fetchTowns(countryFk); + // } +} +async function filterTowns(name) { + if (name !== '') { + townFilter.value.where = { + name: { + like: `%${name}%`, + }, + }; + await townsFetchDataRef.value?.fetch(); + } +} +async function fetchTowns(countryFk) { + if (!countryFk) return; + townFilter.value.where = { + provinceFk: { + inq: provincesOptions.value.map(({ id }) => id), + }, + }; + await townsFetchDataRef.value?.fetch(); } - async function setProvince(id, data) { const newProvince = provincesOptions.value.find((province) => province.id == id); if (!newProvince) return; @@ -76,62 +102,47 @@ async function onProvinceCreated(data) { postcodeFormData.provinceFk = data.id; } -watch( - () => [postcodeFormData.countryFk], - async (newCountryFk, oldValueFk) => { - if (Array.isArray(newCountryFk)) { - newCountryFk = newCountryFk[0]; - } - if (Array.isArray(oldValueFk)) { - oldValueFk = oldValueFk[0]; - } - if (!!oldValueFk && newCountryFk !== oldValueFk) { - postcodeFormData.provinceFk = null; - postcodeFormData.townFk = null; - } - if (oldValueFk !== newCountryFk) { - await fetchProvinces(newCountryFk); - await fetchTowns(newCountryFk); - } - } -); -async function fetchTowns(countryFk) { - const townsFilter = countryFk - ? { - where: { - provinceFk: { - inq: provincesOptions.value.map(({ id }) => id), - }, - }, - } - : {}; - await townsFetchDataRef.value.fetch(townsFilter); -} -async function fetchProvinces(countryFk) { - const provincesFilter = countryFk - ? { - where: { - countryFk: countryFk, - }, - } - : {}; - await provincesFetchDataRef.value.fetch(provincesFilter); -} -watch( - () => postcodeFormData.provinceFk, - async (newProvinceFk, oldValueFk) => { - if (Array.isArray(newProvinceFk)) { - newProvinceFk = newProvinceFk[0]; - } - if (newProvinceFk !== oldValueFk) { - await townsFetchDataRef.value.fetch({ - where: { provinceFk: newProvinceFk }, - }); - } - } -); +// watch( +// () => [postcodeFormData.countryFk], +// async (newCountryFk, oldValueFk) => { +// if (Array.isArray(newCountryFk)) { +// newCountryFk = newCountryFk[0]; +// } +// if (Array.isArray(oldValueFk)) { +// oldValueFk = oldValueFk[0]; +// } +// if (!!oldValueFk && newCountryFk !== oldValueFk) { +// postcodeFormData.provinceFk = null; +// postcodeFormData.townFk = null; +// } +// if (oldValueFk !== newCountryFk) { +// await fetchProvinces(newCountryFk); +// } +// } +// ); +// async function fetchProvinces(countryFk) { +// const provincesFilter = countryFk +// ? { +// where: { +// countryFk: countryFk, +// }, +// } +// : {}; +// await provincesFetchDataRef.value.fetch(provincesFilter); +// } +// watch( +// () => postcodeFormData.provinceFk, +// async (newProvinceFk, oldValueFk) => { +// if (Array.isArray(newProvinceFk)) { +// newProvinceFk = newProvinceFk[0]; +// } +// } +// ); async function handleProvinces(data) { provincesOptions.value = data; + if (postcodeFormData.countryFk) { + await fetchTowns(postcodeFormData.countryFk); + } } async function handleTowns(data) { townsOptions.value = data; @@ -153,18 +164,12 @@ async function handleCountries(data) { - diff --git a/src/components/VnSelectProvince.vue b/src/components/VnSelectProvince.vue index 5da6b781f..bfc4ab7b7 100644 --- a/src/components/VnSelectProvince.vue +++ b/src/components/VnSelectProvince.vue @@ -7,7 +7,7 @@ import VnSelectDialog from 'components/common/VnSelectDialog.vue'; import FetchData from 'components/FetchData.vue'; import CreateNewProvinceForm from './CreateNewProvinceForm.vue'; -const emit = defineEmits(['onProvinceCreated']); +const emit = defineEmits(['onProvinceCreated', 'onProvinceFetched']); const $props = defineProps({ countryFk: { type: Number, @@ -50,6 +50,7 @@ watch( filter.value.where.countryFk = $props.countryFk; } else filter.value.where = {}; await provincesFetchDataRef.value.fetch({}); + emit('onProvinceFetched', provincesOptions.value); } ); @@ -67,6 +68,7 @@ watch( :options="provincesOptions" :tooltip="t('Create province')" hide-selected + :clearable="true" v-model="provinceFk" :rules="validate && validate('postcode.provinceFk')" :acls="[{ model: 'Province', props: '*', accessType: 'WRITE' }]"