auto choose the first non-hidden category when opening transaction create page / dialog

This commit is contained in:
MaysWind
2024-07-23 00:18:44 +08:00
parent 4bf2e94a9d
commit fcca77bca5
2 changed files with 47 additions and 13 deletions
+43 -9
View File
@@ -172,8 +172,20 @@ export function isSubCategoryIdAvailable(categories, categoryId) {
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
if (categories[i].subCategories[j].id === categoryId) {
const primaryCategory = categories[i];
if (primaryCategory.hidden) {
continue;
}
for (let j = 0; j < primaryCategory.subCategories.length; j++) {
const secondaryCategory = primaryCategory.subCategories[j];
if (secondaryCategory.hidden) {
continue;
}
if (secondaryCategory.id === categoryId) {
return true;
}
}
@@ -188,27 +200,49 @@ export function getFirstAvailableCategoryId(categories) {
}
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) {
return categories[i].subCategories[j].id;
const primaryCategory = categories[i];
if (primaryCategory.hidden) {
continue;
}
for (let j = 0; j < primaryCategory.subCategories.length; j++) {
const secondaryCategory = primaryCategory.subCategories[j];
if (secondaryCategory.hidden) {
continue;
}
return secondaryCategory.id;
}
}
return '';
}
export function getFirstAvaiableSubCategoryId(categories, categoryId) {
export function getFirstAvailableSubCategoryId(categories, categoryId) {
if (!categories || !categories.length) {
return '';
}
for (let i = 0; i < categories.length; i++) {
if (categories[i].id !== categoryId) {
const primaryCategory = categories[i];
if (primaryCategory.hidden || primaryCategory.id !== categoryId) {
continue;
}
if (categories[i].subCategories.length <= 0) {
return '';
for (let j = 0; j < primaryCategory.subCategories.length; j++) {
const secondaryCategory = primaryCategory.subCategories[j];
if (secondaryCategory.hidden) {
continue;
}
return secondaryCategory.id;
}
return categories[i].subCategories[0].id;
return '';
}
return '';
+4 -4
View File
@@ -11,7 +11,7 @@ import {
categoryTypeToTransactionType,
isSubCategoryIdAvailable,
getFirstAvailableCategoryId,
getFirstAvaiableSubCategoryId
getFirstAvailableSubCategoryId
} from './category.js';
function getDisplayAmount(amount, currency, hideAmount, formatAmountWithCurrencyFunc) {
@@ -38,7 +38,7 @@ export function setTransactionModelByTransaction(transaction, transaction2, allC
if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId)) {
transaction.expenseCategory = options.categoryId;
} else {
transaction.expenseCategory = getFirstAvaiableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId);
transaction.expenseCategory = getFirstAvailableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId);
}
}
@@ -53,7 +53,7 @@ export function setTransactionModelByTransaction(transaction, transaction2, allC
if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Income], options.categoryId)) {
transaction.incomeCategory = options.categoryId;
} else {
transaction.incomeCategory = getFirstAvaiableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Income], options.categoryId);
transaction.incomeCategory = getFirstAvailableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Income], options.categoryId);
}
}
@@ -68,7 +68,7 @@ export function setTransactionModelByTransaction(transaction, transaction2, allC
if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId)) {
transaction.transferCategory = options.categoryId;
} else {
transaction.transferCategory = getFirstAvaiableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId);
transaction.transferCategory = getFirstAvailableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId);
}
}