From c21424f55d3c9034caa8e7bb7ba506f820025558 Mon Sep 17 00:00:00 2001 From: Net Date: Thu, 25 Jul 2024 16:39:51 +0700 Subject: [PATCH] refactor: by Value properties --- .../04_product-service/ServiceProperties.vue | 187 +++++++++++++----- 1 file changed, 138 insertions(+), 49 deletions(-) diff --git a/src/components/04_product-service/ServiceProperties.vue b/src/components/04_product-service/ServiceProperties.vue index cf144068..e8dde6ff 100644 --- a/src/components/04_product-service/ServiceProperties.vue +++ b/src/components/04_product-service/ServiceProperties.vue @@ -2,18 +2,18 @@ import { ref } from 'vue'; import { useI18n } from 'vue-i18n'; import { Option } from 'src/stores/options/types'; -import { - AdditionalType, - Attributes, - Ability, -} from 'src/stores/product-service/types'; +import { Attributes } from 'src/stores/product-service/types'; import { moveItemUp, moveItemDown, deleteItem, dialog } from 'src/stores/utils'; import NoData from '../NoData.vue'; +import useOptionStore from 'src/stores/options'; const { t } = useI18n(); -const propertiesOption = defineModel('propertiesOption'); +const propertiesOption = + defineModel<(Option & { type: Attributes['additional'][number]['type'] })[]>( + 'propertiesOption', + ); const formServiceProperties = defineModel('formServiceProperties'); // const abilitys = ref>({ @@ -23,15 +23,6 @@ const formServiceProperties = defineModel('formServiceProperties'); // array: ['item1', 'item2'], // }); -const abilitys = ref([ - { - string: { phoneNumber: { length: 10 } }, - number: { comma: true, decimal: { point: 2 } }, - date: '2024-07-25', - array: ['item1', 'item2'], - }, -]); - const telMax = ref(); const pointNum = ref(); @@ -67,8 +58,11 @@ const typeOption = ref([ }, ]); -function manageProperties(properties?: string, type?: string | AdditionalType) { - if (properties === 'all' && propertiesOption.value) { +function manageProperties( + property: string, + propertyType?: Attributes['additional'][number]['type'], +) { + if (property === 'all' && propertiesOption.value) { if ( formServiceProperties.value?.additional.length === propertiesOption.value.length @@ -76,32 +70,77 @@ function manageProperties(properties?: string, type?: string | AdditionalType) { formServiceProperties.value.additional = []; return; } - propertiesOption.value.forEach((ops) => { - const exists = formServiceProperties.value?.additional.some( - (prop) => prop.fieldName === ops.value, - ); - if (!exists) { + + for (const ops of propertiesOption.value) { + if ( + formServiceProperties.value?.additional.some( + (prop) => prop.fieldName === ops.value, + ) + ) { + continue; + } + + if (ops.type === 'array' || ops.type === 'date') { formServiceProperties.value?.additional.push({ + type: ops.type, fieldName: ops.value, - type: ops.type as AdditionalType, }); } - }); + + if (ops.type === 'string') { + formServiceProperties.value?.additional.push({ + type: ops.type, + fieldName: ops.value, + isPhoneNumber: false, + phoneNumberLength: 10, + }); + } + + if (ops.type === 'number') { + formServiceProperties.value?.additional.push({ + type: ops.type, + fieldName: ops.value, + comma: false, + decimalPlace: 2, + }); + } + } + return; } if (formServiceProperties.value) { const propertyIndex = formServiceProperties.value.additional.findIndex( - (prop) => prop.fieldName === properties, + (prop) => prop.fieldName === property, ); if (propertyIndex !== -1) { formServiceProperties.value.additional.splice(propertyIndex, 1); } else { - formServiceProperties.value.additional.push({ - fieldName: properties ?? null, - type: type as AdditionalType, - }); + if (propertyType === 'array' || propertyType === 'date') { + formServiceProperties.value?.additional.push({ + type: propertyType, + fieldName: property, + }); + } + + if (propertyType === 'string') { + formServiceProperties.value?.additional.push({ + type: propertyType, + fieldName: property, + isPhoneNumber: false, + phoneNumberLength: 10, + }); + } + + if (propertyType === 'number') { + formServiceProperties.value?.additional.push({ + type: propertyType, + fieldName: property, + comma: false, + decimalPlace: 2, + }); + } } } } @@ -115,18 +154,45 @@ function shouldShowItem(opt: Option) { } } -function changeType(val: string) { +function changeType(fieldName: string) { if (!propertiesOption.value) return; - const newType = propertiesOption.value.find((op) => op.value === val)?.type; - if (!newType) return; + const defaultPropType = propertiesOption.value.find( + (op) => op.value === fieldName, + )?.type; - const additionalField = formServiceProperties.value?.additional.find( - (l) => l.fieldName === val, + if (!defaultPropType) return; + + const idx = formServiceProperties.value?.additional.findIndex( + (v) => v.fieldName === fieldName, ); - if (!additionalField) return; - additionalField.type = newType as AdditionalType; + if (!idx) return; + + if (defaultPropType === 'array' || defaultPropType === 'date') { + formServiceProperties.value?.additional.push({ + type: defaultPropType, + fieldName, + }); + } + + if (defaultPropType === 'string') { + formServiceProperties.value?.additional.push({ + type: defaultPropType, + fieldName, + isPhoneNumber: false, + phoneNumberLength: 10, + }); + } + + if (defaultPropType === 'number') { + formServiceProperties.value?.additional.push({ + type: defaultPropType, + fieldName, + comma: false, + decimalPlace: 2, + }); + } } function confirmDelete(items: unknown[], index: number) { @@ -142,17 +208,6 @@ function confirmDelete(items: unknown[], index: number) { cancel: () => {}, }); } - -// function addProperties() { -// formServiceProperties.value?.additional.forEach((item, i) => { -// if (!!formServiceProperties.value?.additional) { -// formServiceProperties.value.additional[i] = { -// ...item, -// ability: abilitys.value[i], -// }; -// } -// }); -// }