mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
code refactor
This commit is contained in:
+108
-8
@@ -18,6 +18,11 @@ import {
|
||||
UPDATE_ACCOUNT_LIST_INVALID_STATE,
|
||||
|
||||
LOAD_TRANSACTION_CATEGORY_LIST,
|
||||
ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST,
|
||||
SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST,
|
||||
CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST,
|
||||
UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST,
|
||||
REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST,
|
||||
UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE,
|
||||
|
||||
LOAD_TRANSACTION_TAG_LIST,
|
||||
@@ -46,7 +51,7 @@ const stores = {
|
||||
allAccountsMap: {},
|
||||
allCategorizedAccounts: {},
|
||||
accountListStateInvalid: true,
|
||||
allTransactionCategories: [],
|
||||
allTransactionCategories: {},
|
||||
allTransactionCategoriesMap: {},
|
||||
transactionCategoryListStateInvalid: true,
|
||||
allTransactionTags: [],
|
||||
@@ -69,7 +74,7 @@ const stores = {
|
||||
state.allAccountsMap = {};
|
||||
state.allCategorizedAccounts = {};
|
||||
state.accountListStateInvalid = true;
|
||||
state.allTransactionCategories = [];
|
||||
state.allTransactionCategories = {};
|
||||
state.allTransactionCategoriesMap = {};
|
||||
state.allTransactionTags = [];
|
||||
state.allTransactionTagsMap = {};
|
||||
@@ -203,13 +208,101 @@ const stores = {
|
||||
[UPDATE_ACCOUNT_LIST_INVALID_STATE] (state, invalidState) {
|
||||
state.accountListStateInvalid = invalidState;
|
||||
},
|
||||
[LOAD_TRANSACTION_CATEGORY_LIST] (state, categories) {
|
||||
state.allTransactionCategories = categories;
|
||||
[LOAD_TRANSACTION_CATEGORY_LIST] (state, allCategories) {
|
||||
state.allTransactionCategories = allCategories;
|
||||
state.allTransactionCategoriesMap = {};
|
||||
|
||||
for (let i = 0; i < categories.length; i++) {
|
||||
const category = categories[i];
|
||||
state.allTransactionCategoriesMap[category.id] = category;
|
||||
for (let categoryType in allCategories) {
|
||||
if (!Object.prototype.hasOwnProperty.call(allCategories, categoryType)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const categories = allCategories[categoryType];
|
||||
|
||||
for (let i = 0; i < categories.length; i++) {
|
||||
const category = categories[i];
|
||||
state.allTransactionCategoriesMap[category.id] = category;
|
||||
|
||||
for (let j = 0; j < category.subCategories.length; j++) {
|
||||
const subCategory = category.subCategories[j];
|
||||
state.allTransactionCategoriesMap[subCategory.id] = subCategory;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST] (state, category) {
|
||||
let categoryList = null;
|
||||
|
||||
if (!category.parentId || category.parentId === '0') {
|
||||
categoryList = state.allTransactionCategories[category.type];
|
||||
} else if (state.allTransactionCategoriesMap[category.parentId]) {
|
||||
categoryList = state.allTransactionCategoriesMap[category.parentId].subCategories;
|
||||
}
|
||||
|
||||
if (categoryList) {
|
||||
categoryList.push(category);
|
||||
}
|
||||
|
||||
state.allTransactionCategoriesMap[category.id] = category;
|
||||
},
|
||||
[SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST] (state, category) {
|
||||
let categoryList = null;
|
||||
|
||||
if (!category.parentId || category.parentId === '0') {
|
||||
categoryList = state.allTransactionCategories[category.type];
|
||||
} else if (state.allTransactionCategoriesMap[category.parentId]) {
|
||||
categoryList = state.allTransactionCategoriesMap[category.parentId].subCategories;
|
||||
}
|
||||
|
||||
if (categoryList) {
|
||||
for (let i = 0; i < categoryList.length; i++) {
|
||||
if (categoryList[i].id === category.id) {
|
||||
categoryList.splice(i, 1, category);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.allTransactionCategoriesMap[category.id] = category;
|
||||
},
|
||||
[CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST] (state, { category, from, to }) {
|
||||
let categoryList = null;
|
||||
|
||||
if (!category.parentId || category.parentId === '0') {
|
||||
categoryList = state.allTransactionCategories[category.type];
|
||||
} else if (state.allTransactionCategoriesMap[category.parentId]) {
|
||||
categoryList = state.allTransactionCategoriesMap[category.parentId].subCategories;
|
||||
}
|
||||
|
||||
if (categoryList) {
|
||||
categoryList.splice(to, 0, categoryList.splice(from, 1)[0]);
|
||||
}
|
||||
},
|
||||
[UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST] (state, { category, hidden }) {
|
||||
if (state.allTransactionCategoriesMap[category.id]) {
|
||||
state.allTransactionCategoriesMap[category.id].hidden = hidden;
|
||||
}
|
||||
},
|
||||
[REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST] (state, category) {
|
||||
let categoryList = null;
|
||||
|
||||
if (!category.parentId || category.parentId === '0') {
|
||||
categoryList = state.allTransactionCategories[category.type];
|
||||
} else if (state.allTransactionCategoriesMap[category.parentId]) {
|
||||
categoryList = state.allTransactionCategoriesMap[category.parentId].subCategories;
|
||||
}
|
||||
|
||||
if (categoryList) {
|
||||
for (let i = 0; i < categoryList.length; i++) {
|
||||
if (categoryList[i].id === category.id) {
|
||||
categoryList.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.allTransactionCategoriesMap[category.id]) {
|
||||
delete state.allTransactionCategoriesMap[category.id];
|
||||
}
|
||||
},
|
||||
[UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE] (state, invalidState) {
|
||||
@@ -293,7 +386,14 @@ const stores = {
|
||||
hideAccount: account.hideAccount,
|
||||
deleteAccount: account.deleteAccount,
|
||||
|
||||
addTransactionCategoryBatch: transactionCategory.addTransactionCategoryBatch,
|
||||
loadAllCategories: transactionCategory.loadAllCategories,
|
||||
getCategory: transactionCategory.getCategory,
|
||||
saveCategory: transactionCategory.saveCategory,
|
||||
addCategories: transactionCategory.addCategories,
|
||||
changeCategoryDisplayOrder: transactionCategory.changeCategoryDisplayOrder,
|
||||
updateCategoryDisplayOrders: transactionCategory.updateCategoryDisplayOrders,
|
||||
hideCategory: transactionCategory.hideCategory,
|
||||
deleteCategory: transactionCategory.deleteCategory,
|
||||
|
||||
loadAllTags: transactionTag.loadAllTags,
|
||||
saveTag: transactionTag.saveTag,
|
||||
|
||||
@@ -14,6 +14,11 @@ 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_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';
|
||||
export const CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST = 'CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST';
|
||||
export const UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST = 'UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST';
|
||||
export const REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST = 'REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST';
|
||||
export const UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE = 'UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE';
|
||||
|
||||
export const LOAD_TRANSACTION_TAG_LIST = 'LOAD_TRANSACTION_TAG_LIST';
|
||||
|
||||
@@ -1,11 +1,162 @@
|
||||
import categoryContants from '../consts/category.js';
|
||||
import services from '../lib/services.js';
|
||||
import logger from '../lib/logger.js';
|
||||
|
||||
import {
|
||||
LOAD_TRANSACTION_CATEGORY_LIST,
|
||||
ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST,
|
||||
SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST,
|
||||
CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST,
|
||||
UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST,
|
||||
REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST,
|
||||
UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
function addTransactionCategoryBatch(context, { categories }) {
|
||||
function loadAllCategories(context, { force }) {
|
||||
if (!force && !context.state.transactionCategoryListStateInvalid) {
|
||||
return new Promise((resolve) => {
|
||||
resolve(context.state.allTransactionCategories);
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getAllTransactionCategories().then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get category list' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.result[categoryContants.allCategoryTypes.Income]) {
|
||||
data.result[categoryContants.allCategoryTypes.Income] = [];
|
||||
}
|
||||
|
||||
if (!data.result[categoryContants.allCategoryTypes.Expense]) {
|
||||
data.result[categoryContants.allCategoryTypes.Expense] = [];
|
||||
}
|
||||
|
||||
if (!data.result[categoryContants.allCategoryTypes.Transfer]) {
|
||||
data.result[categoryContants.allCategoryTypes.Transfer] = [];
|
||||
}
|
||||
|
||||
for (let categoryType in data.result) {
|
||||
if (!Object.prototype.hasOwnProperty.call(data.result, categoryType)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const categories = data.result[categoryType];
|
||||
|
||||
for (let i = 0; i < categories.length; i++) {
|
||||
const category = categories[i];
|
||||
|
||||
if (!category.subCategories) {
|
||||
category.subCategories = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.commit(LOAD_TRANSACTION_CATEGORY_LIST, data.result);
|
||||
context.commit(UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
if (force) {
|
||||
logger.error('failed to force load category list', error);
|
||||
} else {
|
||||
logger.error('failed to load category 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 category list' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getCategory(context, { categoryId }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.getTransactionCategory({
|
||||
id: categoryId
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to get category' });
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to load category 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 category' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveCategory(context, { category }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let promise = null;
|
||||
|
||||
if (!category.id) {
|
||||
promise = services.addTransactionCategory(category);
|
||||
} else {
|
||||
promise = services.modifyTransactionCategory(category);
|
||||
}
|
||||
|
||||
promise.then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (!category.id) {
|
||||
reject({ message: 'Unable to add category' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save category' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.result.subCategories) {
|
||||
data.result.subCategories = [];
|
||||
}
|
||||
|
||||
if (!category.id) {
|
||||
context.commit(ADD_CATEGORY_TO_TRANSACTION_CATEGORY_LIST, data.result);
|
||||
} else {
|
||||
context.commit(SAVE_CATEGORY_IN_TRANSACTION_CATEGORY_LIST, data.result);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save category', error);
|
||||
|
||||
if (error.response && error.response.data && error.response.data.errorMessage) {
|
||||
reject({ error: error.response.data });
|
||||
} else if (!error.processed) {
|
||||
if (!category.id) {
|
||||
reject({ message: 'Unable to add category' });
|
||||
} else {
|
||||
reject({ message: 'Unable to save category' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function addCategories(context, { categories }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.addTransactionCategoryBatch({
|
||||
categories: categories
|
||||
@@ -17,7 +168,7 @@ function addTransactionCategoryBatch(context, { categories }) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE);
|
||||
context.commit(UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE, true);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
@@ -34,6 +185,172 @@ function addTransactionCategoryBatch(context, { categories }) {
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
addTransactionCategoryBatch,
|
||||
function changeCategoryDisplayOrder(context, { categoryId, from, to }) {
|
||||
const category = context.state.allTransactionCategoriesMap[categoryId];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!category) {
|
||||
reject({ message: 'Unable to move category' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!category.parentId || category.parentId === '0') {
|
||||
if (!context.state.allTransactionCategories[category.type] ||
|
||||
!context.state.allTransactionCategories[category.type][to]) {
|
||||
reject({ message: 'Unable to move category' });
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (!context.state.allTransactionCategoriesMap[category.parentId].subCategories ||
|
||||
!context.state.allTransactionCategoriesMap[category.parentId].subCategories[to]) {
|
||||
reject({ message: 'Unable to move category' });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE, true);
|
||||
context.commit(CHANGE_CATEGORY_DISPLAY_ORDER_IN_CATEGORY_LIST, {
|
||||
category: category,
|
||||
from: from,
|
||||
to: to
|
||||
});
|
||||
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
function updateCategoryDisplayOrders(context, { type, parentId }) {
|
||||
const newDisplayOrders = [];
|
||||
|
||||
let categoryList = null;
|
||||
|
||||
if (!parentId || parentId === '0') {
|
||||
categoryList = context.state.allTransactionCategories[type];
|
||||
} else if (context.state.allTransactionCategoriesMap[parentId]) {
|
||||
categoryList = context.state.allTransactionCategoriesMap[parentId].subCategories;
|
||||
}
|
||||
|
||||
if (categoryList) {
|
||||
for (let i = 0; i < categoryList.length; i++) {
|
||||
newDisplayOrders.push({
|
||||
id: categoryList[i].id,
|
||||
displayOrder: i + 1
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
services.moveTransactionCategory({
|
||||
newDisplayOrders: newDisplayOrders
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to move category' });
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_TRANSACTION_CATEGORY_LIST_INVALID_STATE, false);
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to save categories 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 category' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function hideCategory(context, { category, hidden }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.hideTransactionCategory({
|
||||
id: category.id,
|
||||
hidden: hidden
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
if (hidden) {
|
||||
reject({ message: 'Unable to hide this category' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this category' });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
context.commit(UPDATE_CATEGORY_VISIBILITY_IN_TRANSACTION_CATEGORY_LIST, {
|
||||
category: category,
|
||||
hidden: hidden
|
||||
});
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to change category 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 category' });
|
||||
} else {
|
||||
reject({ message: 'Unable to unhide this category' });
|
||||
}
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function deleteCategory(context, { category, beforeResolve }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
services.deleteTransactionCategory({
|
||||
id: category.id
|
||||
}).then(response => {
|
||||
const data = response.data;
|
||||
|
||||
if (!data || !data.success || !data.result) {
|
||||
reject({ message: 'Unable to delete this category' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (beforeResolve) {
|
||||
beforeResolve(() => {
|
||||
context.commit(REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST, category);
|
||||
});
|
||||
} else {
|
||||
context.commit(REMOVE_CATEGORY_FROM_TRANSACTION_CATEGORYLIST, category);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
logger.error('failed to delete category', 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 category' });
|
||||
} else {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
loadAllCategories,
|
||||
getCategory,
|
||||
saveCategory,
|
||||
addCategories,
|
||||
changeCategoryDisplayOrder,
|
||||
updateCategoryDisplayOrders,
|
||||
hideCategory,
|
||||
deleteCategory,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user