From 271e33a999d54cf06788aa41dce6164658aaf6ab Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 20 Sep 2024 11:57:15 +0200 Subject: [PATCH 01/65] feat: formatLocation when field is null --- src/components/common/VnLocation.vue | 40 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/components/common/VnLocation.vue b/src/components/common/VnLocation.vue index b6cbfbafd..c3b07c72c 100644 --- a/src/components/common/VnLocation.vue +++ b/src/components/common/VnLocation.vue @@ -12,14 +12,46 @@ const props = defineProps({ default: null, }, }); +const formatLocation = (obj, properties) => { + // Crear un array con las propiedades del objeto + const parts = properties.map((prop) => { + if (typeof prop === 'string') { + return obj[prop]; + } else if (typeof prop === 'function') { + return prop(obj); + } + return null; + }); + + // Filtrar los valores que no son null o undefined + const filteredParts = parts.filter( + (part) => part !== null && part !== undefined && part !== '' + ); + + // Unir los valores filtrados con el formato deseado + return filteredParts.join(', '); +}; + +const locationProperties = [ + 'postcode', + (obj) => + obj.city + ? `${obj.city}${obj.province?.name ? `(${obj.province.name})` : ''}` + : null, + (obj) => obj.country?.name, +]; const modelValue = ref( - props.location - ? `${props.location?.postcode} - ${props.location?.city}(${props.location?.province?.name}), ${props.location?.country?.name}` - : null + props.location ? formatLocation(props.location, locationProperties) : null ); function showLabel(data) { - return `${data.code} - ${data.town}(${data.province}), ${data.country}`; + const dataProperties = [ + 'code', + (obj) => (obj.town ? `${obj.town}(${obj.province})` : null), + 'country', + ]; + return formatLocation(data, dataProperties); } + const handleModelValue = (data) => { emit('update:model-value', data); }; From b822c7722bb3176120c87aa74abbb7c2c33ec88d Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 20 Sep 2024 13:45:43 +0200 Subject: [PATCH 02/65] feat: formatLocation when field is null --- src/components/common/VnLocation.vue | 68 +++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/src/components/common/VnLocation.vue b/src/components/common/VnLocation.vue index 8256ec5b0..c1b921915 100644 --- a/src/components/common/VnLocation.vue +++ b/src/components/common/VnLocation.vue @@ -2,21 +2,68 @@ import CreateNewPostcode from 'src/components/CreateNewPostcodeForm.vue'; import VnSelectDialog from 'components/common/VnSelectDialog.vue'; import { useI18n } from 'vue-i18n'; - +import { ref } from 'vue'; const { t } = useI18n(); -const value = defineModel({ type: [String, Number, Object] }); +const emit = defineEmits(['update:model-value', 'update:options']); + +const props = defineProps({ + location: { + type: Object, + default: null, + }, +}); +const formatLocation = (obj, properties) => { + const parts = properties.map((prop) => { + if (typeof prop === 'string') { + return obj[prop]; + } else if (typeof prop === 'function') { + return prop(obj); + } + return null; + }); + + const filteredParts = parts.filter( + (part) => part !== null && part !== undefined && part !== '' + ); + + return filteredParts.join(', '); +}; + +const locationProperties = [ + 'postcode', + (obj) => + obj.city + ? `${obj.city}${obj.province?.name ? `(${obj.province.name})` : ''}` + : null, + (obj) => obj.country?.name, +]; + +const modelValue = ref( + props.location ? formatLocation(props.location, locationProperties) : null +); + +const handleModelValue = (data) => { + emit('update:model-value', data); +}; function showLabel(data) { - return `${data.code} - ${data.town}(${data.province}), ${data.country}`; + const dataProperties = [ + 'code', + (obj) => (obj.town ? `${obj.town}(${obj.province})` : null), + 'country', + ]; + return formatLocation(data, dataProperties); } -