From 1b2a37e6d12fa93ffa4da1fd96750e2e0d9d79b3 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 7 Jan 2021 22:28:28 +0800 Subject: [PATCH] code refactor --- src/store/account.js | 45 +++++ src/store/index.js | 26 ++- src/store/mutations.js | 3 + src/store/transaction.js | 86 +++++++++ src/views/mobile/transactions/Edit.vue | 258 ++++++++++--------------- 5 files changed, 262 insertions(+), 156 deletions(-) create mode 100644 src/store/transaction.js diff --git a/src/store/account.js b/src/store/account.js index 71d27b5d..9e94ae24 100644 --- a/src/store/account.js +++ b/src/store/account.js @@ -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, } diff --git a/src/store/index.js b/src/store/index.js index aa6588e4..5955189a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -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, diff --git a/src/store/mutations.js b/src/store/mutations.js index f3932fc0..f0dd0733 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -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'; diff --git a/src/store/transaction.js b/src/store/transaction.js new file mode 100644 index 00000000..35d202a5 --- /dev/null +++ b/src/store/transaction.js @@ -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 +} diff --git a/src/views/mobile/transactions/Edit.vue b/src/views/mobile/transactions/Edit.vue index 0badf087..0ecde4c9 100644 --- a/src/views/mobile/transactions/Edit.vue +++ b/src/views/mobile/transactions/Edit.vue @@ -148,7 +148,7 @@ @@ -170,7 +170,7 @@