code refactor

This commit is contained in:
MaysWind
2023-08-19 23:31:03 +08:00
parent b8bdb03fc0
commit b8acff3e7c
7 changed files with 100 additions and 49 deletions
+38 -16
View File
@@ -1,9 +1,12 @@
<template> <template>
<v-text-field :type="hide ? 'password' : 'number'" :class="extraClass" <v-text-field class="text-field-with-colored-label"
:type="hide ? 'password' : 'number'" :class="extraClass"
:color="!readonly && !disabled ? color : undefined"
:base-color="!readonly && !disabled ? color : undefined"
:density="density" :readonly="!!readonly" :disabled="!!disabled" :density="density" :readonly="!!readonly" :disabled="!!disabled"
:label="label" :placeholder="placeholder" :label="label" :placeholder="placeholder"
:persistent-placeholder="!!persistentPlaceholder" :persistent-placeholder="!!persistentPlaceholder"
:rules="enableRules ? rules : []" v-model="value" :rules="enableRules ? rules : []" v-model="currentValue"
@keydown="onKeyUpDown" @keyup="onKeyUpDown"> @keydown="onKeyUpDown" @keyup="onKeyUpDown">
<template #prepend-inner v-if="currency && prependText"> <template #prepend-inner v-if="currency && prependText">
<div style="margin-top: 2px">{{ prependText }}</div> <div style="margin-top: 2px">{{ prependText }}</div>
@@ -19,10 +22,12 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js'; import { useSettingsStore } from '@/stores/setting.js';
import transactionConstants from '@/consts/transaction.js'; import transactionConstants from '@/consts/transaction.js';
import { numericCurrencyToString, stringCurrencyToNumeric } from '@/lib/currency.js';
export default { export default {
props: [ props: [
'class', 'class',
'color',
'density', 'density',
'currency', 'currency',
'label', 'label',
@@ -41,6 +46,7 @@ export default {
const self = this; const self = this;
return { return {
currentValue: numericCurrencyToString(self.modelValue),
rules: [ rules: [
(v) => { (v) => {
if (v === '') { if (v === '') {
@@ -59,16 +65,14 @@ export default {
}, },
computed: { computed: {
...mapStores(useSettingsStore), ...mapStores(useSettingsStore),
value: {
get: function () {
return this.modelValue;
},
set: function (value) {
this.$emit('update:modelValue', value);
}
},
extraClass() { extraClass() {
return this.class; let finalClass = this.class;
if (this.color && !this.readonly && !this.disabled) {
finalClass += ` text-${this.color}`;
}
return finalClass;
}, },
prependText() { prependText() {
if (!this.currency) { if (!this.currency) {
@@ -97,6 +101,22 @@ export default {
return texts.appendText; return texts.appendText;
} }
}, },
watch: {
'modelValue': function (newValue) {
const numericCurrentValue = stringCurrencyToNumeric(this.currentValue);
if (newValue !== numericCurrentValue) {
const newStringValue = numericCurrencyToString(newValue, false, true);
if (!(newStringValue === '0' && this.currentValue === '')) {
this.currentValue = newStringValue;
}
}
},
'currentValue': function (newValue) {
this.$emit('update:modelValue', stringCurrencyToNumeric(newValue));
}
},
methods: { methods: {
onKeyUpDown(e) { onKeyUpDown(e) {
if (e.target.value === '' || e.target.value === 0) { if (e.target.value === '' || e.target.value === 0) {
@@ -104,13 +124,15 @@ export default {
} }
let decimalLength = 0; let decimalLength = 0;
let decimalIndex = e.target.value.indexOf('.');
if (e.target.value.indexOf('.') > 0) { if (decimalIndex >= 0) {
decimalLength = e.target.value.length - e.target.value.indexOf('.') - 1; decimalLength = e.target.value.length - e.target.value.indexOf('.') - 1;
} }
if (decimalLength > 2) { if (decimalLength > 2) {
e.target.value = e.target.value.substring(0, e.target.value.length - 1); e.target.value = e.target.value.substring(0, Math.min(decimalIndex + 3, e.target.value.length - 1));
this.currentValue = e.target.value;
e.preventDefault(); e.preventDefault();
return; return;
} }
@@ -126,15 +148,15 @@ export default {
if (val < transactionConstants.minAmount) { if (val < transactionConstants.minAmount) {
e.target.value = transactionConstants.minAmount; e.target.value = transactionConstants.minAmount;
this.currentValue = e.target.value;
e.preventDefault(); e.preventDefault();
} else if (val > transactionConstants.maxAmount) { } else if (val > transactionConstants.maxAmount) {
e.target.value = transactionConstants.maxAmount; e.target.value = transactionConstants.maxAmount;
this.currentValue = e.target.value;
e.preventDefault(); e.preventDefault();
} else if (e.target.value.length > maxLength) { } else if (e.target.value.length > maxLength) {
e.target.value = e.target.value.substring(0, maxLength); e.target.value = e.target.value.substring(0, maxLength);
e.preventDefault(); this.currentValue = e.target.value;
} else if (e.target.value.charAt(0) === '0' && e.target.value.length > 1) {
e.target.value = e.target.value.substring(1);
e.preventDefault(); e.preventDefault();
} }
} catch (e) { } catch (e) {
+21 -10
View File
@@ -1,6 +1,6 @@
import { isNumber, appendThousandsSeparator } from './common.js'; import { isNumber, appendThousandsSeparator } from './common.js';
export function numericCurrencyToString(num, enableThousandsSeparator) { export function numericCurrencyToString(num, enableThousandsSeparator, trimTailZero) {
let str = num.toString(); let str = num.toString();
const negative = str.charAt(0) === '-'; const negative = str.charAt(0) === '-';
@@ -8,19 +8,30 @@ export function numericCurrencyToString(num, enableThousandsSeparator) {
str = str.substring(1); str = str.substring(1);
} }
if (str.length === 0) { let integer = '0';
str = '0.00'; let decimals = '00';
} else if (str.length === 1) {
str = '0.0' + str; if (str.length > 2) {
integer = str.substring(0, str.length - 2);
decimals = str.substring(str.length - 2);
} else if (str.length === 2) { } else if (str.length === 2) {
str = '0.' + str; decimals = str;
} else { } else if (str.length === 1) {
let integer = str.substring(0, str.length - 2); decimals = '0' + str;
let decimals = str.substring(str.length - 2); }
integer = appendThousandsSeparator(integer, enableThousandsSeparator); if (trimTailZero) {
if (decimals.charAt(0) === '0' && decimals.charAt(1) === '0') {
decimals = '';
} else if (decimals.charAt(0) !== '0' && decimals.charAt(1) === '0') {
decimals = decimals.charAt(0);
}
}
if (decimals !== '') {
str = `${integer}.${decimals}`; str = `${integer}.${decimals}`;
} else {
str = integer;
} }
if (negative) { if (negative) {
+3 -3
View File
@@ -736,7 +736,7 @@ export const useAccountsStore = defineStore('accounts', {
}); });
}); });
}, },
saveAccount({ account, subAccounts, isEdit, isFloatBalance }) { saveAccount({ account, subAccounts, isEdit }) {
const self = this; const self = this;
const submitSubAccounts = []; const submitSubAccounts = [];
@@ -751,7 +751,7 @@ export const useAccountsStore = defineStore('accounts', {
icon: subAccount.icon, icon: subAccount.icon,
color: subAccount.color, color: subAccount.color,
currency: subAccount.currency, currency: subAccount.currency,
balance: isFloatBalance ? subAccount.balance * 100 : subAccount.balance, balance: subAccount.balance,
comment: subAccount.comment comment: subAccount.comment
}; };
@@ -771,7 +771,7 @@ export const useAccountsStore = defineStore('accounts', {
icon: account.icon, icon: account.icon,
color: account.color, color: account.color,
currency: account.type === accountConstants.allAccountTypes.SingleAccount ? account.currency : currencyConstants.parentAccountCurrencyPlaceholder, currency: account.type === accountConstants.allAccountTypes.SingleAccount ? account.currency : currencyConstants.parentAccountCurrencyPlaceholder,
balance: account.type === accountConstants.allAccountTypes.SingleAccount ? (isFloatBalance ? account.balance * 100 : account.balance) : 0, balance: account.type === accountConstants.allAccountTypes.SingleAccount ? account.balance : 0,
comment: account.comment, comment: account.comment,
subAccounts: account.type === accountConstants.allAccountTypes.SingleAccount ? null : submitSubAccounts, subAccounts: account.type === accountConstants.allAccountTypes.SingleAccount ? null : submitSubAccounts,
}; };
+12
View File
@@ -223,6 +223,18 @@ input[type=number] {
height: 38px; height: 38px;
} }
.text-field-with-colored-label.v-text-field.text-primary .v-field-label.v-label {
color: rgba(var(--v-theme-primary), var(--v-medium-emphasis-opacity)) !important;
}
.text-field-with-colored-label.v-text-field.text-expense .v-field-label.v-label {
color: rgba(var(--v-theme-expense), var(--v-medium-emphasis-opacity)) !important;
}
.text-field-with-colored-label.v-text-field.text-income .v-field-label.v-label {
color: rgba(var(--v-theme-income), var(--v-medium-emphasis-opacity)) !important;
}
/** Replacing the default style of @vuepic/vue-datepicker **/ /** Replacing the default style of @vuepic/vue-datepicker **/
.dp__theme_light { .dp__theme_light {
--dp-primary-color: #c67e48; --dp-primary-color: #c67e48;
+11 -10
View File
@@ -109,7 +109,7 @@
<v-btn class="px-2 ml-2 mr-3" color="default" <v-btn class="px-2 ml-2 mr-3" color="default"
density="comfortable" variant="text" density="comfortable" variant="text"
:class="{ 'd-none': loading, 'hover-display': !loading }" :class="{ 'd-none': loading, 'hover-display': !loading }"
@click="setAsBaseline(exchangeRate.currencyCode, exchangeRate)"> @click="setAsBaseline(exchangeRate.currencyCode, getConvertedAmount(exchangeRate))">
{{ $t('Set As Baseline') }} {{ $t('Set As Baseline') }}
</v-btn> </v-btn>
<span>{{ getDisplayConvertedAmount(exchangeRate, isEnableThousandsSeparator) }}</span> <span>{{ getDisplayConvertedAmount(exchangeRate, isEnableThousandsSeparator) }}</span>
@@ -138,7 +138,9 @@ import { useSettingsStore } from '@/stores/setting.js';
import { useUserStore } from '@/stores/user.js'; import { useUserStore } from '@/stores/user.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js'; import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { isNumber } from '@/lib/common.js';
import { import {
stringCurrencyToNumeric,
getConvertedAmount, getConvertedAmount,
getDisplayExchangeRateAmount getDisplayExchangeRateAmount
} from '@/lib/currency.js'; } from '@/lib/currency.js';
@@ -156,7 +158,7 @@ export default {
return { return {
activeTab: 'exchangeRatesPage', activeTab: 'exchangeRatesPage',
baseCurrency: userStore.currentUserDefaultCurrency, baseCurrency: userStore.currentUserDefaultCurrency,
baseAmount: '1', baseAmount: 100,
loading: true, loading: true,
alwaysShowNav: mdAndUp.value, alwaysShowNav: mdAndUp.value,
showNav: mdAndUp.value, showNav: mdAndUp.value,
@@ -246,23 +248,22 @@ export default {
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency]; const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
try { try {
return getConvertedAmount(parseFloat(this.baseAmount), fromExchangeRate, toExchangeRate); return getConvertedAmount(this.baseAmount / 100, fromExchangeRate, toExchangeRate);
} catch (e) { } catch (e) {
return 0; return 0;
} }
}, },
getDisplayConvertedAmount(toExchangeRate, isEnableThousandsSeparator) { getDisplayConvertedAmount(toExchangeRate, isEnableThousandsSeparator) {
if (this.baseAmount === '') {
return '';
}
const rateStr = this.getConvertedAmount(toExchangeRate).toString(); const rateStr = this.getConvertedAmount(toExchangeRate).toString();
return getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator); return getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator);
}, },
setAsBaseline(currency, toExchangeRate) { setAsBaseline(currency, amount) {
const rateStr = this.getConvertedAmount(toExchangeRate).toString(); if (!isNumber(amount)) {
amount = '';
}
this.baseCurrency = currency; this.baseCurrency = currency;
this.baseAmount = getDisplayExchangeRateAmount(rateStr, false) this.baseAmount = stringCurrencyToNumeric(amount.toString());
} }
} }
} }
@@ -124,8 +124,8 @@
/> />
</v-col> </v-col>
<v-col cols="12" md="12" v-if="account.type === allAccountTypes.SingleAccount || currentAccountIndex >= 0"> <v-col cols="12" md="12" v-if="account.type === allAccountTypes.SingleAccount || currentAccountIndex >= 0">
<amount-input persistent-placeholder <amount-input :disabled="loading || submitting || !!editAccountId"
:disabled="loading || submitting || !!editAccountId" :persistent-placeholder="true"
:currency="selectedAccount.currency" :currency="selectedAccount.currency"
:label="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub Account Balance')" :label="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub Account Balance')"
:placeholder="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub Account Balance')" :placeholder="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub Account Balance')"
@@ -436,14 +436,12 @@ export default {
}, },
setAccount(account) { setAccount(account) {
setAccountModelByAnotherAccount(this.account, account); setAccountModelByAnotherAccount(this.account, account);
this.account.balance = this.account.balance / 100;
this.subAccounts = []; this.subAccounts = [];
if (account.subAccounts && account.subAccounts.length > 0) { if (account.subAccounts && account.subAccounts.length > 0) {
for (let i = 0; i < account.subAccounts.length; i++) { for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account); const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
setAccountModelByAnotherAccount(subAccount, account.subAccounts[i]); setAccountModelByAnotherAccount(subAccount, account.subAccounts[i]);
subAccount.balance = subAccount.balance / 100;
this.subAccounts.push(subAccount); this.subAccounts.push(subAccount);
} }
@@ -54,18 +54,20 @@
<v-form class="mt-2"> <v-form class="mt-2">
<v-row> <v-row>
<v-col cols="12" :md="transaction.type === allTransactionTypes.Transfer ? 6 : 12"> <v-col cols="12" :md="transaction.type === allTransactionTypes.Transfer ? 6 : 12">
<amount-input persistent-placeholder <amount-input :color="sourceAmountColor"
:readonly="mode === 'view'" :readonly="mode === 'view'"
:disabled="loading || submitting" :disabled="loading || submitting"
:persistent-placeholder="true"
:hide="transaction.hideAmount" :hide="transaction.hideAmount"
:label="$t(sourceAmountName)" :label="$t(sourceAmountName)"
:placeholder="$t(sourceAmountName)" :placeholder="$t(sourceAmountName)"
v-model="transaction.sourceAmount"/> v-model="transaction.sourceAmount"/>
</v-col> </v-col>
<v-col cols="12" :md="6" v-if="transaction.type === allTransactionTypes.Transfer"> <v-col cols="12" :md="6" v-if="transaction.type === allTransactionTypes.Transfer">
<amount-input persistent-placeholder <amount-input color="primary"
:readonly="mode === 'view'" :readonly="mode === 'view'"
:disabled="loading || submitting" :disabled="loading || submitting"
:persistent-placeholder="true"
:hide="transaction.hideAmount" :hide="transaction.hideAmount"
:label="$t('Transfer In Amount')" :label="$t('Transfer In Amount')"
:placeholder="$t('Transfer In Amount')" :placeholder="$t('Transfer In Amount')"
@@ -464,6 +466,15 @@ export default {
transactionTimezoneTimeDifference() { transactionTimezoneTimeDifference() {
return this.$locale.getTimezoneDifferenceDisplayText(this.transaction.utcOffset); return this.$locale.getTimezoneDifferenceDisplayText(this.transaction.utcOffset);
}, },
sourceAmountColor() {
if (this.transaction.type === this.allTransactionTypes.Expense) {
return 'expense';
} else if (this.transaction.type === this.allTransactionTypes.Income) {
return 'income';
} else if (this.transaction.type === this.allTransactionTypes.Transfer) {
return 'primary';
}
},
geoLocationStatusInfo() { geoLocationStatusInfo() {
if (this.geoLocationStatus === 'success') { if (this.geoLocationStatus === 'success') {
return ''; return '';
@@ -538,8 +549,6 @@ export default {
if (options && options.id) { if (options && options.id) {
if (options.currentTransaction) { if (options.currentTransaction) {
self.setTransaction(options.currentTransaction, options, true); self.setTransaction(options.currentTransaction, options, true);
self.transaction.sourceAmount = self.transaction.sourceAmount / 100;
self.transaction.destinationAmount = self.transaction.destinationAmount / 100;
} }
self.mode = 'view'; self.mode = 'view';
@@ -574,8 +583,6 @@ export default {
if (options.id && responses[3]) { if (options.id && responses[3]) {
const transaction = responses[3]; const transaction = responses[3];
self.setTransaction(transaction, options, true); self.setTransaction(transaction, options, true);
self.transaction.sourceAmount = self.transaction.sourceAmount / 100;
self.transaction.destinationAmount = self.transaction.destinationAmount / 100;
} else { } else {
self.setTransaction(null, options, true); self.setTransaction(null, options, true);
} }