@page "/accounts" @attribute [Authorize] @inject AccountService AccountService @inject AuthenticationStateProvider AuthStateProvider @inject AppState AppState @rendermode InteractiveServer Accounts

Accounts

Manage your financial accounts, balances and currencies.

Add Account
@foreach (var kvp in Account.CurrencySymbols) { }
@if (accounts is null) {

Loading...

} else if (accounts.Count == 0) {
No accounts yet. Add one above to get started.
} else {
@foreach (var account in accounts) {
@if (editingAccountId == account.Id) {
} else {
@account.Name
@account.CurrencyCode
@if (editingBalanceAccountId == account.Id) {
@account.GetSymbol()
} else {

@account.FormatAmount(account.CurrentBalance)

Click balance to adjust } }
}
} @if (showDeleteModal && pendingDeleteAccount is not null) { } @code { private List? accounts; private AccountForm formModel = new(); private string? userId; // Balance editing state private int? editingBalanceAccountId; private decimal editingBalanceValue; // Account details editing state private int? editingAccountId; private string editName = string.Empty; private string editColor = "#1b6ec2"; private string editCurrencyCode = "GBP"; // Delete confirmation state private bool showDeleteModal; private Account? pendingDeleteAccount; protected override async Task OnInitializedAsync() { var authState = await AuthStateProvider.GetAuthenticationStateAsync(); userId = authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value; await LoadAccounts(); } private async Task LoadAccounts() { if (userId is not null) { accounts = await AccountService.GetAccountsAsync(userId); } } private async Task AddAccount() { if (userId is null) return; var account = new Account { Name = formModel.Name, CurrentBalance = formModel.CurrentBalance, Color = formModel.Color, CurrencyCode = formModel.CurrencyCode, UserId = userId }; await AccountService.CreateAccountAsync(account); formModel = new AccountForm(); await LoadAccounts(); AppState.NotifyAccountsChanged(); } private void ConfirmDeleteAccount(Account account) { pendingDeleteAccount = account; showDeleteModal = true; } private async Task DeleteAccountConfirmed() { if (userId is null || pendingDeleteAccount is null) return; showDeleteModal = false; await AccountService.DeleteAccountAsync(pendingDeleteAccount.Id, userId); pendingDeleteAccount = null; await LoadAccounts(); AppState.NotifyAccountsChanged(); } private void CancelDeleteAccount() { showDeleteModal = false; pendingDeleteAccount = null; } private void StartBalanceEdit(Account account) { editingBalanceAccountId = account.Id; editingBalanceValue = account.CurrentBalance; } private void CancelBalanceEdit() { editingBalanceAccountId = null; } private async Task SaveBalance(int accountId) { if (userId is null) return; await AccountService.UpdateBalanceAsync(accountId, userId, editingBalanceValue); editingBalanceAccountId = null; await LoadAccounts(); AppState.NotifyAccountsChanged(); } private void StartAccountEdit(Account account) { editingAccountId = account.Id; editName = account.Name; editColor = account.Color; editCurrencyCode = account.CurrencyCode; } private void CancelAccountEdit() { editingAccountId = null; } private async Task SaveAccountDetails(int accountId) { if (userId is null) return; await AccountService.UpdateAccountDetailsAsync(accountId, userId, editName, editColor, editCurrencyCode); editingAccountId = null; await LoadAccounts(); AppState.NotifyAccountsChanged(); } private sealed class AccountForm { [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Account name is required")] [System.ComponentModel.DataAnnotations.MaxLength(100)] public string Name { get; set; } = string.Empty; public decimal CurrentBalance { get; set; } [System.ComponentModel.DataAnnotations.Required] [System.ComponentModel.DataAnnotations.MaxLength(7)] public string Color { get; set; } = "#1b6ec2"; [System.ComponentModel.DataAnnotations.Required] [System.ComponentModel.DataAnnotations.MaxLength(3)] public string CurrencyCode { get; set; } = "GBP"; } }