feat: improve Merge branch 'test' into dev

This commit is contained in:
Javier Segarra 2024-11-29 07:32:31 +01:00
parent 7315d117c0
commit b83c640760
1 changed files with 52 additions and 24 deletions

View File

@ -22,13 +22,12 @@ const postcodeFormData = reactive({
townFk: null, townFk: null,
}); });
const townsFetchDataRef = ref(false);
const countriesRef = ref(false); const countriesRef = ref(false);
const townsRef = ref(false); const townsRef = ref(false);
const provincesFetchDataRef = ref(false); const provincesFetchDataRef = ref(false);
const provincesOptions = ref([]); const provincesOptions = ref([]);
const townsOptions = ref([]);
const town = ref({}); const town = ref({});
const townFilter = ref({});
const countryFilter = ref({}); const countryFilter = ref({});
function onDataSaved(formData) { function onDataSaved(formData) {
@ -50,13 +49,13 @@ function onDataSaved(formData) {
async function onCityCreated(newTown, formData) { async function onCityCreated(newTown, formData) {
await provincesFetchDataRef.value.fetch(); await provincesFetchDataRef.value.fetch();
newTown.province = provincesOptions.value.find( newTown.province = findProvince(newTown.provinceFk);
(province) => province.id === newTown.provinceFk
);
formData.townFk = newTown; formData.townFk = newTown;
setTown(newTown, formData); setTown(newTown, formData);
} }
function findProvince(provinceId) {
return provincesOptions.value.find(({ id }) => id === provinceId);
}
function setTown(newTown, data) { function setTown(newTown, data) {
town.value = newTown; town.value = newTown;
data.provinceFk = newTown?.provinceFk ?? newTown; data.provinceFk = newTown?.provinceFk ?? newTown;
@ -71,12 +70,22 @@ async function setCountry(countryFk, data) {
async function handleProvinces(data) { async function handleProvinces(data) {
provincesOptions.value = 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) { async function setProvince(id, data) {
const newProvince = provincesOptions.value.find((province) => province.id == id); const newProvince = findProvince(id);
if (!newProvince) return; if (!newProvince) {
data.townFk = null;
return;
}
whereTowns.value.provinceFk.inq = [id];
// handleTowns([newProvince]);
data.countryFk = newProvince.countryFk; data.countryFk = newProvince.countryFk;
} }
@ -87,13 +96,26 @@ async function onProvinceCreated(data) {
postcodeFormData.provinceFk = data.id; postcodeFormData.provinceFk = data.id;
} }
const whereTowns = computed(() => { const whereTowns = ref({});
return { // const whereTowns = ref(() => {
provinceFk: { // if (!postcodeFormData.provinceFk) return { provinceFk: { inq: null } };
inq: provincesOptions.value.map(({ id }) => id), // 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);
}
</script> </script>
<template> <template>
@ -101,10 +123,19 @@ const whereTowns = computed(() => {
ref="provincesFetchDataRef" ref="provincesFetchDataRef"
@on-fetch="handleProvinces" @on-fetch="handleProvinces"
:sort-by="['name ASC']" :sort-by="['name ASC']"
:limit="30" :limit="2"
auto-load auto-load
url="Provinces/location" url="Provinces/location"
/> />
<FetchData
ref="townsRef"
@on-fetch="handleTowns"
:sort-by="['name ASC']"
:limit="2"
auto-load
:where="whereTowns"
url="Towns/location"
/>
<FormModelPopup <FormModelPopup
url-create="postcodes" url-create="postcodes"
@ -116,6 +147,7 @@ const whereTowns = computed(() => {
@on-data-saved="onDataSaved" @on-data-saved="onDataSaved"
> >
<template #form-inputs="{ data, validate }"> <template #form-inputs="{ data, validate }">
{{ whereTowns }}
<VnRow> <VnRow>
<VnInput <VnInput
:label="t('Postcode')" :label="t('Postcode')"
@ -125,18 +157,14 @@ const whereTowns = computed(() => {
required required
/> />
<VnSelectDialog <VnSelectDialog
ref="townsRef"
:sort-by="['name ASC']"
:limit="30"
auto-load
url="Towns/location"
:where="whereTowns"
:label="t('City')" :label="t('City')"
@update:model-value="(value) => setTown(value, data)" @update:model-value="(value) => setTown(value, data)"
:tooltip="t('Create city')" :tooltip="t('Create city')"
v-model="data.townFk" v-model="data.townFk"
option-label="name" option-label="name"
:options="townsOptions"
option-value="id" option-value="id"
@input-value="(evt) => (whereTowns.name = { like: `%${evt}%` })"
:rules="validate('postcode.city')" :rules="validate('postcode.city')"
:acls="[{ model: 'Town', props: '*', accessType: 'WRITE' }]" :acls="[{ model: 'Town', props: '*', accessType: 'WRITE' }]"
:emit-value="false" :emit-value="false"
@ -178,7 +206,7 @@ const whereTowns = computed(() => {
/> />
<VnSelect <VnSelect
ref="countriesRef" ref="countriesRef"
:limit="30" :limit="2"
:filter="countryFilter" :filter="countryFilter"
:sort-by="['name ASC']" :sort-by="['name ASC']"
auto-load auto-load