reduce unnecessary network requesting

This commit is contained in:
MaysWind
2021-01-10 00:01:55 +08:00
parent 3bd91f2748
commit fb65402897
5 changed files with 240 additions and 105 deletions
+114 -16
View File
@@ -18,6 +18,8 @@ import {
UPDATE_ACCOUNT_LIST_INVALID_STATE,
LOAD_TRANSACTION_LIST,
INIT_TRANSACTION_LIST_FILTER,
UPDATE_TRANSACTION_LIST_FILTER,
COLLAPSE_MONTH_IN_TRANSACTION_LIST,
SAVE_TRANSACTION_IN_TRANSACTION_LIST,
REMOVE_TRANSACTION_FROM_TRANSACTION_LIST,
@@ -92,6 +94,8 @@ import {
} from './account.js';
import {
initTransactionListFilter,
updateTransactionListFilter,
getTransactions,
getTransaction,
saveTransaction,
@@ -99,6 +103,7 @@ import {
collapseMonthInTransactionList,
noTransaction,
hasMoreTransaction,
fillTransactionObject,
calculateMonthTotalAmount,
} from './transaction.js';
@@ -131,6 +136,15 @@ const stores = {
allAccountsMap: {},
allCategorizedAccounts: {},
accountListStateInvalid: true,
transactionsFilter: {
dateType: 0,
maxTime: 0,
minTime: 0,
type: 0,
categoryId: '0',
accountId: '0',
keyword: ''
},
transactions: [],
transactionsNextTimeId: 0,
transactionListStateInvalid: true,
@@ -169,6 +183,13 @@ const stores = {
state.allCategorizedAccounts = {};
state.accountListStateInvalid = true;
state.transactionsFilter.dateType = 0;
state.transactionsFilter.maxTime = 0;
state.transactionsFilter.minTime = 0;
state.transactionsFilter.type = 0;
state.transactionsFilter.categoryId = '0';
state.transactionsFilter.accountId = '0';
state.transactionsFilter.keyword = '';
state.transactions = [];
state.transactionsNextTimeId = 0;
state.transactionListStateInvalid = true;
@@ -340,7 +361,7 @@ const stores = {
[UPDATE_ACCOUNT_LIST_INVALID_STATE] (state, invalidState) {
state.accountListStateInvalid = invalidState;
},
[LOAD_TRANSACTION_LIST] (state, { transactions, reload, autoExpand, defaultCurrency, accountId }) {
[LOAD_TRANSACTION_LIST] (state, { transactions, reload, autoExpand, defaultCurrency }) {
if (reload) {
state.transactions = [];
}
@@ -351,14 +372,9 @@ const stores = {
for (let i = 0; i < transactions.items.length; i++) {
const item = transactions.items[i];
fillTransactionObject(state, item);
const transactionTime = utils.parseDateFromUnixTime(item.time);
item.day = utils.getDay(transactionTime);
item.dayOfWeek = utils.getDayOfWeek(transactionTime);
item.sourceAccount = state.allAccountsMap[item.sourceAccountId];
item.destinationAccount = state.allAccountsMap[item.destinationAccountId];
item.category = state.allTransactionCategoriesMap[item.categoryId];
const transactionYear = utils.getYear(transactionTime);
const transactionMonth = utils.getMonth(transactionTime);
const transactionYearMonth = utils.getYearAndMonth(transactionTime);
@@ -377,7 +393,7 @@ const stores = {
}
if (!currentMonthList || currentMonthList.year !== transactionYear || currentMonthList.month !== transactionMonth) {
calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, accountId, false);
calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, state.transactionsFilter.accountId, false);
state.transactions.push({
year: transactionYear,
@@ -392,23 +408,95 @@ const stores = {
}
currentMonthList.items.push(Object.freeze(item));
calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, accountId, true);
calculateMonthTotalAmount(state, currentMonthList, defaultCurrency, state.transactionsFilter.accountId, true);
}
}
if (transactions.nextTimeSequenceId) {
state.transactionsNextTimeId = transactions.nextTimeSequenceId;
} else {
calculateMonthTotalAmount(state, state.transactions[state.transactions.length - 1], defaultCurrency, accountId, false);
calculateMonthTotalAmount(state, state.transactions[state.transactions.length - 1], defaultCurrency, state.transactionsFilter.accountId, false);
state.transactionsNextTimeId = -1;
}
},
[INIT_TRANSACTION_LIST_FILTER] (state, filter) {
if (filter && utils.isNumber(filter.dateType)) {
state.transactionsFilter.dateType = filter.dateType;
} else {
state.transactionsFilter.dateType = 0;
}
if (filter && utils.isNumber(filter.maxTime)) {
state.transactionsFilter.maxTime = filter.maxTime;
} else {
state.transactionsFilter.maxTime = 0;
}
if (filter && utils.isNumber(filter.minTime)) {
state.transactionsFilter.minTime = filter.minTime;
} else {
state.transactionsFilter.minTime = 0;
}
if (filter && utils.isNumber(filter.type)) {
state.transactionsFilter.type = filter.type;
} else {
state.transactionsFilter.type = 0;
}
if (filter && utils.isString(filter.categoryId)) {
state.transactionsFilter.categoryId = filter.categoryId;
} else {
state.transactionsFilter.categoryId = '0';
}
if (filter && utils.isString(filter.accountId)) {
state.transactionsFilter.accountId = filter.accountId;
} else {
state.transactionsFilter.accountId = '0';
}
if (filter && utils.isString(filter.keyword)) {
state.transactionsFilter.keyword = filter.keyword;
} else {
state.transactionsFilter.keyword = '';
}
},
[UPDATE_TRANSACTION_LIST_FILTER] (state, filter) {
if (filter && utils.isNumber(filter.dateType)) {
state.transactionsFilter.dateType = filter.dateType;
}
if (filter && utils.isNumber(filter.maxTime)) {
state.transactionsFilter.maxTime = filter.maxTime;
}
if (filter && utils.isNumber(filter.minTime)) {
state.transactionsFilter.minTime = filter.minTime;
}
if (filter && utils.isNumber(filter.type)) {
state.transactionsFilter.type = filter.type;
}
if (filter && utils.isString(filter.categoryId)) {
state.transactionsFilter.categoryId = filter.categoryId;
}
if (filter && utils.isString(filter.accountId)) {
state.transactionsFilter.accountId = filter.accountId;
}
if (filter && utils.isString(filter.keyword)) {
state.transactionsFilter.keyword = filter.keyword;
}
},
[COLLAPSE_MONTH_IN_TRANSACTION_LIST] (state, { month, collapse }) {
if (month) {
month.opened = !collapse;
}
},
[SAVE_TRANSACTION_IN_TRANSACTION_LIST] (state, { transaction, defaultCurrency, accountId }) {
[SAVE_TRANSACTION_IN_TRANSACTION_LIST] (state, { transaction, defaultCurrency }) {
for (let i = 0; i < state.transactions.length; i++) {
const transactionMonthList = state.transactions[i];
@@ -420,14 +508,22 @@ const stores = {
for (let j = 0; j < transactionMonthList.items.length; j++) {
if (transactionMonthList.items[j].id === transaction.id) {
transactionMonthList.items.splice(j, 1, transaction);
calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
fillTransactionObject(state, transaction);
if ((state.transactionsFilter.categoryId && state.transactionsFilter.categoryId !== '0' && state.transactionsFilter.categoryId !== transaction.categoryId) ||
(state.transactionsFilter.accountId && state.transactionsFilter.accountId !== '0' && state.transactionsFilter.accountId !== transaction.sourceAccountId && state.transactionsFilter.accountId !== transaction.destinationAccountId)) {
transactionMonthList.items.splice(j, 1);
} else {
transactionMonthList.items.splice(j, 1, transaction);
}
calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, state.transactionsFilter.accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
return;
}
}
}
},
[REMOVE_TRANSACTION_FROM_TRANSACTION_LIST] (state, { transaction, defaultCurrency, accountId }) {
[REMOVE_TRANSACTION_FROM_TRANSACTION_LIST] (state, { transaction, defaultCurrency }) {
for (let i = 0; i < state.transactions.length; i++) {
const transactionMonthList = state.transactions[i];
@@ -446,7 +542,7 @@ const stores = {
if (transactionMonthList.items.length < 1) {
state.transactions.splice(i, 1);
} else {
calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, state.transactionsFilter.accountId, i >= state.transactions.length - 1 && state.transactionsNextTimeId > 0);
}
}
},
@@ -648,6 +744,8 @@ const stores = {
deleteAccount,
// transaction
initTransactionListFilter,
updateTransactionListFilter,
getTransactions,
getTransaction,
saveTransaction,
+2
View File
@@ -14,6 +14,8 @@ export const REMOVE_ACCOUNT_FROM_ACCOUNT_LIST = 'REMOVE_ACCOUNT_FROM_ACCOUNT_LIS
export const UPDATE_ACCOUNT_LIST_INVALID_STATE = 'UPDATE_ACCOUNT_LIST_INVALID_STATE';
export const LOAD_TRANSACTION_LIST = 'LOAD_TRANSACTION_LIST';
export const INIT_TRANSACTION_LIST_FILTER = 'INIT_TRANSACTION_LIST_FILTER';
export const UPDATE_TRANSACTION_LIST_FILTER = 'UPDATE_TRANSACTION_LIST_FILTER';
export const COLLAPSE_MONTH_IN_TRANSACTION_LIST = 'COLLAPSE_MONTH_IN_TRANSACTION_LIST';
export const SAVE_TRANSACTION_IN_TRANSACTION_LIST = 'SAVE_TRANSACTION_IN_TRANSACTION_LIST';
export const REMOVE_TRANSACTION_FROM_TRANSACTION_LIST = 'REMOVE_TRANSACTION_FROM_TRANSACTION_LIST';
+56 -22
View File
@@ -7,7 +7,10 @@ import { getExchangedAmount } from "./exchangeRates.js";
import {
LOAD_TRANSACTION_LIST,
INIT_TRANSACTION_LIST_FILTER,
UPDATE_TRANSACTION_LIST_FILTER,
COLLAPSE_MONTH_IN_TRANSACTION_LIST,
SAVE_TRANSACTION_IN_TRANSACTION_LIST,
REMOVE_TRANSACTION_FROM_TRANSACTION_LIST,
UPDATE_TRANSACTION_LIST_INVALID_STATE,
UPDATE_ACCOUNT_LIST_INVALID_STATE,
@@ -18,23 +21,31 @@ const emptyTransactionResult = {
transactionsNextTimeId: 0
};
export function getTransactions(context, { reload, autoExpand, defaultCurrency, maxTime, minTime, type, categoryId, accountId, keyword }) {
export function initTransactionListFilter(context, filter) {
context.commit(INIT_TRANSACTION_LIST_FILTER, filter);
}
export function updateTransactionListFilter(context, filter) {
context.commit(UPDATE_TRANSACTION_LIST_FILTER, filter);
}
export function getTransactions(context, { reload, autoExpand, defaultCurrency }) {
let actualMaxTime = context.state.transactionsNextTimeId;
if (reload && maxTime > 0) {
actualMaxTime = maxTime;
} else if (reload && maxTime <= 0) {
if (reload && context.state.transactionsFilter.maxTime > 0) {
actualMaxTime = context.state.transactionsFilter.maxTime * 1000 + 999;
} else if (reload && context.state.transactionsFilter.maxTime <= 0) {
actualMaxTime = 0;
}
return new Promise((resolve, reject) => {
services.getTransactions({
maxTime: actualMaxTime,
minTime: minTime,
type: type,
categoryId: categoryId,
accountId: accountId,
keyword: keyword
minTime: context.state.transactionsFilter.minTime * 1000,
type: context.state.transactionsFilter.type,
categoryId: context.state.transactionsFilter.categoryId,
accountId: context.state.transactionsFilter.accountId,
keyword: context.state.transactionsFilter.keyword
}).then(response => {
const data = response.data;
@@ -44,8 +55,7 @@ export function getTransactions(context, { reload, autoExpand, defaultCurrency,
transactions: emptyTransactionResult,
reload: reload,
autoExpand: autoExpand,
defaultCurrency: defaultCurrency,
accountId: accountId
defaultCurrency: defaultCurrency
});
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
}
@@ -58,8 +68,7 @@ export function getTransactions(context, { reload, autoExpand, defaultCurrency,
transactions: data.result,
reload: reload,
autoExpand: autoExpand,
defaultCurrency: defaultCurrency,
accountId: accountId
defaultCurrency: defaultCurrency
});
if (reload) {
@@ -75,8 +84,7 @@ export function getTransactions(context, { reload, autoExpand, defaultCurrency,
transactions: emptyTransactionResult,
reload: reload,
autoExpand: autoExpand,
defaultCurrency: defaultCurrency,
accountId: accountId
defaultCurrency: defaultCurrency
});
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
}
@@ -119,7 +127,7 @@ export function getTransaction(context, { transactionId }) {
});
}
export function saveTransaction(context, { transaction }) {
export function saveTransaction(context, { transaction, defaultCurrency }) {
return new Promise((resolve, reject) => {
let promise = null;
@@ -144,7 +152,10 @@ export function saveTransaction(context, { transaction }) {
if (!transaction.id) {
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
} else {
context.commit(UPDATE_TRANSACTION_LIST_INVALID_STATE, true);
context.commit(SAVE_TRANSACTION_IN_TRANSACTION_LIST, {
transaction: data.result,
defaultCurrency: defaultCurrency
});
}
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, true);
@@ -168,7 +179,7 @@ export function saveTransaction(context, { transaction }) {
});
}
export function deleteTransaction(context, { transaction, defaultCurrency, accountId, beforeResolve }) {
export function deleteTransaction(context, { transaction, defaultCurrency, beforeResolve }) {
return new Promise((resolve, reject) => {
services.deleteTransaction({
id: transaction.id
@@ -184,15 +195,13 @@ export function deleteTransaction(context, { transaction, defaultCurrency, accou
beforeResolve(() => {
context.commit(REMOVE_TRANSACTION_FROM_TRANSACTION_LIST, {
transaction: transaction,
defaultCurrency: defaultCurrency,
accountId: accountId
defaultCurrency: defaultCurrency
});
});
} else {
context.commit(REMOVE_TRANSACTION_FROM_TRANSACTION_LIST, {
transaction: transaction,
defaultCurrency: defaultCurrency,
accountId: accountId
defaultCurrency: defaultCurrency
});
}
@@ -238,6 +247,31 @@ export function hasMoreTransaction(state) {
return state.transactionsNextTimeId > 0;
}
export function fillTransactionObject(state, transaction) {
if (!transaction) {
return;
}
const transactionTime = utils.parseDateFromUnixTime(transaction.time);
transaction.day = utils.getDay(transactionTime);
transaction.dayOfWeek = utils.getDayOfWeek(transactionTime);
if (transaction.sourceAccountId) {
transaction.sourceAccount = state.allAccountsMap[transaction.sourceAccountId];
}
if (transaction.destinationAccountId) {
transaction.destinationAccount = state.allAccountsMap[transaction.destinationAccountId];
}
if (transaction.categoryId) {
transaction.category = state.allTransactionCategoriesMap[transaction.categoryId];
}
return transaction;
}
export function calculateMonthTotalAmount(state, transactionMonthList, defaultCurrency, accountId, incomplete) {
if (!transactionMonthList) {
return;