Fix AliasVault.Client issue when creating new item when already on the item AddEdit page

This commit is contained in:
Leendert de Borst
2026-01-27 22:21:07 +01:00
parent c9fbff6a43
commit cb90dc1199
3 changed files with 76 additions and 22 deletions

View File

@@ -282,6 +282,12 @@
QuickCreateStateService.FolderId = GetCurrentFolderIdFromUrl();
NavigationManager.NavigateTo("/items/create");
// Notify subscribers (e.g. AddEdit page) that the state has changed. This handles
// the case where the user is already on the AddEdit page and the navigation above
// does not trigger a re-initialization.
QuickCreateStateService.NotifyStateChanged();
ClosePopup();
}

View File

@@ -457,6 +457,7 @@ else
/// <inheritdoc />
async ValueTask IAsyncDisposable.DisposeAsync()
{
QuickCreateStateService.OnChange -= OnQuickCreateStateChanged;
await KeyboardShortcutService.UnregisterShortcutAsync("gc");
if (Module is not null)
{
@@ -502,34 +503,17 @@ else
{
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/modules/newIdentityWidget.js");
// Subscribe to quick create state changes so we can reinitialize
// when the user triggers the widget while already on this page.
QuickCreateStateService.OnChange += OnQuickCreateStateChanged;
if (EditMode)
{
await LoadExistingCredential();
}
else
{
CreateNewCredential();
// Use the state service to pre-fill form data
if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceName))
{
Obj.ServiceName = QuickCreateStateService.ServiceName;
}
if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceUrl))
{
Obj.SetFieldValue(FieldKey.LoginUrl, QuickCreateStateService.ServiceUrl);
}
if (!string.IsNullOrEmpty(QuickCreateStateService.ItemType))
{
await HandleItemTypeChange(QuickCreateStateService.ItemType);
}
if (QuickCreateStateService.FolderId.HasValue)
{
Obj.FolderId = QuickCreateStateService.FolderId;
}
// Clear the state after using it
QuickCreateStateService.ClearState();
await ApplyQuickCreateState();
}
Loading = false;
@@ -542,6 +526,60 @@ else
}
}
/// <summary>
/// Handles the quick create state change event when the user triggers the
/// CreateNewIdentityWidget while already on the AddEdit page.
/// </summary>
private async void OnQuickCreateStateChanged()
{
await InvokeAsync(async () =>
{
// Ensure we are in create mode since quick create always creates new items.
EditMode = false;
await ApplyQuickCreateState();
Loading = false;
StateHasChanged();
await JsInteropService.FocusElementById("service-name");
});
}
/// <summary>
/// Creates a new credential and applies any prefilled data from the QuickCreateStateService.
/// </summary>
private async Task ApplyQuickCreateState()
{
CreateNewCredential();
// Reset manually added fields tracking for the fresh form.
ManuallyAddedFields = [];
InitiallyVisibleFields = [];
// Use the state service to pre-fill form data
if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceName))
{
Obj.ServiceName = QuickCreateStateService.ServiceName;
}
if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceUrl))
{
Obj.SetFieldValue(FieldKey.LoginUrl, QuickCreateStateService.ServiceUrl);
}
if (!string.IsNullOrEmpty(QuickCreateStateService.ItemType))
{
await HandleItemTypeChange(QuickCreateStateService.ItemType);
}
if (QuickCreateStateService.FolderId.HasValue)
{
Obj.FolderId = QuickCreateStateService.FolderId;
}
// Clear the state after using it
QuickCreateStateService.ClearState();
}
/// <summary>
/// Checks if the current item type has login-related fields.
/// </summary>

View File

@@ -12,6 +12,11 @@ namespace AliasVault.Client.Services;
/// </summary>
public class QuickCreateStateService
{
/// <summary>
/// Event that is raised when the state has been updated and the consumer should reinitialize.
/// </summary>
public event Action? OnChange;
/// <summary>
/// Gets or sets the service name from quick create.
/// </summary>
@@ -32,6 +37,11 @@ public class QuickCreateStateService
/// </summary>
public Guid? FolderId { get; set; }
/// <summary>
/// Notifies subscribers that the state has changed.
/// </summary>
public void NotifyStateChanged() => OnChange?.Invoke();
/// <summary>
/// Clears the stored state.
/// </summary>