diff --git a/src/frontend/src/common.js b/src/frontend/src/common.js index 9cf4558..9d1fda8 100644 --- a/src/frontend/src/common.js +++ b/src/frontend/src/common.js @@ -1,15 +1,17 @@ -export const parseNumberFromString = (value, decimals = 2) => { +export const parseNumberFromString = (value, decimals = 2, allowZero = false) => { if (typeof value === 'number') return value; + if(typeof value === 'string' && value.trim().length === 0) return null; if (!value || typeof value !== 'string') return 0; + /* determine decimal separator */ const lastDot = value.lastIndexOf('.'); const lastComma = value.lastIndexOf(','); const decimalSeparator = (lastDot > lastComma) ? '.' : ','; - const hasSeperator = !(lastDot === -1 && lastComma === -1); + const hasSeparator = !(lastDot === -1 && lastComma === -1); - if (hasSeperator) { + if (hasSeparator) { /* remove all digit grouping */ value = (decimalSeparator === '.') ? value.replace(',', '') : value.replace('.', ''); @@ -23,11 +25,11 @@ export const parseNumberFromString = (value, decimals = 2) => { const parsed = parseFloat(normalizedValue); if(parsed > 10e10) - return 0; + return null; - - if (isNaN(parsed)) return 0; - return Math.round(parsed * Math.pow(10, decimals)) / Math.pow(10, decimals); + if (isNaN(parsed)) return null; + const final = Math.round(parsed * Math.pow(10, decimals)) / Math.pow(10, decimals); + return (final === 0 && !allowZero) ? null : final; } export class UrlSafeBase64 { diff --git a/src/frontend/src/components/layout/edit/MaterialEdit.vue b/src/frontend/src/components/layout/edit/MaterialEdit.vue index 499e80a..865d270 100644 --- a/src/frontend/src/components/layout/edit/MaterialEdit.vue +++ b/src/frontend/src/components/layout/edit/MaterialEdit.vue @@ -93,7 +93,7 @@ export default { computed: { ...mapStores(useMaterialStore, useCustomsStore), tariffRatePercent() { - return this.tariffRate ? (this.tariffRate * 100).toFixed(2) : ''; + return ((this.tariffRate ?? null) !== null) ? (this.tariffRate * 100).toFixed(2) : ''; } }, data() { @@ -134,16 +134,16 @@ export default { }); }, validateTariffRate(event) { - const percentValue = parseNumberFromString(event.target.value, 4); + const percentValue = parseNumberFromString(event.target.value, 2, true); - const validatedPercent = Math.max(0, Math.min(999.99, percentValue)); - const validatedDecimal = validatedPercent / 100; + const validatedPercent = (percentValue === null) ? null : Math.max(0, Math.min(999.99, percentValue)); + const validatedDecimal = (validatedPercent === null) ? null : validatedPercent / 100; if (validatedDecimal !== this.tariffRate) { this.$emit('update:tariffRate', validatedDecimal); } - event.target.value = validatedPercent.toFixed(2); + event.target.value = validatedPercent === null ? null : validatedPercent.toFixed(2); }, activateEditMode() { this.modalSelectMaterial = true; diff --git a/src/frontend/src/components/layout/edit/PackagingEdit.vue b/src/frontend/src/components/layout/edit/PackagingEdit.vue index b90d0ae..acc1a02 100644 --- a/src/frontend/src/components/layout/edit/PackagingEdit.vue +++ b/src/frontend/src/components/layout/edit/PackagingEdit.vue @@ -153,20 +153,15 @@ export default { const unitType = this.huDimensionUnits.find(unit => unit.id === value)?.value; const decimals = (unitType === 'cm') ? 2 : ((unitType === 'm') ? 3 : 0); - if (this.length) { - const parsedLength = parseFloat(this.length.toFixed(decimals)); + const parsedLength = (this.length ?? null) === null ? null : parseFloat(this.length.toFixed(decimals)); this.$emit('update:length', parsedLength); - } - if (this.height) { - const parsedHeight = parseFloat(this.height.toFixed(decimals)); + const parsedHeight = (this.height ?? null) === null ? null : parseFloat(this.height.toFixed(decimals)); this.$emit('update:height', parsedHeight); - } - if (this.weight) { - const parsedWidth = parseFloat(this.width.toFixed(decimals)); + const parsedWidth = (this.width ?? null) === null ? null : parseFloat(this.width.toFixed(decimals)); this.$emit('update:width', parsedWidth); - } + this.$emit('update:dimensionUnit', unitType); } @@ -206,32 +201,31 @@ export default { }, validateDimension(type, event) { const decimals = (this.huDimensionUnitSelected === 2) ? 2 : ((this.huDimensionUnitSelected === 3) ? 3 : 0); - const parsed = parseNumberFromString(event.target.value, decimals); - + const parsed = event.target.value.trim().length === 0 ? null : parseNumberFromString(event.target.value, decimals); this.$emit(`update:${type}`, parsed); // Force update the input field with the correctly formatted value - const formattedValue = parsed.toFixed(decimals); + const formattedValue = parsed ? parsed.toFixed(decimals) : ''; const inputRef = `${type}Input`; this.updateInputValue(inputRef, formattedValue); }, validateWeight(type, event) { const decimals = (this.huWeightUnitSelected === 2) ? 3 : ((this.huWeightUnitSelected === 3) ? 6 : 0); - const parsed = parseNumberFromString(event.target.value, decimals); + const parsed = event.target.value.trim().length === 0 ? null : parseNumberFromString(event.target.value, decimals); this.$emit('update:weight', parsed); // Force update the input field with the correctly formatted value - const formattedValue = parsed.toFixed(decimals); + const formattedValue = parsed ? parsed.toFixed(decimals) : ''; this.updateInputValue('weightInput', formattedValue); }, validateCount(event) { - const parsed = parseNumberFromString(event.target.value, 0); + const parsed = event.target.value.trim().length === 0 ? null : parseNumberFromString(event.target.value, 0); this.$emit('update:unitCount', parsed); // Force update the input field with the correctly formatted value - const formattedValue = parsed.toString(); + const formattedValue = parsed ? parsed.toString() : ''; this.updateInputValue('unitCountInput', formattedValue); }, updateStackable(value) { diff --git a/src/frontend/src/components/layout/edit/PriceEdit.vue b/src/frontend/src/components/layout/edit/PriceEdit.vue index 39ecdd5..6c0b112 100644 --- a/src/frontend/src/components/layout/edit/PriceEdit.vue +++ b/src/frontend/src/components/layout/edit/PriceEdit.vue @@ -56,7 +56,7 @@ export default { return this.price?.toFixed(2) ?? ''; }, overSeaSharePercent() { - return this.overSeaShare ? (this.overSeaShare * 100).toFixed(2) : ''; + return ((this.overSeaShare ?? null) !== null) ? (this.overSeaShare * 100).toFixed(2) : ''; } }, methods: { @@ -66,29 +66,33 @@ export default { } }, validatePrice(event) { - const value = parseNumberFromString(event.target.value, 2); - const validatedValue = Math.max(0, value); + const value = (event.target.value.trim().length === 0) ? null : parseNumberFromString(event.target.value, 2); + + const validatedValue = value === null ? null : Math.max(0, value); if (validatedValue !== this.price) { this.$emit('update:price', validatedValue); } - event.target.value = validatedValue.toFixed(2); + event.target.value = validatedValue?.toFixed(2) ?? ''; }, updateIncludeFcaFee(value) { this.$emit('update:includeFcaFee', value); }, validateOverSeaShare(event) { - const percentValue = parseNumberFromString(event.target.value, 4); - const validatedPercent = Math.max(0, Math.min(100, percentValue)); - const validatedDecimal = validatedPercent / 100; + const percentValue = (event.target.value.trim().length === 0) ? null : parseNumberFromString(event.target.value, 2, true); + + + const validatedPercent = percentValue === null ? null : Math.max(0, Math.min(100, percentValue)); + const validatedDecimal = validatedPercent === null ? null :validatedPercent / 100; if (validatedDecimal !== this.overSeaShare) { this.$emit('update:overSeaShare', validatedDecimal); } - event.target.value = validatedPercent.toFixed(2); + + event.target.value = validatedPercent?.toFixed(2) ?? ''; } } } diff --git a/src/frontend/src/pages/CalculationSingleEdit.vue b/src/frontend/src/pages/CalculationSingleEdit.vue index 0c10ec6..d74b85f 100644 --- a/src/frontend/src/pages/CalculationSingleEdit.vue +++ b/src/frontend/src/pages/CalculationSingleEdit.vue @@ -169,17 +169,16 @@ export default { success = await this.premiseEditStore.savePackaging(); } - console.log("save success: ", success); - if(success) { - this.$refs.toast.addToast({ - icon: 'floppy-disk', - message: `Changes on ${type} saved.`, - - variant: 'primary', - duration: 3000 - }) - } + // if(success) { + // this.$refs.toast.addToast({ + // icon: 'floppy-disk', + // message: `Changes on ${type} saved.`, + // + // variant: 'primary', + // duration: 3000 + // }) + // } }, updateMaterial(id, action) {