Skip to content

Commit

Permalink
Change client routing
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 24, 2024
1 parent 9fb9701 commit ee4a53f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/Inc.TeamAssistant.WebUI/Extensions/JsFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public static IJsFunction<int> GetTimezone()
return new JsFunction<int>("window.browserJsFunctions.getTimezone", postAction: null, args: null);
}

public static IJsFunction<dynamic> ChangeUrl(this NavRouter navRouter, NavRoute route)
public static IJsFunction<dynamic> ChangeUrl(NavRoute route, Action<NavRoute> onChanged)
{
ArgumentNullException.ThrowIfNull(navRouter);
ArgumentNullException.ThrowIfNull(route);

ArgumentNullException.ThrowIfNull(onChanged);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@inject ResourcesManager Resources
@inject NavRouter NavRouter
@inject RequestProcessor RequestProcessor
@inject IJSRuntime JsRuntime

<MetaDataModule
WebsiteSection="WebsiteSection.Assessment"
Expand Down Expand Up @@ -81,11 +80,11 @@

private async Task OnViewChanged(AssessmentType assessmentType)
{
var url = NavRouter.CreateRoute($"assessment-history/{TeamId:N}/{Date}/{assessmentType}".ToLowerInvariant());
var route = NavRouter.CreateRoute($"assessment-history/{TeamId:N}/{Date}/{assessmentType}".ToLowerInvariant());

_currentView = assessmentType;
await JsRuntime.Execute(NavRouter.ChangeUrl(url));

await NavRouter.ChangeCurrentRoute(route);
}

private async Task Load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@inject ResourcesManager Resources
@inject NavRouter NavRouter
@inject RequestProcessor RequestProcessor
@inject IJSRuntime JsRuntime

<MetaDataModule
WebsiteSection="WebsiteSection.Assessment"
Expand Down Expand Up @@ -146,11 +145,11 @@

private async Task OnViewChanged(AssessmentType assessmentType)
{
var url = NavRouter.CreateRoute($"assessment-session/{Id:N}/{assessmentType}".ToLowerInvariant());
var route = NavRouter.CreateRoute($"assessment-session/{Id:N}/{assessmentType}".ToLowerInvariant());

_currentView = assessmentType;
await JsRuntime.Execute(NavRouter.ChangeUrl(url));

await NavRouter.ChangeCurrentRoute(route);
}

public async ValueTask DisposeAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@inject ResourcesManager Resources
@inject NavRouter NavRouter
@inject RequestProcessor RequestProcessor
@inject IJSRuntime JsRuntime

<MetaDataModule
WebsiteSection="WebsiteSection.Constructor"
Expand Down Expand Up @@ -116,8 +115,8 @@

_currentState = stage.Value;
StateHasChanged();
await JsRuntime.Execute(NavRouter.ChangeUrl(route));

await NavRouter.ChangeCurrentRoute(route);
return;
}

Expand Down
32 changes: 22 additions & 10 deletions src/Inc.TeamAssistant.WebUI/Services/ClientCore/NavRouter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
using Inc.TeamAssistant.Primitives.Languages;
using Inc.TeamAssistant.WebUI.Contracts;
using Inc.TeamAssistant.WebUI.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.JSInterop;

namespace Inc.TeamAssistant.WebUI.Services.ClientCore;

public sealed class NavRouter : IDisposable
{
private readonly IRenderContext _renderContext;
private readonly NavigationManager _navigationManager;
private readonly IServiceProvider _serviceProvider;
private Action? _onRouteChanged;

public string CurrentUrl { get; private set; }

public NavRouter(IRenderContext renderContext, NavigationManager navigationManager)
public NavRouter(
IRenderContext renderContext,
NavigationManager navigationManager,
IServiceProvider serviceProvider)
{
_renderContext = renderContext ?? throw new ArgumentNullException(nameof(renderContext));
_navigationManager = navigationManager ?? throw new ArgumentNullException(nameof(navigationManager));
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_navigationManager.LocationChanged += OnLocationChanged;
CurrentUrl = _navigationManager.Uri;
}
Expand All @@ -39,6 +46,13 @@ public NavRoute CreateRoute(string? routeSegment)

return new NavRoute(selectedLanguage, link);
}

public IDisposable OnRouteChanged(Action onRouteChanged)
{
_onRouteChanged += onRouteChanged;

return new RouterScope(() => _onRouteChanged -= onRouteChanged);
}

public void NavigateToPath(string uri)
{
Expand All @@ -62,19 +76,17 @@ public void NavigateToRoute(NavRoute route)

_navigationManager.NavigateTo(route);
}

public IDisposable OnRouteChanged(Action onRouteChanged)
{
_onRouteChanged += onRouteChanged;

return new RouterScope(() => _onRouteChanged -= onRouteChanged);
}

public void ChangeRoute(NavRoute route)
public async Task ChangeCurrentRoute(NavRoute route)
{
ArgumentNullException.ThrowIfNull(route);

var jsRuntime = _serviceProvider.GetRequiredService<IJSRuntime>();
var jsFunction = JsFunctions.ChangeUrl(
route,
r => ChangeRouteCore(_navigationManager.ToAbsoluteUri(r).ToString()));

ChangeRouteCore(_navigationManager.ToAbsoluteUri(route).ToString());
await jsRuntime.Execute(jsFunction);
}

private void ChangeRouteCore(string uri)
Expand Down

0 comments on commit ee4a53f

Please sign in to comment.