Add state persistence to SenderPage and refactor logic

Introduced state persistence to `SenderPage.razor` using the
`PersistentComponentState` service, enabling the page to save
and restore its state across sessions. Added `IDisposable`
implementation for resource cleanup and refactored envelope
data handling into reusable methods for improved readability
and maintainability.

Modified `appsettings.Secrets.json` to switch the notification
consumer type from `RabbitMQ` to `API`.
This commit is contained in:
2026-11-18 09:48:18 +01:00
parent 3a21b3ba94
commit 03a3d0a116
2 changed files with 73 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
@page "/sender"
@rendermode InteractiveWebAssembly
@implements IDisposable
@using System.Text.Json
@using EnvelopeGenerator.Domain.Constants
@using EnvelopeGenerator.Server.Client.Models
@@ -28,6 +29,7 @@
@inject AppVersionService AppVersion
@inject IStringLocalizer<Model> ModelLocalizer
@inject IStringLocalizer<Resource> UiLocalizer
@inject PersistentComponentState PersistentComponentState
<link href="@AppVersion.GetVersionedUrl("css/envelope-viewer.css")" rel="stylesheet" />
<link href="@AppVersion.GetVersionedUrl("css/sender-page.css")" rel="stylesheet" />
@@ -631,6 +633,9 @@
// StableDefaults keys used to store/restore each tab's layout in UserPreferences.GridLayouts
private const string GridKeyActive = "sender.active-envelopes";
private const string GridKeyCompleted = "sender.completed-envelopes";
private const string InitialStateKey = "sender-page.initial-state.v1";
private PersistingComponentStateSubscription? _persistingSubscription;
private IEnumerable<EnvelopeDto> CurrentGridData =>
(_activeTab == "completed" ? _completedEnvelopes : _activeEnvelopes) ?? Enumerable.Empty<EnvelopeDto>();
@@ -639,6 +644,11 @@
protected override async Task OnInitializedAsync()
{
_persistingSubscription ??= PersistentComponentState.RegisterOnPersisting(PersistInitialStateAsync);
if (TryRestoreInitialState())
return;
var hasAccess = await AuthService.CheckSenderAccessAsync();
if (!hasAccess)
{
@@ -693,13 +703,8 @@
_isLoading = true;
try
{
_allEnvelopes = (await envelopesTask) ?? [];
// Split into active and completed based on effective status
// (history-aware: rejected envelopes should not stay in active tab).
var envelopes = _allEnvelopes.ToList();
_activeEnvelopes = envelopes.Where(e => GetEffectiveEnvelopeStatus(e).IsActive()).ToList();
_completedEnvelopes = envelopes.Where(e => GetEffectiveEnvelopeStatus(e).IsCompleted()).ToList();
var envelopes = await envelopesTask;
ApplyEnvelopeData(envelopes);
}
catch (Exception ex)
{
@@ -712,6 +717,57 @@
}
}
void ApplyEnvelopeData(IEnumerable<EnvelopeDto>? envelopes)
{
_allEnvelopes = envelopes ?? [];
// Split into active and completed based on effective status
// (history-aware: rejected envelopes should not stay in active tab).
var list = _allEnvelopes.ToList();
_activeEnvelopes = list.Where(e => GetEffectiveEnvelopeStatus(e).IsActive()).ToList();
_completedEnvelopes = list.Where(e => GetEffectiveEnvelopeStatus(e).IsCompleted()).ToList();
}
bool TryRestoreInitialState()
{
if (!PersistentComponentState.TryTakeFromJson<SenderPageInitialState>(InitialStateKey, out var state) || state is null)
return false;
_canUseReceiverEnvelopeUrl = state.CanUseReceiverEnvelopeUrl;
_appLogo = state.AppLogo;
_companyLogo = state.CompanyLogo;
_envelopeTypes = state.EnvelopeTypes;
ApplyEnvelopeData(state.Envelopes);
_errorMessage = null;
_isLoading = false;
return true;
}
Task PersistInitialStateAsync()
{
if (_allEnvelopes is null || !string.IsNullOrWhiteSpace(_errorMessage))
return Task.CompletedTask;
var state = new SenderPageInitialState
{
CanUseReceiverEnvelopeUrl = _canUseReceiverEnvelopeUrl,
AppLogo = _appLogo,
CompanyLogo = _companyLogo,
EnvelopeTypes = _envelopeTypes?.ToList() ?? [],
Envelopes = _allEnvelopes.ToList()
};
PersistentComponentState.PersistAsJson(InitialStateKey, state);
return Task.CompletedTask;
}
public void Dispose()
{
_persistingSubscription?.Dispose();
_persistingSubscription = null;
}
async Task RefreshEnvelopes()
{
await LoadEnvelopesAsync();
@@ -1377,5 +1433,14 @@
prop.SetValue(target, value);
}
private sealed class SenderPageInitialState
{
public bool CanUseReceiverEnvelopeUrl { get; set; }
public CustomImageDto? AppLogo { get; set; }
public CustomImageDto? CompanyLogo { get; set; }
public List<EnvelopeTypeDefaultsDto> EnvelopeTypes { get; set; } = [];
public List<EnvelopeDto> Envelopes { get; set; } = [];
}
private sealed record StatusFilterItem(EnvelopeStatus Value, string Text);
}