From 05edc9f247e698c52b39897d68ab7f4e87911a2c Mon Sep 17 00:00:00 2001 From: dyatlov-a Date: Wed, 25 Sep 2024 13:03:28 +0400 Subject: [PATCH] Refactoring client routing --- .../Extensions/JsFunctions.cs | 10 ++--- .../AssessmentSessionHistoryPage.razor | 4 +- .../AssessmentSessionPage.razor | 4 +- .../Features/Auth/RedirectToLogin.razor | 4 +- .../Features/Constructor/BotSelector.razor | 6 +-- .../Constructor/Stages/StagesPage.razor | 17 ++++--- .../Features/Dashboard/DashboardPage.razor | 4 +- .../Features/Layouts/CultureSelector.razor | 6 +-- .../Features/_Imports.razor | 1 + .../ClientCore => Routing}/NavRoute.cs | 2 +- .../ClientCore => Routing}/NavRouter.cs | 45 +++++++++---------- .../Routing/RoutingType.cs | 8 ++++ .../Services/ServiceCollectionExtensions.cs | 1 + 13 files changed, 60 insertions(+), 52 deletions(-) rename src/Inc.TeamAssistant.WebUI/{Services/ClientCore => Routing}/NavRoute.cs (90%) rename src/Inc.TeamAssistant.WebUI/{Services/ClientCore => Routing}/NavRouter.cs (74%) create mode 100644 src/Inc.TeamAssistant.WebUI/Routing/RoutingType.cs diff --git a/src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs b/src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs index 7e9782ef..f0a6961d 100644 --- a/src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs +++ b/src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs @@ -1,5 +1,3 @@ -using Inc.TeamAssistant.WebUI.Services.ClientCore; - namespace Inc.TeamAssistant.WebUI.Extensions; public static class JsFunctions @@ -9,15 +7,15 @@ public static IJsFunction GetTimezone() return new JsFunction("window.browserJsFunctions.getTimezone", postAction: null, args: null); } - public static IJsFunction ChangeUrl(NavRoute route, Action onChanged) + public static IJsFunction ChangeUrl(string url, Action onChanged) { - ArgumentNullException.ThrowIfNull(route); + ArgumentNullException.ThrowIfNull(url); ArgumentNullException.ThrowIfNull(onChanged); return new JsFunction( "window.browserJsFunctions.changeUrl", - () => onChanged(route), - route.ToString()); + () => onChanged(url), + url); } private sealed record JsFunction : IJsFunction diff --git a/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionHistoryPage.razor b/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionHistoryPage.razor index 1f83038e..87e8e34a 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionHistoryPage.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionHistoryPage.razor @@ -80,11 +80,11 @@ private async Task OnViewChanged(AssessmentType assessmentType) { - var route = NavRouter.CreateRoute($"assessment-history/{TeamId:N}/{Date}/{assessmentType}".ToLowerInvariant()); + var routeSegment = $"assessment-history/{TeamId:N}/{Date}/{assessmentType}".ToLowerInvariant(); _currentView = assessmentType; - await NavRouter.ChangeCurrentRoute(route); + await NavRouter.MoveToRoute(routeSegment, RoutingType.Browser); } private async Task Load() diff --git a/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionPage.razor b/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionPage.razor index 199a9ff3..5246cb55 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionPage.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/AssessmentSession/AssessmentSessionPage.razor @@ -145,11 +145,11 @@ private async Task OnViewChanged(AssessmentType assessmentType) { - var route = NavRouter.CreateRoute($"assessment-session/{Id:N}/{assessmentType}".ToLowerInvariant()); + var routeSegment = $"assessment-session/{Id:N}/{assessmentType}".ToLowerInvariant(); _currentView = assessmentType; - await NavRouter.ChangeCurrentRoute(route); + await NavRouter.MoveToRoute(routeSegment, RoutingType.Browser); } public async ValueTask DisposeAsync() diff --git a/src/Inc.TeamAssistant.WebUI/Features/Auth/RedirectToLogin.razor b/src/Inc.TeamAssistant.WebUI/Features/Auth/RedirectToLogin.razor index 207add56..39e6100c 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/Auth/RedirectToLogin.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/Auth/RedirectToLogin.razor @@ -1,8 +1,8 @@ @inject NavRouter NavRouter @code { - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { - NavRouter.NavigateToRouteSegment($"login?returnUrl={NavRouter.CurrentUrl}"); + await NavRouter.MoveToRoute($"login?returnUrl={NavRouter.CurrentUrl}"); } } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Features/Constructor/BotSelector.razor b/src/Inc.TeamAssistant.WebUI/Features/Constructor/BotSelector.razor index 62355b2f..673efd0f 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/Constructor/BotSelector.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/Constructor/BotSelector.razor @@ -73,16 +73,16 @@ }); } - private void MoveToAdd() => MoveToEdit(botId: null); + private Task MoveToAdd() => MoveToEdit(botId: null); - private void MoveToEdit(Guid? botId) + private async Task MoveToEdit(Guid? botId) { var stage = Stage.CheckBot.ToString().ToLowerInvariant(); var routeSegment = botId.HasValue ? $"constructor/{botId.Value:N}/{stage}" : $"constructor/{stage}"; - NavRouter.NavigateToRouteSegment(routeSegment); + await NavRouter.MoveToRoute(routeSegment); } private void MoveToRemove(BotDto bot) diff --git a/src/Inc.TeamAssistant.WebUI/Features/Constructor/Stages/StagesPage.razor b/src/Inc.TeamAssistant.WebUI/Features/Constructor/Stages/StagesPage.razor index c8bebe43..8a71b183 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/Constructor/Stages/StagesPage.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/Constructor/Stages/StagesPage.razor @@ -111,12 +111,12 @@ { if (stage.HasValue) { - var route = LinkFactory(stage.Value); + var routeSegment = CreateRouteSegment(stage.Value); _currentState = stage.Value; StateHasChanged(); - await NavRouter.ChangeCurrentRoute(route); + await NavRouter.MoveToRoute(routeSegment, RoutingType.Browser); return; } @@ -124,15 +124,20 @@ notificationsService.Publish(Notification.Info(Resources[Messages.GUI_DataRefreshed])); - NavRouter.NavigateToRouteSegment("constructor"); + await NavRouter.MoveToRoute("constructor"); } private NavRoute LinkFactory(Stage stage) { - var routeSegment = _stagesState.Id.HasValue - ? $"constructor/{_stagesState.Id.Value:N}/{stage}" - : $"constructor/{stage}"; + var routeSegment = CreateRouteSegment(stage); return NavRouter.CreateRoute(routeSegment.ToLowerInvariant()); } + + private string CreateRouteSegment(Stage stage) + { + return _stagesState.Id.HasValue + ? $"constructor/{_stagesState.Id.Value:N}/{stage}" + : $"constructor/{stage}"; + } } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Features/Dashboard/DashboardPage.razor b/src/Inc.TeamAssistant.WebUI/Features/Dashboard/DashboardPage.razor index c0c93e29..93194ca8 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/Dashboard/DashboardPage.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/Dashboard/DashboardPage.razor @@ -116,7 +116,7 @@ private Type ToWidgetType(string type) => _widgetsLookup[type]; - private void ChangeTeamContext((Guid? BotId, Guid? TeamId) botContext) + private async Task ChangeTeamContext((Guid? BotId, Guid? TeamId) botContext) { var routeSegment = (botContext.BotId.HasValue, botContext.TeamId.HasValue) switch { @@ -128,6 +128,6 @@ _botId = botContext.BotId; _teamId = botContext.TeamId; - NavRouter.NavigateToRouteSegment(routeSegment); + await NavRouter.MoveToRoute(routeSegment); } } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Features/Layouts/CultureSelector.razor b/src/Inc.TeamAssistant.WebUI/Features/Layouts/CultureSelector.razor index 304749ab..690bce2f 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/Layouts/CultureSelector.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/Layouts/CultureSelector.razor @@ -1,5 +1,3 @@ -@using Inc.TeamAssistant.Primitives.Languages - @implements IDisposable @inject NavRouter NavRouter @@ -28,7 +26,7 @@ private void SetLanguages() { - var routeWithoutLanguage = NavRouter.GetRouteWithoutLanguage(); + var routeWithoutLanguage = NavRouter.GetRouteSegment(); _languages = LanguageSettings.LanguageIds .Select(l => (l.Value, $"/{l.Value}/{routeWithoutLanguage}")) @@ -37,7 +35,7 @@ StateHasChanged(); } - private void MoveTo(string path) => NavRouter.NavigateToPath(path); + private Task MoveTo(string path) => NavRouter.MoveToRoute(path, RoutingType.Server); public void Dispose() => _routerScope?.Dispose(); } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Features/_Imports.razor b/src/Inc.TeamAssistant.WebUI/Features/_Imports.razor index e8ba9412..8a2247d5 100644 --- a/src/Inc.TeamAssistant.WebUI/Features/_Imports.razor +++ b/src/Inc.TeamAssistant.WebUI/Features/_Imports.razor @@ -15,6 +15,7 @@ @using Inc.TeamAssistant.WebUI.Features.Meta @using Inc.TeamAssistant.WebUI.Features.Dialogs @using Inc.TeamAssistant.WebUI.Services.ClientCore +@using Inc.TeamAssistant.WebUI.Routing @using Inc.TeamAssistant.WebUI.Extensions @using Inc.TeamAssistant.WebUI.Features.Icons @using Inc.TeamAssistant.WebUI.Features.Auth diff --git a/src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRoute.cs b/src/Inc.TeamAssistant.WebUI/Routing/NavRoute.cs similarity index 90% rename from src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRoute.cs rename to src/Inc.TeamAssistant.WebUI/Routing/NavRoute.cs index 7a900cd8..9fdf64e7 100644 --- a/src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRoute.cs +++ b/src/Inc.TeamAssistant.WebUI/Routing/NavRoute.cs @@ -1,6 +1,6 @@ using Inc.TeamAssistant.Primitives.Languages; -namespace Inc.TeamAssistant.WebUI.Services.ClientCore; +namespace Inc.TeamAssistant.WebUI.Routing; public sealed record NavRoute( LanguageId? LanguageId, diff --git a/src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRouter.cs b/src/Inc.TeamAssistant.WebUI/Routing/NavRouter.cs similarity index 74% rename from src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRouter.cs rename to src/Inc.TeamAssistant.WebUI/Routing/NavRouter.cs index fe170631..abdf6fcb 100644 --- a/src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRouter.cs +++ b/src/Inc.TeamAssistant.WebUI/Routing/NavRouter.cs @@ -1,11 +1,12 @@ using Inc.TeamAssistant.Primitives.Languages; using Inc.TeamAssistant.WebUI.Contracts; using Inc.TeamAssistant.WebUI.Extensions; +using Inc.TeamAssistant.WebUI.Services.ClientCore; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; using Microsoft.JSInterop; -namespace Inc.TeamAssistant.WebUI.Services.ClientCore; +namespace Inc.TeamAssistant.WebUI.Routing; public sealed class NavRouter : IDisposable { @@ -28,7 +29,7 @@ public NavRouter( CurrentUrl = _navigationManager.Uri; } - public string GetRouteWithoutLanguage() + public string GetRouteSegment() { var routeWithoutLanguage = _navigationManager.ToBaseRelativePath(CurrentUrl); @@ -54,42 +55,38 @@ public IDisposable OnRouteChanged(Action onRouteChanged) return new RouterScope(() => _onRouteChanged -= onRouteChanged); } - public void NavigateToPath(string uri) + public async Task MoveToRoute(string routeSegment, RoutingType type = RoutingType.Client) { - ArgumentException.ThrowIfNullOrWhiteSpace(uri); - - _navigationManager.NavigateTo(uri, forceLoad: true); - } - - public void NavigateToRouteSegment(string routeSegment) - { - ArgumentException.ThrowIfNullOrWhiteSpace(routeSegment); - var route = CreateRoute(routeSegment); - NavigateToRoute(route); - } - - public void NavigateToRoute(NavRoute route) - { - ArgumentNullException.ThrowIfNull(route); - - _navigationManager.NavigateTo(route); + switch (type) + { + case RoutingType.Browser: + await ChangeBrowserPath(route); + break; + case RoutingType.Server: + _navigationManager.NavigateTo(routeSegment, forceLoad: true); + break; + case RoutingType.Client: + default: + _navigationManager.NavigateTo(route); + break; + } } - public async Task ChangeCurrentRoute(NavRoute route) + private async Task ChangeBrowserPath(NavRoute route) { ArgumentNullException.ThrowIfNull(route); var jsRuntime = _serviceProvider.GetRequiredService(); var jsFunction = JsFunctions.ChangeUrl( route, - r => ChangeRouteCore(_navigationManager.ToAbsoluteUri(r).ToString())); + r => ChangeCurrentUrl(_navigationManager.ToAbsoluteUri(r).ToString())); await jsRuntime.Execute(jsFunction); } - private void ChangeRouteCore(string uri) + private void ChangeCurrentUrl(string uri) { ArgumentNullException.ThrowIfNull(uri); @@ -97,7 +94,7 @@ private void ChangeRouteCore(string uri) _onRouteChanged?.Invoke(); } - private void OnLocationChanged(object? sender, LocationChangedEventArgs e) => ChangeRouteCore(e.Location); + private void OnLocationChanged(object? sender, LocationChangedEventArgs e) => ChangeCurrentUrl(e.Location); public void Dispose() => _navigationManager.LocationChanged -= OnLocationChanged; } \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Routing/RoutingType.cs b/src/Inc.TeamAssistant.WebUI/Routing/RoutingType.cs new file mode 100644 index 00000000..920ec6c0 --- /dev/null +++ b/src/Inc.TeamAssistant.WebUI/Routing/RoutingType.cs @@ -0,0 +1,8 @@ +namespace Inc.TeamAssistant.WebUI.Routing; + +public enum RoutingType +{ + Client = 1, + Browser = 2, + Server = 3 +} \ No newline at end of file diff --git a/src/Inc.TeamAssistant.WebUI/Services/ServiceCollectionExtensions.cs b/src/Inc.TeamAssistant.WebUI/Services/ServiceCollectionExtensions.cs index ccf2c301..036ac5c5 100644 --- a/src/Inc.TeamAssistant.WebUI/Services/ServiceCollectionExtensions.cs +++ b/src/Inc.TeamAssistant.WebUI/Services/ServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using Inc.TeamAssistant.WebUI.Features.Constructor.Stages.Stage2; using Inc.TeamAssistant.WebUI.Features.Constructor.Stages.Stage3; using Inc.TeamAssistant.WebUI.Features.Notifications; +using Inc.TeamAssistant.WebUI.Routing; using Inc.TeamAssistant.WebUI.Services.ClientCore; using Inc.TeamAssistant.WebUI.Services.Clients; using Microsoft.AspNetCore.Components.Authorization;