code refactor

This commit is contained in:
MaysWind
2021-01-05 22:55:34 +08:00
parent fbfba764d4
commit abae1c85e7
16 changed files with 172 additions and 122 deletions
+12 -21
View File
@@ -9,7 +9,7 @@
</f7-navbar>
<f7-card>
<f7-card-content class="no-safe-areas" :padding="false" v-if="exchangeRatesData.exchangeRates && exchangeRatesData.exchangeRates.length">
<f7-card-content class="no-safe-areas" :padding="false" v-if="exchangeRatesData && exchangeRatesData.exchangeRates && exchangeRatesData.exchangeRates.length">
<f7-list>
<f7-list-item
:title="$t('Base Currency')"
@@ -25,12 +25,12 @@
</f7-card>
<f7-card>
<f7-card-content class="no-safe-areas" :padding="false" v-if="!exchangeRatesData.exchangeRates || !exchangeRatesData.exchangeRates.length">
<f7-card-content class="no-safe-areas" :padding="false" v-if="!exchangeRatesData || !exchangeRatesData.exchangeRates || !exchangeRatesData.exchangeRates.length">
<f7-list>
<f7-list-item :title="$t('No exchange rates data')"></f7-list-item>
</f7-list>
</f7-card-content>
<f7-card-content class="no-safe-areas" :padding="false" v-if="exchangeRatesData.exchangeRates && exchangeRatesData.exchangeRates.length">
<f7-card-content class="no-safe-areas" :padding="false" v-if="exchangeRatesData && exchangeRatesData.exchangeRates && exchangeRatesData.exchangeRates.length">
<f7-list>
<f7-list-item v-for="exchangeRate in availableExchangeRates" :key="exchangeRate.currencyCode"
:title="exchangeRate.currencyDisplayName"
@@ -56,11 +56,13 @@ export default {
return {
baseCurrency: self.$store.getters.currentUserDefaultCurrency || self.$t('default.currency'),
exchangeRatesData: self.$exchangeRates.getExchangeRates(),
updating: false
};
},
computed: {
exchangeRatesData() {
return this.$store.state.latestExchangeRates.data;
},
availableExchangeRates() {
if (!this.exchangeRatesData || !this.exchangeRatesData.exchangeRates) {
return [];
@@ -117,7 +119,10 @@ export default {
self.$showLoading();
}
self.$services.getLatestExchangeRates().then(response => {
self.$store.dispatch('getLatestExchangeRates', {
silent: false,
force: true
}).then(() => {
if (done) {
done();
}
@@ -125,20 +130,8 @@ export default {
self.updating = false;
self.$hideLoading();
const data = response.data;
if (!data || !data.success || !data.result) {
self.$toast('Unable to get exchange rates data');
return;
}
self.exchangeRatesData = data.result;
self.$exchangeRates.setExchangeRates(data.result);
self.$toast('Exchange rates data has been updated');
}).catch(error => {
self.$logger.error('failed to get latest exchange rates data', error);
if (done) {
done();
}
@@ -146,10 +139,8 @@ export default {
self.updating = false;
self.$hideLoading();
if (error.response && error.response.data && error.response.data.errorMessage) {
self.$toast({ error: error.response.data });
} else if (!error.processed) {
self.$toast('Unable to get exchange rates data');
if (!error.processed) {
self.$toast(error.message || error);
}
});
}
+2 -2
View File
@@ -188,7 +188,7 @@ export default {
}
if (self.$settings.isAutoUpdateExchangeRatesData()) {
self.$services.autoRefreshLatestExchangeRates();
self.$store.dispatch('getLatestExchangeRates', { silent: true, force: false });
}
router.refreshPage();
@@ -239,7 +239,7 @@ export default {
self.$hideLoading();
if (self.$settings.isAutoUpdateExchangeRatesData()) {
self.$services.autoRefreshLatestExchangeRates();
self.$store.dispatch('getLatestExchangeRates', { silent: true, force: false });
}
self.show2faSheet = false;
+5 -9
View File
@@ -86,8 +86,7 @@ export default {
const self = this;
return {
isEnableApplicationLock: this.$settings.isEnableApplicationLock(),
exchangeRatesLastUpdateDate: self.getExchangeRatesLastUpdateDate(),
isEnableApplicationLock: self.$settings.isEnableApplicationLock(),
logouting: false
};
},
@@ -104,7 +103,6 @@ export default {
},
set: function (value) {
this.$locale.setLanguage(value);
this.exchangeRatesLastUpdateDate = this.getExchangeRatesLastUpdateDate();
}
},
currentNickName() {
@@ -113,6 +111,10 @@ export default {
isDataExportingEnabled() {
return this.$settings.isDataExportingEnabled();
},
exchangeRatesLastUpdateDate() {
const exchangeRatesLastUpdateDate = this.$store.getters.exchangeRatesLastUpdateDate;
return exchangeRatesLastUpdateDate ? this.$utilities.formatDate(exchangeRatesLastUpdateDate, this.$t('format.date.long')) : '';
},
isAutoUpdateExchangeRatesData: {
get: function () {
return this.$settings.isAutoUpdateExchangeRatesData();
@@ -171,11 +173,6 @@ export default {
methods: {
onPageAfterIn() {
this.isEnableApplicationLock = this.$settings.isEnableApplicationLock();
this.exchangeRatesLastUpdateDate = this.getExchangeRatesLastUpdateDate();
},
getExchangeRatesLastUpdateDate() {
const exchangeRates = this.$exchangeRates.getExchangeRates();
return exchangeRates && exchangeRates.date ? this.$moment(exchangeRates.date).format(this.$t('format.date.long')) : '';
},
logout() {
const self = this;
@@ -189,7 +186,6 @@ export default {
self.logouting = false;
self.$hideLoading();
self.$exchangeRates.clearExchangeRates();
self.$settings.clearSettings();
self.$locale.init();
+1 -1
View File
@@ -160,7 +160,7 @@ export default {
if (self.$user.isUserLogined()) {
if (self.$settings.isAutoUpdateExchangeRatesData()) {
self.$services.autoRefreshLatestExchangeRates();
self.$store.dispatch('getLatestExchangeRates', { silent: true, force: false });
}
}
+3 -3
View File
@@ -60,7 +60,7 @@ export default {
self.$store.dispatch('refreshTokenAndRevokeOldToken');
if (self.$settings.isAutoUpdateExchangeRatesData()) {
self.$services.autoRefreshLatestExchangeRates();
self.$store.dispatch('getLatestExchangeRates', { silent: true, force: false });
}
router.refreshPage();
@@ -104,7 +104,7 @@ export default {
this.$store.dispatch('refreshTokenAndRevokeOldToken');
if (this.$settings.isAutoUpdateExchangeRatesData()) {
this.$services.autoRefreshLatestExchangeRates();
this.$store.dispatch('getLatestExchangeRates', { silent: true, force: false });
}
router.refreshPage();
@@ -121,7 +121,7 @@ export default {
self.$user.clearTokenAndUserInfo(true);
self.$user.clearWebAuthnConfig();
self.$store.dispatch('clearUserInfoState');
self.$exchangeRates.clearExchangeRates();
self.$store.dispatch('resetState');
self.$settings.clearSettings();
self.$locale.init();
+4 -4
View File
@@ -245,7 +245,7 @@ export default {
if (accountsBalance[i].currency === this.defaultCurrency) {
netAssets += accountsBalance[i].balance;
} else {
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
hasUnCalculatedAmount = true;
@@ -275,7 +275,7 @@ export default {
if (accountsBalance[i].currency === this.defaultCurrency) {
totalAssets += accountsBalance[i].balance;
} else {
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
hasUnCalculatedAmount = true;
@@ -305,7 +305,7 @@ export default {
if (accountsBalance[i].currency === this.defaultCurrency) {
totalLiabilities -= accountsBalance[i].balance;
} else {
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
hasUnCalculatedAmount = true;
@@ -431,7 +431,7 @@ export default {
totalBalance += accountsBalance[i].balance;
}
} else {
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
hasUnCalculatedAmount = true;
+3 -3
View File
@@ -369,8 +369,8 @@ export default {
}
if (sourceAccount && destinationAccount && sourceAccount.currency !== destinationAccount.currency) {
const exchangedOldValue = this.$exchangeRates.getOtherCurrencyAmount(oldValue, sourceAccount.currency, destinationAccount.currency);
const exchangedNewValue = this.$exchangeRates.getOtherCurrencyAmount(newValue, sourceAccount.currency, destinationAccount.currency);
const exchangedOldValue = this.$store.getters.getExchangedAmount(oldValue, sourceAccount.currency, destinationAccount.currency);
const exchangedNewValue = this.$store.getters.getExchangedAmount(newValue, sourceAccount.currency, destinationAccount.currency);
if (this.$utilities.isNumber(exchangedOldValue)) {
oldValue = Math.floor(exchangedOldValue);
@@ -704,7 +704,7 @@ export default {
totalBalance += accountsBalance[i].balance;
}
} else {
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
hasUnCalculatedAmount = true;
+1 -1
View File
@@ -888,7 +888,7 @@ export default {
let amount = transaction.sourceAmount;
if (transaction.sourceAccount.currency !== this.defaultCurrency) {
const balance = this.$exchangeRates.getOtherCurrencyAmount(amount, transaction.sourceAccount.currency, this.defaultCurrency);
const balance = this.$store.getters.getExchangedAmount(amount, transaction.sourceAccount.currency, this.defaultCurrency);
if (!this.$utilities.isNumber(balance)) {
if (transaction.type === this.$constants.transaction.allTransactionTypes.Expense) {