code refactor

This commit is contained in:
MaysWind
2023-08-13 22:19:59 +08:00
parent 33cbdfbd13
commit 62ded1290c
4 changed files with 169 additions and 246 deletions
+31
View File
@@ -1,5 +1,18 @@
import accountConstants from '@/consts/account.js';
export function setAccountModelByAnotherAccount(account, account2) {
account.id = account2.id;
account.category = account2.category;
account.type = account2.type;
account.name = account2.name;
account.icon = account2.icon;
account.color = account2.color;
account.currency = account2.currency;
account.balance = account2.balance;
account.comment = account2.comment;
account.visible = !account2.hidden;
}
export function getAccountCategoryInfo(categoryId) {
for (let i = 0; i < accountConstants.allCategories.length; i++) {
if (accountConstants.allCategories[i].id === categoryId) {
@@ -303,3 +316,21 @@ export function isAccountOrSubAccountsHasButNotAllChecked(account, filterAccount
return checkedCount > 0 && checkedCount < account.subAccounts.length;
}
export function setAccountSuitableIcon(account, oldCategory, newCategory) {
for (let i = 0; i < accountConstants.allCategories.length; i++) {
if (accountConstants.allCategories[i].id === oldCategory) {
if (account.icon !== accountConstants.allCategories[i].defaultAccountIconId) {
return;
} else {
break;
}
}
}
for (let i = 0; i < accountConstants.allCategories.length; i++) {
if (accountConstants.allCategories[i].id === newCategory) {
account.icon = accountConstants.allCategories[i].defaultAccountIconId;
}
}
}
+84 -8
View File
@@ -4,6 +4,9 @@ import { useUserStore } from './user.js';
import { useExchangeRatesStore } from './exchangeRates.js';
import accountConstants from '@/consts/account.js';
import currencyConstants from '@/consts/currency.js';
import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js';
import services from '@/lib/services.js';
import logger from '@/lib/logger.js';
import { isNumber, isEquals } from '@/lib/common.js';
@@ -249,6 +252,36 @@ export const useAccountsStore = defineStore('accounts', {
}
},
actions: {
generateNewAccountModel() {
const userStore = useUserStore();
return {
category: 1,
type: accountConstants.allAccountTypes.SingleAccount,
name: '',
icon: iconConstants.defaultAccountIconId,
color: colorConstants.defaultAccountColor,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true
};
},
generateNewSubAccountModel(parentAccount) {
const userStore = useUserStore();
return {
category: null,
type: null,
name: '',
icon: parentAccount.icon,
color: parentAccount.color,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true
};
},
updateAccountListInvalidState(invalidState) {
this.accountListStateInvalid = invalidState;
},
@@ -703,24 +736,67 @@ export const useAccountsStore = defineStore('accounts', {
});
});
},
saveAccount({ account }) {
saveAccount({ account, subAccounts, isEdit, isFloatBalance }) {
const self = this;
const oldAccount = account.id ? self.allAccountsMap[account.id] : null;
const submitSubAccounts = [];
if (account.type === accountConstants.allAccountTypes.MultiSubAccounts) {
for (let i = 0; i < subAccounts.length; i++) {
const subAccount = subAccounts[i];
const submitAccount = {
category: account.category,
type: accountConstants.allAccountTypes.SingleAccount,
name: subAccount.name,
icon: subAccount.icon,
color: subAccount.color,
currency: subAccount.currency,
balance: isFloatBalance ? subAccount.balance * 100 : subAccount.balance,
comment: subAccount.comment
};
if (isEdit) {
submitAccount.id = subAccount.id;
submitAccount.hidden = !subAccount.visible;
}
submitSubAccounts.push(submitAccount);
}
}
const submitAccount = {
category: account.category,
type: account.type,
name: account.name,
icon: account.icon,
color: account.color,
currency: account.type === accountConstants.allAccountTypes.SingleAccount ? account.currency : currencyConstants.parentAccountCurrencyPlaceholder,
balance: account.type === accountConstants.allAccountTypes.SingleAccount ? (isFloatBalance ? account.balance * 100 : account.balance) : 0,
comment: account.comment,
subAccounts: account.type === accountConstants.allAccountTypes.SingleAccount ? null : submitSubAccounts,
};
if (isEdit) {
submitAccount.id = account.id;
submitAccount.hidden = !account.visible;
}
const oldAccount = submitAccount.id ? self.allAccountsMap[submitAccount.id] : null;
return new Promise((resolve, reject) => {
let promise = null;
if (!account.id) {
promise = services.addAccount(account);
if (!submitAccount.id) {
promise = services.addAccount(submitAccount);
} else {
promise = services.modifyAccount(account);
promise = services.modifyAccount(submitAccount);
}
promise.then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
if (!account.id) {
if (!submitAccount.id) {
reject({ message: 'Unable to add account' });
} else {
reject({ message: 'Unable to save account' });
@@ -728,7 +804,7 @@ export const useAccountsStore = defineStore('accounts', {
return;
}
if (!account.id) {
if (!submitAccount.id) {
addAccountToAccountList(self, data.result);
} else {
if (oldAccount && oldAccount.category === data.result.category) {
@@ -745,7 +821,7 @@ export const useAccountsStore = defineStore('accounts', {
if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data });
} else if (!error.processed) {
if (!account.id) {
if (!submitAccount.id) {
reject({ message: 'Unable to add account' });
} else {
reject({ message: 'Unable to save account' });
@@ -171,7 +171,6 @@
<script>
import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js';
import { useUserStore } from '@/stores/user.js';
import { useAccountsStore } from '@/stores/account.js';
import accountConstants from '@/consts/account.js';
@@ -179,6 +178,10 @@ import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js';
import currencyConstants from '@/consts/currency.js';
import { isNumber } from '@/lib/common.js';
import {
setAccountModelByAnotherAccount,
setAccountSuitableIcon
} from '@/lib/account.js';
import {
mdiDotsVertical,
@@ -195,24 +198,15 @@ export default {
'open'
],
data() {
const userStore = useUserStore();
const accountsStore = useAccountsStore();
const newAccount = accountsStore.generateNewAccountModel();
return {
showState: false,
activeTab: 'account',
editAccountId: null,
loading: false,
account: {
category: 1,
type: accountConstants.allAccountTypes.SingleAccount,
name: '',
icon: iconConstants.defaultAccountIconId,
color: colorConstants.defaultAccountColor,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true
},
account: newAccount,
subAccounts: [],
currentAccountIndex: -1,
submitting: false,
@@ -226,7 +220,7 @@ export default {
};
},
computed: {
...mapStores(useSettingsStore, useUserStore, useAccountsStore),
...mapStores(useSettingsStore, useAccountsStore),
title() {
if (!this.editAccountId) {
return 'Add Account';
@@ -284,16 +278,8 @@ export default {
self.loading = true;
self.submitting = false;
self.account.id = null;
self.account.category = 1;
self.account.type = accountConstants.allAccountTypes.SingleAccount;
self.account.name = '';
self.account.icon = iconConstants.defaultAccountIconId;
self.account.color = colorConstants.defaultAccountColor;
self.account.currency = self.userStore.currentUserDefaultCurrency;
self.account.balance = 0;
self.account.comment = '';
self.account.visible = true;
const newAccount = self.accountsStore.generateNewAccountModel();
setAccountModelByAnotherAccount(self.account, newAccount);
self.subAccounts = [];
self.currentAccountIndex = -1;
@@ -334,23 +320,12 @@ export default {
});
},
addSubAccount() {
const self = this;
if (self.account.type !== self.allAccountTypes.MultiSubAccounts) {
if (this.account.type !== this.allAccountTypes.MultiSubAccounts) {
return;
}
self.subAccounts.push({
category: null,
type: null,
name: '',
icon: self.account.icon,
color: self.account.color,
currency: self.userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true
});
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
this.subAccounts.push(subAccount);
},
removeSubAccount(subAccount) {
const self = this;
@@ -389,50 +364,11 @@ export default {
self.submitting = true;
const subAccounts = [];
if (self.account.type === self.allAccountTypes.MultiSubAccounts) {
for (let i = 0; i < self.subAccounts.length; i++) {
const subAccount = self.subAccounts[i];
const submitAccount = {
category: self.account.category,
type: self.allAccountTypes.SingleAccount,
name: subAccount.name,
icon: subAccount.icon,
color: subAccount.color,
currency: subAccount.currency,
balance: subAccount.balance * 100,
comment: subAccount.comment
};
if (self.editAccountId) {
submitAccount.id = subAccount.id;
submitAccount.hidden = !subAccount.visible;
}
subAccounts.push(submitAccount);
}
}
const submitAccount = {
category: self.account.category,
type: self.account.type,
name: self.account.name,
icon: self.account.icon,
color: self.account.color,
currency: self.account.type === self.allAccountTypes.SingleAccount ? self.account.currency : currencyConstants.parentAccountCurrencyPlaceholder,
balance: self.account.type === self.allAccountTypes.SingleAccount ? self.account.balance * 100 : 0,
comment: self.account.comment,
subAccounts: self.account.type === self.allAccountTypes.SingleAccount ? null : subAccounts,
};
if (self.editAccountId) {
submitAccount.id = self.account.id;
submitAccount.hidden = !self.account.visible;
}
self.accountsStore.saveAccount({
account: submitAccount
account: self.account,
subAccounts: self.subAccounts,
isEdit: !!self.editAccountId,
isFloatBalance: true
}).then(() => {
self.submitting = false;
@@ -465,21 +401,7 @@ export default {
this.showState = false;
},
chooseSuitableIcon(oldCategory, newCategory) {
for (let i = 0; i < this.allAccountCategories.length; i++) {
if (this.allAccountCategories[i].id === oldCategory) {
if (this.account.icon !== this.allAccountCategories[i].defaultAccountIconId) {
return;
} else {
break;
}
}
}
for (let i = 0; i < this.allAccountCategories.length; i++) {
if (this.allAccountCategories[i].id === newCategory) {
this.account.icon = this.allAccountCategories[i].defaultAccountIconId;
}
}
setAccountSuitableIcon(this.account, oldCategory, newCategory);
},
isInputEmpty() {
const isAccountEmpty = !!this.getInputEmptyProblemMessage(this.account, false);
@@ -514,35 +436,17 @@ export default {
}
},
setAccount(account) {
this.account.id = account.id;
this.account.category = account.category;
this.account.type = account.type;
this.account.name = account.name;
this.account.icon = account.icon;
this.account.color = account.color;
this.account.currency = account.currency;
this.account.balance = account.balance / 100;
this.account.comment = account.comment;
this.account.visible = !account.hidden;
setAccountModelByAnotherAccount(this.account, account);
this.account.balance = this.account.balance / 100;
this.subAccounts = [];
if (account.subAccounts && account.subAccounts.length > 0) {
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
setAccountModelByAnotherAccount(subAccount, account.subAccounts[i]);
subAccount.balance = subAccount.balance / 100;
this.subAccounts.push({
id: subAccount.id,
category: subAccount.category,
type: subAccount.type,
name: subAccount.name,
icon: subAccount.icon,
color: subAccount.color,
currency: subAccount.currency,
balance: subAccount.balance / 100,
comment: subAccount.comment,
visible: !subAccount.hidden
});
this.subAccounts.push(subAccount);
}
}
}
+30 -118
View File
@@ -422,7 +422,6 @@
<script>
import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.js';
import { useUserStore } from '@/stores/user.js';
import { useAccountsStore } from '@/stores/account.js';
import accountConstants from '@/consts/account.js';
@@ -431,6 +430,10 @@ import colorConstants from '@/consts/color.js';
import currencyConstants from '@/consts/currency.js';
import transactionConstants from '@/consts/transaction.js';
import { getNameByKeyValue } from '@/lib/common.js';
import {
setAccountModelByAnotherAccount,
setAccountSuitableIcon
} from '@/lib/account.js';
export default {
props: [
@@ -438,26 +441,17 @@ export default {
'f7router'
],
data() {
const userStore = useUserStore();
const accountsStore = useAccountsStore();
const newAccount = accountsStore.generateNewAccountModel();
newAccount.showIconSelectionSheet = false;
newAccount.showColorSelectionSheet = false;
newAccount.showBalanceSheet = false;
return {
editAccountId: null,
loading: false,
loadingError: null,
account: {
category: 1,
type: accountConstants.allAccountTypes.SingleAccount,
name: '',
icon: iconConstants.defaultAccountIconId,
color: colorConstants.defaultAccountColor,
currency: userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true,
showIconSelectionSheet: false,
showColorSelectionSheet: false,
showBalanceSheet: false
},
account: newAccount,
subAccounts: [],
subAccountToDelete: null,
submitting: false,
@@ -468,7 +462,7 @@ export default {
};
},
computed: {
...mapStores(useSettingsStore, useUserStore, useAccountsStore),
...mapStores(useSettingsStore, useAccountsStore),
title() {
if (!this.editAccountId) {
return 'Add Account';
@@ -530,37 +524,18 @@ export default {
self.accountsStore.getAccount({
accountId: self.editAccountId
}).then(account => {
self.account.id = account.id;
self.account.category = account.category;
self.account.type = account.type;
self.account.name = account.name;
self.account.icon = account.icon;
self.account.color = account.color;
self.account.currency = account.currency;
self.account.balance = account.balance;
self.account.comment = account.comment;
self.account.visible = !account.hidden;
setAccountModelByAnotherAccount(self.account, account);
self.subAccounts = [];
if (account.subAccounts && account.subAccounts.length > 0) {
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
const subAccount = self.accountsStore.generateNewSubAccountModel(self.account);
setAccountModelByAnotherAccount(subAccount, account.subAccounts[i]);
subAccount.showIconSelectionSheet = false;
subAccount.showColorSelectionSheet = false;
subAccount.showBalanceSheet = false;
self.subAccounts.push({
id: subAccount.id,
category: subAccount.category,
type: subAccount.type,
name: subAccount.name,
icon: subAccount.icon,
color: subAccount.color,
currency: subAccount.currency,
balance: subAccount.balance,
comment: subAccount.comment,
visible: !subAccount.hidden,
showIconSelectionSheet: false,
showColorSelectionSheet: false,
showBalanceSheet: false
});
self.subAccounts.push(subAccount);
}
}
@@ -582,26 +557,16 @@ export default {
this.$routeBackOnError(this.f7router, 'loadingError');
},
addSubAccount() {
const self = this;
if (self.account.type !== self.allAccountTypes.MultiSubAccounts) {
if (this.account.type !== this.allAccountTypes.MultiSubAccounts) {
return;
}
self.subAccounts.push({
category: null,
type: null,
name: '',
icon: self.account.icon,
color: self.account.color,
currency: self.userStore.currentUserDefaultCurrency,
balance: 0,
comment: '',
visible: true,
showIconSelectionSheet: false,
showColorSelectionSheet: false,
showBalanceSheet: false
});
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
subAccount.showIconSelectionSheet = false;
subAccount.showColorSelectionSheet = false;
subAccount.showBalanceSheet = false;
this.subAccounts.push(subAccount);
},
removeSubAccount(subAccount, confirm) {
if (!subAccount) {
@@ -648,50 +613,11 @@ export default {
self.submitting = true;
self.$showLoading(() => self.submitting);
const subAccounts = [];
if (self.account.type === self.allAccountTypes.MultiSubAccounts) {
for (let i = 0; i < self.subAccounts.length; i++) {
const subAccount = self.subAccounts[i];
const submitAccount = {
category: self.account.category,
type: self.allAccountTypes.SingleAccount,
name: subAccount.name,
icon: subAccount.icon,
color: subAccount.color,
currency: subAccount.currency,
balance: subAccount.balance,
comment: subAccount.comment
};
if (self.editAccountId) {
submitAccount.id = subAccount.id;
submitAccount.hidden = !subAccount.visible;
}
subAccounts.push(submitAccount);
}
}
const submitAccount = {
category: self.account.category,
type: self.account.type,
name: self.account.name,
icon: self.account.icon,
color: self.account.color,
currency: self.account.type === self.allAccountTypes.SingleAccount ? self.account.currency : currencyConstants.parentAccountCurrencyPlaceholder,
balance: self.account.type === self.allAccountTypes.SingleAccount ? self.account.balance : 0,
comment: self.account.comment,
subAccounts: self.account.type === self.allAccountTypes.SingleAccount ? null : subAccounts,
};
if (self.editAccountId) {
submitAccount.id = self.account.id;
submitAccount.hidden = !self.account.visible;
}
self.accountsStore.saveAccount({
account: submitAccount
account: self.account,
subAccounts: self.subAccounts,
isEdit: !!self.editAccountId,
isFloatBalance: false
}).then(() => {
self.submitting = false;
self.$hideLoading();
@@ -733,21 +659,7 @@ export default {
});
},
chooseSuitableIcon(oldCategory, newCategory) {
for (let i = 0; i < this.allAccountCategories.length; i++) {
if (this.allAccountCategories[i].id === oldCategory) {
if (this.account.icon !== this.allAccountCategories[i].defaultAccountIconId) {
return;
} else {
break;
}
}
}
for (let i = 0; i < this.allAccountCategories.length; i++) {
if (this.allAccountCategories[i].id === newCategory) {
this.account.icon = this.allAccountCategories[i].defaultAccountIconId;
}
}
setAccountSuitableIcon(this.account, oldCategory, newCategory);
},
isInputEmpty() {
const isAccountEmpty = !!this.getInputEmptyProblemMessage(this.account, false);