support hiding/unhiding/deleting sub-account in account list page

This commit is contained in:
MaysWind
2025-04-13 20:46:06 +08:00
parent 68b08c1e8a
commit df31be61e8
15 changed files with 329 additions and 32 deletions
+77 -1
View File
@@ -295,6 +295,46 @@ export const useAccountsStore = defineStore('accounts', () => {
}
}
function removeSubAccountFromAccountList(subAccount: Account): void {
for (let i = 0; i < allAccounts.value.length; i++) {
if (allAccounts.value[i].type !== AccountType.MultiSubAccounts.type || !allAccounts.value[i].subAccounts) {
continue;
}
const subAccounts = allAccounts.value[i].subAccounts as Account[];
for (let j = 0; j < subAccounts.length; j++) {
if (subAccounts[j].id === subAccount.id) {
subAccounts.splice(j, 1);
break;
}
}
}
if (allAccountsMap.value[subAccount.id]) {
delete allAccountsMap.value[subAccount.id];
}
if (allCategorizedAccountsMap.value[subAccount.category]) {
const accountList = allCategorizedAccountsMap.value[subAccount.category].accounts;
for (let i = 0; i < accountList.length; i++) {
if (accountList[i].type !== AccountType.MultiSubAccounts.type || !accountList[i].subAccounts) {
continue;
}
const subAccounts = accountList[i].subAccounts as Account[];
for (let j = 0; j < subAccounts.length; j++) {
if (subAccounts[j].id === subAccount.id) {
subAccounts.splice(j, 1);
break;
}
}
}
}
}
function updateAccountListInvalidState(invalidState: boolean): void {
accountListStateInvalid.value = invalidState;
}
@@ -997,6 +1037,41 @@ export const useAccountsStore = defineStore('accounts', () => {
});
}
function deleteSubAccount({ subAccount, beforeResolve }: { subAccount: Account, beforeResolve?: BeforeResolveFunction }): Promise<boolean> {
return new Promise((resolve, reject) => {
services.deleteSubAccount({
id: subAccount.id
}).then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
reject({ message: 'Unable to delete this sub-account' });
return;
}
if (beforeResolve) {
beforeResolve(() => {
removeSubAccountFromAccountList(subAccount);
});
} else {
removeSubAccountFromAccountList(subAccount);
}
resolve(data.result);
}).catch(error => {
logger.error('failed to delete sub-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 sub-account' });
} else {
reject(error);
}
});
});
}
return {
// states
allAccounts,
@@ -1029,6 +1104,7 @@ export const useAccountsStore = defineStore('accounts', () => {
changeAccountDisplayOrder,
updateAccountDisplayOrders,
hideAccount,
deleteAccount
deleteAccount,
deleteSubAccount
}
});