From 03a3d0a116e676c49967ae869ae7a6dfdec7b047 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 18 Nov 2026 09:48:18 +0100 Subject: [PATCH] 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`. --- .../Pages/SenderPage.razor | 79 +++++++++++++++++-- .../appsettings.Secrets.json | 2 +- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Pages/SenderPage.razor b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Pages/SenderPage.razor index ba3d4754..0d21227d 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Pages/SenderPage.razor +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Pages/SenderPage.razor @@ -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 ModelLocalizer @inject IStringLocalizer UiLocalizer +@inject PersistentComponentState PersistentComponentState @@ -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 CurrentGridData => (_activeTab == "completed" ? _completedEnvelopes : _activeEnvelopes) ?? Enumerable.Empty(); @@ -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? 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(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 EnvelopeTypes { get; set; } = []; + public List Envelopes { get; set; } = []; + } + private sealed record StatusFilterItem(EnvelopeStatus Value, string Text); } diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.Secrets.json b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.Secrets.json index 1a813471..66e155d4 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.Secrets.json +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.Secrets.json @@ -6,7 +6,7 @@ } }, "Notification": { - "ConsumerType": "RabbitMQ", + "ConsumerType": "API", "ApiUrl": "http://172.24.12.39:1414", "_ApiUrl": "https://localhost:7261", "SenderEmail": "demo.signflow@digitaldata.works",