From ae3f096722c29f7253f1683a47a1b4941f2f178d Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Sat, 29 Mar 2025 18:44:39 +0100 Subject: [PATCH] Use NavigationLock on EditServer page to prevent user from navigating away before confirmation. --- .../Components/Pages/EditServer.razor | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ServerManagement/Components/Pages/EditServer.razor b/ServerManagement/Components/Pages/EditServer.razor index 53821a3..ed4e72b 100644 --- a/ServerManagement/Components/Pages/EditServer.razor +++ b/ServerManagement/Components/Pages/EditServer.razor @@ -1,9 +1,17 @@ @page "/servers/{id:int}" @* Route constraints: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0#route-constraints *@ -@attribute [ExcludeFromInteractiveRouting] +@* Note that this attribute now needs to be removed, because Routing is globally interactive. +The navigation manager only will respond correctly if all its behaviours are the same. *@ +@* @attribute [ExcludeFromInteractiveRouting] *@ @inject NavigationManager NavigationManager +@inject IJSRuntime JSRuntime + + +

Edit server


@@ -68,4 +76,13 @@ // An exception is raised when debugging from VS Code, but not when using dotnet watch. NavigationManager.NavigateTo("/servers"); } + + private async Task OnBeforeInternalNavigation(LocationChangingContext context) + { + var isConfirmed = await JSRuntime.InvokeAsync("confirm", "Are you sure you want to leave this page?"); + if (!isConfirmed) + { + context.PreventNavigation(); + } + } } \ No newline at end of file