diff --git a/src/boot/axios.js b/src/boot/axios.js index fa8a08003..9b32275bd 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -43,6 +43,15 @@ const onResponseError = (error) => { } switch (response?.status) { + case 422: + if (error.name == 'ValidationError') + message += + ' "' + + responseError.details.context + + '.' + + Object.keys(responseError.details.codes).join(',') + + '"'; + break; case 500: message = 'errors.statusInternalServerError'; break; diff --git a/src/components/CreateNewCityForm.vue b/src/components/CreateNewCityForm.vue index 48e76d885..9a7d8666c 100644 --- a/src/components/CreateNewCityForm.vue +++ b/src/components/CreateNewCityForm.vue @@ -4,8 +4,8 @@ import { useI18n } from 'vue-i18n'; import FetchData from 'components/FetchData.vue'; import VnRow from 'components/ui/VnRow.vue'; -import VnSelect from 'src/components/common/VnSelect.vue'; -import VnInput from 'src/components/common/VnInput.vue'; +import VnSelectProvince from 'components/VnSelectProvince.vue'; +import VnInput from 'components/common/VnInput.vue'; import FormModelPopup from './FormModelPopup.vue'; const emit = defineEmits(['onDataSaved']); @@ -19,8 +19,8 @@ const cityFormData = reactive({ const provincesOptions = ref([]); -const onDataSaved = (dataSaved) => { - emit('onDataSaved', dataSaved); +const onDataSaved = (...args) => { + emit('onDataSaved', ...args); }; @@ -36,7 +36,7 @@ const onDataSaved = (dataSaved) => { :form-initial-data="cityFormData" url-create="towns" model="city" - @on-data-saved="onDataSaved($event)" + @on-data-saved="onDataSaved" > diff --git a/src/components/CreateNewPostcodeForm.vue b/src/components/CreateNewPostcodeForm.vue index 7ea171e53..e6247226e 100644 --- a/src/components/CreateNewPostcodeForm.vue +++ b/src/components/CreateNewPostcodeForm.vue @@ -5,9 +5,9 @@ import { useI18n } from 'vue-i18n'; import FetchData from 'components/FetchData.vue'; import VnRow from 'components/ui/VnRow.vue'; import VnSelect from 'src/components/common/VnSelect.vue'; +import VnSelectProvince from 'src/components/VnSelectProvince.vue'; import VnInput from 'src/components/common/VnInput.vue'; import CreateNewCityForm from './CreateNewCityForm.vue'; -import CreateNewProvinceForm from './CreateNewProvinceForm.vue'; import VnSelectDialog from 'components/common/VnSelectDialog.vue'; import FormModelPopup from './FormModelPopup.vue'; @@ -22,20 +22,17 @@ const postcodeFormData = reactive({ townFk: null, }); -const townsFetchDataRef = ref(null); const provincesFetchDataRef = ref(null); const countriesOptions = ref([]); const provincesOptions = ref([]); -const townsLocationOptions = ref([]); +const town = ref({}); -const onDataSaved = (formData) => { +function onDataSaved(formData) { const newPostcode = { ...formData, }; - const townObject = townsLocationOptions.value.find( - ({ id }) => id === formData.townFk - ); - newPostcode.town = townObject?.name; + newPostcode.town = town.value.name; + newPostcode.townFk = town.value.id; const provinceObject = provincesOptions.value.find( ({ id }) => id === formData.provinceFk ); @@ -43,39 +40,40 @@ const onDataSaved = (formData) => { const countryObject = countriesOptions.value.find( ({ id }) => id === formData.countryFk ); - newPostcode.country = countryObject?.country; + newPostcode.country = countryObject?.name; emit('onDataSaved', newPostcode); -}; +} -const onCityCreated = async ({ name, provinceFk }, formData) => { - await townsFetchDataRef.value.fetch(); - formData.townFk = townsLocationOptions.value.find((town) => town.name === name).id; - formData.provinceFk = provinceFk; - formData.countryFk = provincesOptions.value.find( - (province) => province.id === provinceFk - ).countryFk; -}; +async function onCityCreated(newTown, formData) { + newTown.province = provincesOptions.value.find( + (province) => province.id === newTown.provinceFk + ); + formData.townFk = newTown; + setTown(newTown, formData); +} -const onProvinceCreated = async ({ name }, formData) => { +function setTown(newTown, data) { + if (!newTown) return; + town.value = newTown; + data.provinceFk = newTown.provinceFk; + data.countryFk = newTown.province.countryFk; +} + +async function setProvince(id, data) { await provincesFetchDataRef.value.fetch(); - formData.provinceFk = provincesOptions.value.find( - (province) => province.name === name - ).id; -}; + const newProvince = provincesOptions.value.find((province) => province.id == id); + if (!newProvince) return; + + data.countryFk = newProvince.countryFk; +}