code refactor

This commit is contained in:
MaysWind
2021-01-07 22:28:28 +08:00
parent 71d55adf33
commit 1b2a37e6d1
5 changed files with 262 additions and 156 deletions
+45
View File
@@ -1,3 +1,4 @@
import accountConstants from '../consts/account.js';
import services from '../lib/services.js';
import logger from '../lib/logger.js';
@@ -271,6 +272,48 @@ function deleteAccount(context, { account, beforeResolve }) {
});
}
function allPlainAccounts(state) {
const allAccounts = [];
for (let i = 0; i < state.allAccounts.length; i++) {
const account = state.allAccounts[i];
if (account.type === accountConstants.allAccountTypes.SingleAccount) {
allAccounts.push(account);
} else if (account.type === accountConstants.allAccountTypes.MultiSubAccounts) {
for (let j = 0; j < account.subAccounts.length; j++) {
const subAccount = account.subAccounts[j];
allAccounts.push(subAccount);
}
}
}
return allAccounts;
}
function allVisiblePlainAccounts(state) {
const allVisibleAccounts = [];
for (let i = 0; i < state.allAccounts.length; i++) {
const account = state.allAccounts[i];
if (account.hidden) {
continue;
}
if (account.type === accountConstants.allAccountTypes.SingleAccount) {
allVisibleAccounts.push(account);
} else if (account.type === accountConstants.allAccountTypes.MultiSubAccounts) {
for (let j = 0; j < account.subAccounts.length; j++) {
const subAccount = account.subAccounts[j];
allVisibleAccounts.push(subAccount);
}
}
}
return allVisibleAccounts;
}
function allAvailableAccountsCount(state) {
let allAccountCount = 0;
@@ -313,6 +356,8 @@ export default {
updateAccountDisplayOrders,
hideAccount,
deleteAccount,
allPlainAccounts,
allVisiblePlainAccounts,
allAvailableAccountsCount,
allVisibleAccountsCount,
}
+24 -2
View File
@@ -17,6 +17,9 @@ import {
REMOVE_ACCOUNT_FROM_ACCOUNT_LIST,
UPDATE_ACCOUNT_LIST_INVALID_STATE,
LOAD_TRANSACTION_LIST,
UPDATE_TRANSACTION_LIST_INVALID_STATE,
LOAD_TRANSACTION_CATEGORY_LIST,
ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST,
SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST,
@@ -39,6 +42,7 @@ import twoFactorAuth from './twoFactorAuth.js';
import token from './token.js';
import exchangeRates from './exchangeRates.js';
import account from './account.js';
import transaction from './transaction.js';
import transactionCategory from './transactionCategory.js';
import transactionTag from './transactionTag.js';
@@ -51,35 +55,44 @@ const stores = {
allAccountsMap: {},
allCategorizedAccounts: {},
accountListStateInvalid: true,
transactions: [],
transactionListStateInvalid: true,
allTransactionCategories: {},
allTransactionCategoriesMap: {},
transactionCategoryListStateInvalid: true,
allTransactionTags: [],
allTransactionTagsMap: {},
transactionTagListStateInvalid: true,
transactions: [],
},
getters: {
currentUserNickname: user.currentUserNickname,
currentUserDefaultCurrency: user.currentUserDefaultCurrency,
exchangeRatesLastUpdateDate: exchangeRates.exchangeRatesLastUpdateDate,
getExchangedAmount: exchangeRates.getExchangedAmount,
allPlainAccounts: account.allPlainAccounts,
allVisiblePlainAccounts: account.allVisiblePlainAccounts,
allAvailableAccountsCount: account.allAvailableAccountsCount,
allVisibleAccountsCount: account.allVisibleAccountsCount,
},
mutations: {
[RESET_STATE] (state) {
state.latestExchangeRates = {};
state.allAccounts = [];
state.allAccountsMap = {};
state.allCategorizedAccounts = {};
state.accountListStateInvalid = true;
state.transactions = [];
state.transactionListStateInvalid = true;
state.allTransactionCategories = {};
state.allTransactionCategoriesMap = {};
state.transactionCategoryListStateInvalid = true;
state.allTransactionTags = [];
state.allTransactionTagsMap = {};
state.transactionTagListStateInvalid = true;
state.transactions = [];
exchangeRates.clearExchangeRatesFromLocalStorage();
},
@@ -208,6 +221,12 @@ const stores = {
[UPDATE_ACCOUNT_LIST_INVALID_STATE] (state, invalidState) {
state.accountListStateInvalid = invalidState;
},
[LOAD_TRANSACTION_LIST] (state, transactions) {
state.transactions = transactions;
},
[UPDATE_TRANSACTION_LIST_INVALID_STATE] (state, invalidState) {
state.transactionListStateInvalid = invalidState;
},
[LOAD_TRANSACTION_CATEGORY_LIST] (state, allCategories) {
state.allTransactionCategories = allCategories;
state.allTransactionCategoriesMap = {};
@@ -386,6 +405,9 @@ const stores = {
hideAccount: account.hideAccount,
deleteAccount: account.deleteAccount,
getTransaction: transaction.getTransaction,
saveTransaction: transaction.saveTransaction,
loadAllCategories: transactionCategory.loadAllCategories,
getCategory: transactionCategory.getCategory,
saveCategory: transactionCategory.saveCategory,
+3
View File
@@ -13,6 +13,9 @@ export const UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST = 'UPDATE_ACCOUNT_VISIBIL
export const REMOVE_ACCOUNT_FROM_ACCOUNT_LIST = 'REMOVE_ACCOUNT_FROM_ACCOUNT_LIST';
export const UPDATE_ACCOUNT_LIST_INVALID_STATE = 'UPDATE_ACCOUNT_LIST_INVALID_STATE';
export const LOAD_TRANSACTION_LIST = 'LOAD_TRANSACTION_LIST';
export const UPDATE_TRANSACTION_LIST_INVALID_STATE = 'UPDATE_TRANSACTION_LIST_INVALID_STATE';
export const LOAD_TRANSACTION_CATEGORY_LIST = 'LOAD_TRANSACTION_CATEGORY_LIST';
export const ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST = 'ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST';
export const SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST = 'SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST';
+86
View File
@@ -0,0 +1,86 @@
import services from '../lib/services.js';
import logger from '../lib/logger.js';
import {
LOAD_TRANSACTION_LIST, UPDATE_ACCOUNT_LIST_INVALID_STATE,
UPDATE_TRANSACTION_LIST_INVALID_STATE
} from './mutations.js';
function getTransaction(context, { transactionId }) {
return new Promise((resolve, reject) => {
services.getTransaction({
id: transactionId
}).then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
reject({ message: 'Unable to get transaction' });
return;
}
context.commit(LOAD_TRANSACTION_LIST, data.result);
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, false);
resolve(data.result);
}).catch(error => {
logger.error('failed to load transaction info', error);
if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data });
} else if (!error.processed) {
reject({ message: 'Unable to get transaction' });
} else {
reject(error);
}
});
});
}
function saveTransaction(context, { transaction }) {
return new Promise((resolve, reject) => {
let promise = null;
if (!transaction.id) {
promise = services.addTransaction(transaction);
} else {
promise = services.modifyTransaction(transaction);
}
promise.then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
if (!transaction.id) {
reject({ message: 'Unable to add transaction' });
} else {
reject({ message: 'Unable to save transaction' });
}
return;
}
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, true);
resolve(data.result);
}).catch(error => {
logger.error('failed to save transaction', error);
if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data });
} else if (!error.processed) {
if (!transaction.id) {
reject({ message: 'Unable to add transaction' });
} else {
reject({ message: 'Unable to save transaction' });
}
} else {
reject(error);
}
});
});
}
export default {
getTransaction,
saveTransaction
}