0
0
Fork 0

Merge pull request '#8061 Improve_newCP' (!1016) from 8061_improve_newCP into dev

Reviewed-on: verdnatura/salix-front#1016
Reviewed-by: Jon Elias <jon@verdnatura.es>
This commit is contained in:
Javier Segarra 2024-11-28 05:52:26 +00:00
commit c287c04894
4 changed files with 42 additions and 88 deletions

View File

@ -47,11 +47,13 @@ const onDataSaved = (...args) => {
:label="t('Name')" :label="t('Name')"
v-model="data.name" v-model="data.name"
:rules="validate('city.name')" :rules="validate('city.name')"
required
/> />
<VnSelectProvince <VnSelectProvince
:province-selected="$props.provinceSelected" :province-selected="$props.provinceSelected"
:country-fk="$props.countryFk" :country-fk="$props.countryFk"
v-model="data.provinceFk" v-model="data.provinceFk"
required
/> />
</VnRow> </VnRow>
</template> </template>

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { reactive, ref } from 'vue'; import { computed, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import FetchData from 'components/FetchData.vue'; import FetchData from 'components/FetchData.vue';
@ -23,11 +23,10 @@ const postcodeFormData = reactive({
}); });
const townsFetchDataRef = ref(false); const townsFetchDataRef = ref(false);
const countriesFetchDataRef = ref(false); const countriesRef = ref(false);
const townsRef = ref(false);
const provincesFetchDataRef = ref(false); const provincesFetchDataRef = ref(false);
const countriesOptions = ref([]);
const provincesOptions = ref([]); const provincesOptions = ref([]);
const townsOptions = ref([]);
const town = ref({}); const town = ref({});
const townFilter = ref({}); const townFilter = ref({});
const countryFilter = ref({}); const countryFilter = ref({});
@ -42,7 +41,7 @@ function onDataSaved(formData) {
({ id }) => id === formData.provinceFk ({ id }) => id === formData.provinceFk
); );
newPostcode.province = provinceObject?.name; newPostcode.province = provinceObject?.name;
const countryObject = countriesOptions.value.find( const countryObject = countriesRef.value.opts.find(
({ id }) => id === formData.countryFk ({ id }) => id === formData.countryFk
); );
newPostcode.country = countryObject?.name; newPostcode.country = countryObject?.name;
@ -70,49 +69,8 @@ async function setCountry(countryFk, data) {
data.countryFk = countryFk; data.countryFk = countryFk;
} }
async function filterTowns(name) {
if (name !== '') {
townFilter.value.where = {
name: {
like: `%${name}%`,
},
};
await townsFetchDataRef.value?.fetch();
}
}
async function filterCountries(name) {
if (name !== '') {
countryFilter.value.where = {
name: {
like: `%${name}%`,
},
};
await countriesFetchDataRef.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 handleProvinces(data) { async function handleProvinces(data) {
provincesOptions.value = data; provincesOptions.value = data;
if (postcodeFormData.countryFk) {
await fetchTowns(postcodeFormData.countryFk);
}
}
async function handleTowns(data) {
townsOptions.value = data;
}
async function handleCountries(data) {
countriesOptions.value = data;
} }
async function setProvince(id, data) { async function setProvince(id, data) {
@ -128,6 +86,14 @@ async function onProvinceCreated(data) {
}); });
postcodeFormData.provinceFk = data.id; postcodeFormData.provinceFk = data.id;
} }
const whereTowns = computed(() => {
return {
provinceFk: {
inq: provincesOptions.value.map(({ id }) => id),
},
};
});
</script> </script>
<template> <template>
@ -139,24 +105,6 @@ async function onProvinceCreated(data) {
auto-load auto-load
url="Provinces/location" url="Provinces/location"
/> />
<FetchData
ref="townsFetchDataRef"
:sort-by="['name ASC']"
:limit="30"
:filter="townFilter"
@on-fetch="handleTowns"
auto-load
url="Towns/location"
/>
<FetchData
ref="countriesFetchDataRef"
:limit="30"
:filter="countryFilter"
:sort-by="['name ASC']"
@on-fetch="handleCountries"
auto-load
url="Countries"
/>
<FormModelPopup <FormModelPopup
url-create="postcodes" url-create="postcodes"
@ -174,20 +122,26 @@ async function onProvinceCreated(data) {
v-model="data.code" v-model="data.code"
:rules="validate('postcode.code')" :rules="validate('postcode.code')"
clearable clearable
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)"
@filter="filterTowns"
:tooltip="t('Create city')" :tooltip="t('Create city')"
v-model="data.townFk" v-model="data.townFk"
:options="townsOptions"
option-label="name" option-label="name"
option-value="id" option-value="id"
: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"
:clearable="true" :clearable="true"
required
> >
<template #option="{ itemProps, opt }"> <template #option="{ itemProps, opt }">
<QItem v-bind="itemProps"> <QItem v-bind="itemProps">
@ -220,13 +174,18 @@ async function onProvinceCreated(data) {
v-model="data.provinceFk" v-model="data.provinceFk"
@on-province-fetched="handleProvinces" @on-province-fetched="handleProvinces"
@on-province-created="onProvinceCreated" @on-province-created="onProvinceCreated"
required
/> />
<VnSelect <VnSelect
ref="countriesRef"
:limit="30"
:filter="countryFilter"
:sort-by="['name ASC']"
auto-load
url="Countries"
required
:label="t('Country')" :label="t('Country')"
@update:options="handleCountries"
:options="countriesOptions"
hide-selected hide-selected
@filter="filterCountries"
option-label="name" option-label="name"
option-value="id" option-value="id"
v-model="data.countryFk" v-model="data.countryFk"

View File

@ -2,7 +2,6 @@
import { computed, reactive, ref } from 'vue'; import { computed, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import FetchData from 'components/FetchData.vue';
import VnRow from 'components/ui/VnRow.vue'; import VnRow from 'components/ui/VnRow.vue';
import VnSelect from 'src/components/common/VnSelect.vue'; import VnSelect from 'src/components/common/VnSelect.vue';
import VnInput from 'src/components/common/VnInput.vue'; import VnInput from 'src/components/common/VnInput.vue';
@ -21,15 +20,11 @@ const $props = defineProps({
type: Number, type: Number,
default: null, default: null,
}, },
provinces: {
type: Array,
default: () => [],
},
}); });
const autonomiesOptions = ref([]); const autonomiesRef = ref([]);
const onDataSaved = (dataSaved, requestResponse) => { const onDataSaved = (dataSaved, requestResponse) => {
requestResponse.autonomy = autonomiesOptions.value.find( requestResponse.autonomy = autonomiesRef.value.opts.find(
(autonomy) => autonomy.id == requestResponse.autonomyFk (autonomy) => autonomy.id == requestResponse.autonomyFk
); );
emit('onDataSaved', dataSaved, requestResponse); emit('onDataSaved', dataSaved, requestResponse);
@ -43,16 +38,6 @@ const where = computed(() => {
</script> </script>
<template> <template>
<FetchData
@on-fetch="(data) => (autonomiesOptions = data)"
auto-load
:filter="{
where,
}"
url="Autonomies/location"
:sort-by="['name ASC']"
:limit="30"
/>
<FormModelPopup <FormModelPopup
:title="t('New province')" :title="t('New province')"
:subtitle="t('Please, ensure you put the correct data!')" :subtitle="t('Please, ensure you put the correct data!')"
@ -67,10 +52,17 @@ const where = computed(() => {
:label="t('Name')" :label="t('Name')"
v-model="data.name" v-model="data.name"
:rules="validate('province.name')" :rules="validate('province.name')"
required
/> />
<VnSelect <VnSelect
required
ref="autonomiesRef"
auto-load
:where="where"
url="Autonomies/location"
:sort-by="['name ASC']"
:limit="30"
:label="t('Autonomy')" :label="t('Autonomy')"
:options="autonomiesOptions"
hide-selected hide-selected
option-label="name" option-label="name"
option-value="id" option-value="id"

View File

@ -86,9 +86,10 @@ Cypress.Commands.add('getValue', (selector) => {
}); });
// Fill Inputs // Fill Inputs
Cypress.Commands.add('selectOption', (selector, option) => { Cypress.Commands.add('selectOption', (selector, option, timeout) => {
cy.waitForElement(selector); cy.waitForElement(selector);
cy.get(selector).click(); cy.get(selector).click();
cy.wait(timeout || 1000);
cy.get('.q-menu .q-item').contains(option).click(); cy.get('.q-menu .q-item').contains(option).click();
}); });
Cypress.Commands.add('countSelectOptions', (selector, option) => { Cypress.Commands.add('countSelectOptions', (selector, option) => {