Skip to content

Commit

Permalink
Refactoring client routing
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 25, 2024
1 parent a869875 commit 05edc9f
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 52 deletions.
10 changes: 4 additions & 6 deletions src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Inc.TeamAssistant.WebUI.Services.ClientCore;

namespace Inc.TeamAssistant.WebUI.Extensions;

public static class JsFunctions
Expand All @@ -9,15 +7,15 @@ public static IJsFunction<int> GetTimezone()
return new JsFunction<int>("window.browserJsFunctions.getTimezone", postAction: null, args: null);
}

public static IJsFunction<dynamic> ChangeUrl(NavRoute route, Action<NavRoute> onChanged)
public static IJsFunction<dynamic> ChangeUrl(string url, Action<string> onChanged)
{
ArgumentNullException.ThrowIfNull(route);
ArgumentNullException.ThrowIfNull(url);
ArgumentNullException.ThrowIfNull(onChanged);

return new JsFunction<dynamic>(
"window.browserJsFunctions.changeUrl",
() => onChanged(route),
route.ToString());
() => onChanged(url),
url);
}

private sealed record JsFunction<TResult> : IJsFunction<TResult>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,33 @@
{
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;
}

var notificationsService = ServiceProvider.GetRequiredService<INotificationsService>();

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}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -128,6 +128,6 @@
_botId = botContext.BotId;
_teamId = botContext.TeamId;

NavRouter.NavigateToRouteSegment(routeSegment);
await NavRouter.MoveToRoute(routeSegment);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@using Inc.TeamAssistant.Primitives.Languages

@implements IDisposable

@inject NavRouter NavRouter
Expand Down Expand Up @@ -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}"))
Expand All @@ -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();
}
1 change: 1 addition & 0 deletions src/Inc.TeamAssistant.WebUI/Features/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -28,7 +29,7 @@ public NavRouter(
CurrentUrl = _navigationManager.Uri;
}

public string GetRouteWithoutLanguage()
public string GetRouteSegment()
{
var routeWithoutLanguage = _navigationManager.ToBaseRelativePath(CurrentUrl);

Expand All @@ -54,50 +55,46 @@ 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<IJSRuntime>();
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);

CurrentUrl = 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;
}
8 changes: 8 additions & 0 deletions src/Inc.TeamAssistant.WebUI/Routing/RoutingType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Inc.TeamAssistant.WebUI.Routing;

public enum RoutingType
{
Client = 1,
Browser = 2,
Server = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 05edc9f

Please sign in to comment.