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 i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) { const primaryCategory = categories[i];
if (categories[i].subCategories[j].id === categoryId) {
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; return true;
} }
} }
@@ -188,27 +200,49 @@ export function getFirstAvailableCategoryId(categories) {
} }
for (let i = 0; i < categories.length; i++) { for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].subCategories.length; j++) { const primaryCategory = categories[i];
return categories[i].subCategories[j].id;
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) { if (!categories || !categories.length) {
return ''; return '';
} }
for (let i = 0; i < categories.length; i++) { for (let i = 0; i < categories.length; i++) {
if (categories[i].id !== categoryId) { const primaryCategory = categories[i];
if (primaryCategory.hidden || primaryCategory.id !== categoryId) {
continue; continue;
} }
if (categories[i].subCategories.length <= 0) { for (let j = 0; j < primaryCategory.subCategories.length; j++) {
return ''; const secondaryCategory = primaryCategory.subCategories[j];
if (secondaryCategory.hidden) {
continue;
}
return secondaryCategory.id;
} }
return categories[i].subCategories[0].id; return '';
} }
return ''; return '';
+4 -4
View File
@@ -11,7 +11,7 @@ import {
categoryTypeToTransactionType, categoryTypeToTransactionType,
isSubCategoryIdAvailable, isSubCategoryIdAvailable,
getFirstAvailableCategoryId, getFirstAvailableCategoryId,
getFirstAvaiableSubCategoryId getFirstAvailableSubCategoryId
} from './category.js'; } from './category.js';
function getDisplayAmount(amount, currency, hideAmount, formatAmountWithCurrencyFunc) { function getDisplayAmount(amount, currency, hideAmount, formatAmountWithCurrencyFunc) {
@@ -38,7 +38,7 @@ export function setTransactionModelByTransaction(transaction, transaction2, allC
if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId)) { if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Expense], options.categoryId)) {
transaction.expenseCategory = options.categoryId; transaction.expenseCategory = options.categoryId;
} else { } 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)) { if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Income], options.categoryId)) {
transaction.incomeCategory = options.categoryId; transaction.incomeCategory = options.categoryId;
} else { } 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)) { if (isSubCategoryIdAvailable(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId)) {
transaction.transferCategory = options.categoryId; transaction.transferCategory = options.categoryId;
} else { } else {
transaction.transferCategory = getFirstAvaiableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId); transaction.transferCategory = getFirstAvailableSubCategoryId(allCategories[categoryConstants.allCategoryTypes.Transfer], options.categoryId);
} }
} }