136 lines
3.9 KiB
C#
136 lines
3.9 KiB
C#
using BudgetApp.Data.Entities;
|
|
|
|
namespace BudgetApp.Components.Pages;
|
|
|
|
public partial class Accounts
|
|
{
|
|
private List<Account>? _accounts;
|
|
private AccountForm _formModel = new();
|
|
private string? _userId;
|
|
private int? _editingBalanceAccountId;
|
|
private decimal _editingBalanceValue;
|
|
private int? _editingAccountId;
|
|
private string _editName = string.Empty;
|
|
private string _editColor = "#1b6ec2";
|
|
private string _editCurrencyCode = "GBP";
|
|
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";
|
|
}
|
|
|
|
} |