mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-20 01:34:24 +08:00
code refactor
This commit is contained in:
@@ -0,0 +1,318 @@
|
||||
import services from '../lib/services.js';
|
||||
import logger from '../lib/logger.js';
|
||||
|
||||
import {
|
||||
LOAD_ACCOUNT_LIST,
|
||||
ADD_ACCOUNT_TO_ACCOUNT_LIST,
|
||||
SAVE_ACCOUNT_IN_ACCOUNT_LIST,
|
||||
CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST,
|
||||
UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST,
|
||||
REMOVE_ACCOUNT_FROM_ACCOUNT_LIST,
|
||||
UPDATE_ACCOUNT_LIST_INVALID_STATE
|
||||
} from './mutations.js';
|
||||
|
||||
function loadAllAccounts(context, { force }) {
|
||||
if (!force && !context.state.accountListStateInvalid) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(context.state.allAccounts);
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getAllAccounts({
|
||||
visibleOnly: false
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get account list' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(LOAD_ACCOUNT_LIST, data.result);
|
||||
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
if (force) {
|
||||
logger.error('failed to force load account list', error);
|
||||
} else {
|
||||
logger.error('failed to load account list', 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 account list' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getAccount(context, { accountId }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getAccount({
|
||||
id: accountId
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get account' });
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to load account 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 account' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveAccount(context, { account }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let promise = null;
|
||||
|
||||
if (!account.id) {
|
||||
promise = services.addAccount(account);
|
||||
} else {
|
||||
promise = services.modifyAccount(account);
|
||||
}
|
||||
|
||||
promise.then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (!account.id) {
|
||||
reject({ message: 'Unable to add account' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save account' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!account.id) {
|
||||
context.commit(ADD_ACCOUNT_TO_ACCOUNT_LIST, data.result);
|
||||
} else {
|
||||
context.commit(SAVE_ACCOUNT_IN_ACCOUNT_LIST, data.result);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save account', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
if (!account.id) {
|
||||
reject({ message: 'Unable to add account' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save account' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function changeAccountDisplayOrder(context, { accountId, from, to }) {
|
||||
const account = context.state.allAccountsMap[accountId];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!account ||
|
||||
!context.state.allCategorizedAccounts[account.category] ||
|
||||
!context.state.allCategorizedAccounts[account.category].accounts ||
|
||||
!context.state.allCategorizedAccounts[account.category].accounts[to]) {
|
||||
reject({ message: 'Unable to move account' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, true);
|
||||
context.commit(CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST, {
|
||||
account: account,
|
||||
from: from,
|
||||
to: to
|
||||
});
|
||||
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
function updateAccountDisplayOrders(context) {
|
||||
const newDisplayOrders = [];
|
||||
|
||||
for (let category in context.state.allCategorizedAccounts) {
|
||||
if (!Object.prototype.hasOwnProperty.call(context.state.allCategorizedAccounts, category)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const accountList = context.state.allCategorizedAccounts[category].accounts;
|
||||
|
||||
for (let i = 0; i < accountList.length; i++) {
|
||||
newDisplayOrders.push({
|
||||
id: accountList[i].id,
|
||||
displayOrder: i + 1
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.moveAccount({
|
||||
newDisplayOrders: newDisplayOrders
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to move account' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_ACCOUNT_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save accounts display order', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to move account' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function hideAccount(context, { account, hidden }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.hideAccount({
|
||||
id: account.id,
|
||||
hidden: hidden
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (hidden) {
|
||||
reject({ message: 'Unable to hide this account' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this account' });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST, {
|
||||
account: account,
|
||||
hidden: hidden
|
||||
});
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to change account visibility', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
if (hidden) {
|
||||
reject({ message: 'Unable to hide this account' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this account' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function deleteAccount(context, { account, beforeResolve }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.deleteAccount({
|
||||
id: account.id
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to delete this account' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (beforeResolve) {
|
||||
beforeResolve(() => {
|
||||
context.commit(REMOVE_ACCOUNT_FROM_ACCOUNT_LIST, account);
|
||||
});
|
||||
} else {
|
||||
context.commit(REMOVE_ACCOUNT_FROM_ACCOUNT_LIST, account);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to delete account', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to delete this account' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function allAvailableAccountsCount(state) {
|
||||
let allAccountCount = 0;
|
||||
|
||||
for (let category in state.allCategorizedAccounts) {
|
||||
if (!Object.prototype.hasOwnProperty.call(state.allCategorizedAccounts, category)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
allAccountCount += state.allCategorizedAccounts[category].accounts.length;
|
||||
}
|
||||
|
||||
return allAccountCount;
|
||||
}
|
||||
|
||||
function allVisibleAccountsCount(state) {
|
||||
let shownAccountCount = 0;
|
||||
|
||||
for (let category in state.allCategorizedAccounts) {
|
||||
if (!Object.prototype.hasOwnProperty.call(state.allCategorizedAccounts, category)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const accountList = state.allCategorizedAccounts[category].accounts;
|
||||
|
||||
for (let i = 0; i < accountList.length; i++) {
|
||||
if (!accountList[i].hidden) {
|
||||
shownAccountCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shownAccountCount;
|
||||
}
|
||||
|
||||
export default {
|
||||
loadAllAccounts,
|
||||
getAccount,
|
||||
saveAccount,
|
||||
changeAccountDisplayOrder,
|
||||
updateAccountDisplayOrders,
|
||||
hideAccount,
|
||||
deleteAccount,
|
||||
allAvailableAccountsCount,
|
||||
allVisibleAccountsCount,
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
import utils from "../lib/utils.js";
|
||||
|
||||
import {
|
||||
LOAD_ACCOUNT_LIST,
|
||||
ADD_ACCOUNT_TO_ACCOUNT_LIST,
|
||||
SAVE_ACCOUNT_IN_ACCOUNT_LIST,
|
||||
CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST,
|
||||
UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST,
|
||||
REMOVE_ACCOUNT_FROM_ACCOUNT_LIST,
|
||||
UPDATE_ACCOUNT_LIST_INVALID_STATE,
|
||||
|
||||
LOAD_TRANSACTION_CATEGORY_LIST,
|
||||
|
||||
LOAD_TRANSACTION_TAG_LIST,
|
||||
ADD_TAG_TO_TRANSACTION_TAG_LIST,
|
||||
SAVE_TAG_IN_TRANSACTION_TAG_LIST,
|
||||
CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST,
|
||||
REMOVE_TAG_FROM_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE
|
||||
} from './mutations.js';
|
||||
|
||||
import account from './account.js';
|
||||
import transactionTag from './transactionTag.js';
|
||||
|
||||
const stores = {
|
||||
strict: process.env.NODE_ENV !== 'production',
|
||||
state: {
|
||||
allAccounts: [],
|
||||
allAccountsMap: {},
|
||||
allCategorizedAccounts: {},
|
||||
accountListStateInvalid: true,
|
||||
allTransactionCategories: [],
|
||||
allTransactionCategoriesMap: {},
|
||||
allTransactionTags: [],
|
||||
allTransactionTagsMap: {},
|
||||
transactionTagListStateInvalid: true,
|
||||
transactions: [],
|
||||
},
|
||||
getters: {
|
||||
allAvailableAccountsCount: account.allAvailableAccountsCount,
|
||||
allVisibleAccountsCount: account.allVisibleAccountsCount,
|
||||
},
|
||||
mutations: {
|
||||
[LOAD_ACCOUNT_LIST] (state, accounts) {
|
||||
state.allAccounts = accounts;
|
||||
state.allAccountsMap = {};
|
||||
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
const account = accounts[i];
|
||||
state.allAccountsMap[account.id] = account;
|
||||
}
|
||||
|
||||
state.allCategorizedAccounts = utils.getCategorizedAccounts(accounts);
|
||||
},
|
||||
[ADD_ACCOUNT_TO_ACCOUNT_LIST] (state, account) {
|
||||
let insertIndexToAllList = 0;
|
||||
|
||||
for (let i = 0; i < state.allAccounts.length; i++) {
|
||||
if (state.allAccounts[i].category > account.category) {
|
||||
insertIndexToAllList = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
state.allAccounts.splice(insertIndexToAllList, 0, account);
|
||||
|
||||
state.allAccountsMap[account.id] = account;
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
accountList.push(account);
|
||||
} else {
|
||||
state.allCategorizedAccounts = utils.getCategorizedAccounts(state.allAccounts);
|
||||
}
|
||||
},
|
||||
[SAVE_ACCOUNT_IN_ACCOUNT_LIST] (state, account) {
|
||||
for (let i = 0; i < state.allAccounts.length; i++) {
|
||||
if (state.allAccounts[i].id === account.id) {
|
||||
state.allAccounts.splice(i, 1, account);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
state.allAccountsMap[account.id] = account;
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
|
||||
for (let i = 0; i < accountList.length; i++) {
|
||||
if (accountList[i].id === account.id) {
|
||||
accountList.splice(i, 1, account);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST] (state, { account, from, to }) {
|
||||
let fromAccount = null;
|
||||
let toAccount = null;
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
fromAccount = accountList[from];
|
||||
toAccount = accountList[to];
|
||||
|
||||
accountList.splice(to, 0, accountList.splice(from, 1)[0]);
|
||||
}
|
||||
|
||||
if (fromAccount && toAccount) {
|
||||
let globalFromIndex = -1;
|
||||
let globalToIndex = -1;
|
||||
|
||||
for (let i = 0; i < state.allAccounts.length; i++) {
|
||||
if (state.allAccounts[i].id === fromAccount.id) {
|
||||
globalFromIndex = i;
|
||||
} else if (state.allAccounts[i].id === toAccount.id) {
|
||||
globalToIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (globalFromIndex >= 0 && globalToIndex >= 0) {
|
||||
state.allAccounts.splice(globalToIndex, 0, state.allAccounts.splice(globalFromIndex, 1)[0]);
|
||||
}
|
||||
}
|
||||
},
|
||||
[UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST] (state, { account, hidden }) {
|
||||
if (state.allAccountsMap[account.id]) {
|
||||
state.allAccountsMap[account.id].hidden = hidden;
|
||||
}
|
||||
},
|
||||
[REMOVE_ACCOUNT_FROM_ACCOUNT_LIST] (state, account) {
|
||||
for (let i = 0; i < state.allAccounts.length; i++) {
|
||||
if (state.allAccounts[i].id === account.id) {
|
||||
state.allAccounts.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allAccountsMap[account.id]) {
|
||||
delete state.allAccountsMap[account.id];
|
||||
}
|
||||
|
||||
if (state.allCategorizedAccounts[account.category]) {
|
||||
const accountList = state.allCategorizedAccounts[account.category].accounts;
|
||||
|
||||
for (let i = 0; i < accountList.length; i++) {
|
||||
if (accountList[i].id === account.id) {
|
||||
accountList.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[UPDATE_ACCOUNT_LIST_INVALID_STATE] (state, invalidState) {
|
||||
state.accountListStateInvalid = invalidState;
|
||||
},
|
||||
[LOAD_TRANSACTION_CATEGORY_LIST] (state, categories) {
|
||||
state.allTransactionCategories = categories;
|
||||
state.allTransactionCategoriesMap = {};
|
||||
|
||||
for (let i = 0; i < categories.length; i++) {
|
||||
const category = categories[i];
|
||||
state.allTransactionCategoriesMap[category.id] = category;
|
||||
}
|
||||
},
|
||||
[LOAD_TRANSACTION_TAG_LIST] (state, tags) {
|
||||
state.allTransactionTags = tags;
|
||||
state.allTransactionTagsMap = {};
|
||||
|
||||
for (let i = 0; i < tags.length; i++) {
|
||||
const tag = tags[i];
|
||||
state.allTransactionTagsMap[tag.id] = tag;
|
||||
}
|
||||
},
|
||||
[ADD_TAG_TO_TRANSACTION_TAG_LIST] (state, tag) {
|
||||
state.allTransactionTags.push(tag);
|
||||
state.allTransactionTagsMap[tag.id] = tag;
|
||||
},
|
||||
[SAVE_TAG_IN_TRANSACTION_TAG_LIST] (state, tag) {
|
||||
for (let i = 0; i < state.allTransactionTags.length; i++) {
|
||||
if (state.allTransactionTags[i].id === tag.id) {
|
||||
state.allTransactionTags.splice(i, 1, tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
state.allTransactionTagsMap[tag.id] = tag;
|
||||
},
|
||||
[CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST] (state, { from, to }) {
|
||||
state.allTransactionTags.splice(to, 0, state.allTransactionTags.splice(from, 1)[0]);
|
||||
},
|
||||
[UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST] (state, { tag, hidden }) {
|
||||
if (state.allTransactionTagsMap[tag.id]) {
|
||||
state.allTransactionTagsMap[tag.id].hidden = hidden;
|
||||
}
|
||||
},
|
||||
[REMOVE_TAG_FROM_TRANSACTION_TAG_LIST] (state, tag) {
|
||||
for (let i = 0; i < state.allTransactionTags.length; i++) {
|
||||
if (state.allTransactionTags[i].id === tag.id) {
|
||||
state.allTransactionTags.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allTransactionTagsMap[tag.id]) {
|
||||
delete state.allTransactionTagsMap[tag.id];
|
||||
}
|
||||
},
|
||||
[UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE] (state, invalidState) {
|
||||
state.transactionTagListStateInvalid = invalidState;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
loadAllAccounts: account.loadAllAccounts,
|
||||
saveAccount: account.saveAccount,
|
||||
getAccount: account.getAccount,
|
||||
changeAccountDisplayOrder: account.changeAccountDisplayOrder,
|
||||
updateAccountDisplayOrders: account.updateAccountDisplayOrders,
|
||||
hideAccount: account.hideAccount,
|
||||
deleteAccount: account.deleteAccount,
|
||||
loadAllTags: transactionTag.loadAllTags,
|
||||
saveTag: transactionTag.saveTag,
|
||||
changeTagDisplayOrder: transactionTag.changeTagDisplayOrder,
|
||||
updateTagDisplayOrders: transactionTag.updateTagDisplayOrders,
|
||||
hideTag: transactionTag.hideTag,
|
||||
deleteTag: transactionTag.deleteTag,
|
||||
}
|
||||
};
|
||||
|
||||
export default stores;
|
||||
@@ -0,0 +1,17 @@
|
||||
export const LOAD_ACCOUNT_LIST = 'LOAD_ACCOUNT_LIST';
|
||||
export const ADD_ACCOUNT_TO_ACCOUNT_LIST = 'ADD_ACCOUNT_TO_ACCOUNT_LIST';
|
||||
export const SAVE_ACCOUNT_IN_ACCOUNT_LIST = 'SAVE_ACCOUNT_IN_ACCOUNT_LIST';
|
||||
export const CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST = 'CHANGE_ACCOUNT_DISPLAY_ORDER_IN_ACCOUNT_LIST';
|
||||
export const UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST = 'UPDATE_ACCOUNT_VISIBILITY_IN_ACCOUNT_LIST';
|
||||
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_CATEGORY_LIST = 'LOAD_TRANSACTION_CATEGORY_LIST';
|
||||
|
||||
export const LOAD_TRANSACTION_TAG_LIST = 'LOAD_TRANSACTION_TAG_LIST';
|
||||
export const ADD_TAG_TO_TRANSACTION_TAG_LIST = 'ADD_TAG_TO_TRANSACTION_TAG_LIST';
|
||||
export const SAVE_TAG_IN_TRANSACTION_TAG_LIST = 'SAVE_TAG_IN_TRANSACTION_TAG_LIST';
|
||||
export const CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST = 'CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST';
|
||||
export const UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST = 'UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST';
|
||||
export const REMOVE_TAG_FROM_TRANSACTION_TAG_LIST = 'REMOVE_TAG_FROM_TRANSACTION_TAG_LIST';
|
||||
export const UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE = 'UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE';
|
||||
@@ -0,0 +1,248 @@
|
||||
import services from '../lib/services.js';
|
||||
import logger from '../lib/logger.js';
|
||||
|
||||
import {
|
||||
LOAD_TRANSACTION_TAG_LIST,
|
||||
ADD_TAG_TO_TRANSACTION_TAG_LIST,
|
||||
SAVE_TAG_IN_TRANSACTION_TAG_LIST,
|
||||
CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST,
|
||||
REMOVE_TAG_FROM_TRANSACTION_TAG_LIST,
|
||||
UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
function loadAllTags(context, { force }) {
|
||||
if (!force && !context.state.transactionTagListStateInvalid) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(context.state.allTransactionTags);
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getAllTransactionTags().then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get tag list' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_TAG_LIST, data.result);
|
||||
context.commit(UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
if (force) {
|
||||
logger.error('failed to force load tag list', error);
|
||||
} else {
|
||||
logger.error('failed to load tag list', 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 tag list' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveTag(context, { tag }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let promise = null;
|
||||
|
||||
if (!tag.id) {
|
||||
promise = services.addTransactionTag(tag);
|
||||
} else {
|
||||
promise = services.modifyTransactionTag(tag);
|
||||
}
|
||||
|
||||
promise.then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (!tag.id) {
|
||||
reject({ message: 'Unable to add tag' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save tag' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tag.id) {
|
||||
context.commit(ADD_TAG_TO_TRANSACTION_TAG_LIST, data.result);
|
||||
} else {
|
||||
context.commit(SAVE_TAG_IN_TRANSACTION_TAG_LIST, data.result);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save tag', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
if (!tag.id) {
|
||||
reject({ message: 'Unable to add tag' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save tag' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function changeTagDisplayOrder(context, { tagId, from, to }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let tag = null;
|
||||
|
||||
for (let i = 0; i < context.state.allTransactionTags.length; i++) {
|
||||
if (context.state.allTransactionTags[i].id === tagId) {
|
||||
tag = context.state.allTransactionTags[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!tag || !context.state.allTransactionTags[to]) {
|
||||
reject({ message: 'Unable to move tag' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE, true);
|
||||
context.commit(CHANGE_TAG_DISPLAY_ORDER_IN_TRANSACTION_TAG_LIST, {
|
||||
tag: tag,
|
||||
from: from,
|
||||
to: to
|
||||
});
|
||||
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
function updateTagDisplayOrders(context) {
|
||||
const newDisplayOrders = [];
|
||||
|
||||
for (let i = 0; i < context.state.allTransactionTags.length; i++) {
|
||||
newDisplayOrders.push({
|
||||
id: context.state.allTransactionTags[i].id,
|
||||
displayOrder: i + 1
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.moveTransactionTag({
|
||||
newDisplayOrders: newDisplayOrders
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to move tag' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_TAG_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save tags display order', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to move tag' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function hideTag(context, { tag, hidden }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.hideTransactionTag({
|
||||
id: tag.id,
|
||||
hidden: hidden
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (hidden) {
|
||||
reject({ message: 'Unable to hide this tag' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this tag' });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TAG_VISIBILITY_IN_TRANSACTION_TAG_LIST, {
|
||||
tag: tag,
|
||||
hidden: hidden
|
||||
});
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to change tag visibility', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
if (hidden) {
|
||||
reject({ message: 'Unable to hide this tag' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this tag' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function deleteTag(context, { tag, beforeResolve }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.deleteTransactionTag({
|
||||
id: tag.id
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to delete this tag' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (beforeResolve) {
|
||||
beforeResolve(() => {
|
||||
context.commit(REMOVE_TAG_FROM_TRANSACTION_TAG_LIST, tag);
|
||||
});
|
||||
} else {
|
||||
context.commit(REMOVE_TAG_FROM_TRANSACTION_TAG_LIST, tag);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to delete tag', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
reject({ message: 'Unable to delete this tag' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
loadAllTags,
|
||||
saveTag,
|
||||
changeTagDisplayOrder,
|
||||
updateTagDisplayOrders,
|
||||
hideTag,
|
||||
deleteTag,
|
||||
}
|
||||
Reference in New Issue
Block a user