From b83c6407604681f7324cd65fda3f2661f7890b57 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 29 Nov 2024 07:32:31 +0100 Subject: [PATCH 1/4] feat: improve Merge branch 'test' into dev --- src/components/CreateNewPostcodeForm.vue | 76 ++++++++++++++++-------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/src/components/CreateNewPostcodeForm.vue b/src/components/CreateNewPostcodeForm.vue index 232f86a49..d082df9e5 100644 --- a/src/components/CreateNewPostcodeForm.vue +++ b/src/components/CreateNewPostcodeForm.vue @@ -22,13 +22,12 @@ const postcodeFormData = reactive({ townFk: null, }); -const townsFetchDataRef = ref(false); const countriesRef = ref(false); const townsRef = ref(false); const provincesFetchDataRef = ref(false); const provincesOptions = ref([]); +const townsOptions = ref([]); const town = ref({}); -const townFilter = ref({}); const countryFilter = ref({}); function onDataSaved(formData) { @@ -50,13 +49,13 @@ function onDataSaved(formData) { async function onCityCreated(newTown, formData) { await provincesFetchDataRef.value.fetch(); - newTown.province = provincesOptions.value.find( - (province) => province.id === newTown.provinceFk - ); + newTown.province = findProvince(newTown.provinceFk); formData.townFk = newTown; setTown(newTown, formData); } - +function findProvince(provinceId) { + return provincesOptions.value.find(({ id }) => id === provinceId); +} function setTown(newTown, data) { town.value = newTown; data.provinceFk = newTown?.provinceFk ?? newTown; @@ -71,12 +70,22 @@ async function setCountry(countryFk, data) { async function handleProvinces(data) { provincesOptions.value = data; + // handleTowns(data); +} +async function handleTowns(data) { + townsOptions.value = data; + // whereTowns.value.provinceFk.inq = mapData(data); + // townsRef.value.opts = []; } async function setProvince(id, data) { - const newProvince = provincesOptions.value.find((province) => province.id == id); - if (!newProvince) return; - + const newProvince = findProvince(id); + if (!newProvince) { + data.townFk = null; + return; + } + whereTowns.value.provinceFk.inq = [id]; + // handleTowns([newProvince]); data.countryFk = newProvince.countryFk; } @@ -87,13 +96,26 @@ async function onProvinceCreated(data) { postcodeFormData.provinceFk = data.id; } -const whereTowns = computed(() => { - return { - provinceFk: { - inq: provincesOptions.value.map(({ id }) => id), - }, - }; -}); +const whereTowns = ref({}); +// const whereTowns = ref(() => { +// if (!postcodeFormData.provinceFk) return { provinceFk: { inq: null } }; +// if (postcodeFormData.provinceFk) +// return { +// provinceFk: { +// inq: [postcodeFormData.provinceFk], //provincesOptions.value.map(({ id }) => id), +// }, +// }; + +// return { +// provinceFk: { +// inq: mapData(provincesOptions.value), +// }, +// }; +// }); +function mapData(data) { + if (data.length === 0) return data; + return data.map(({ id }) => id); +}