From 18e02792ec372daf00703e42dfc268a3edaa21af Mon Sep 17 00:00:00 2001 From: prtsie Date: Thu, 12 Sep 2024 00:08:22 +0300 Subject: [PATCH] Navmenu related to current authorized user's role --- .../Components/Auth/AuthComponent.razor | 1 + .../UserDataProvider/IUserDataProvider.cs | 5 ++- .../UserDataProvider/UserDataProvider.cs | 15 +++++++-- .../Layout/NavMenu.razor | 31 ++++++++++++++----- .../Pages/Application.razor | 2 +- .../Pages/Applications.razor | 9 +++++- .../Pages/CreateApplication.razor | 2 +- 7 files changed, 50 insertions(+), 15 deletions(-) diff --git a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Components/Auth/AuthComponent.razor b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Components/Auth/AuthComponent.razor index 16d21bb..94e4841 100644 --- a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Components/Auth/AuthComponent.razor +++ b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Components/Auth/AuthComponent.razor @@ -26,6 +26,7 @@ var token = await Client.LoginAsync(authData.Email, authData.Password); Client.AuthToken = token; AuthData = authData; + UserDataProvider.UpdateCurrentRole(); Status?.SetSuccess("Logged in successfully."); } diff --git a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/IUserDataProvider.cs b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/IUserDataProvider.cs index d6b22d2..f72ad20 100644 --- a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/IUserDataProvider.cs +++ b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/IUserDataProvider.cs @@ -4,9 +4,12 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide { public interface IUserDataProvider { + public string? CurrentRole { get; } + + public Action? OnRoleChanged { get; set; } public Task GetApplicant(); - public string? GetCurrentRole(); + public void UpdateCurrentRole(); } } diff --git a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/UserDataProvider.cs b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/UserDataProvider.cs index 9119d94..56860ae 100644 --- a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/UserDataProvider.cs +++ b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Infrastructure/Services/UserDataProvider/UserDataProvider.cs @@ -9,16 +9,21 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide { private readonly static JwtSecurityTokenHandler tokenHandler = new(); + public string? CurrentRole { get; private set; } + + public Action? OnRoleChanged { get; set; } + public async Task GetApplicant() { return await client.GetApplicantAsync(); } - public string? GetCurrentRole() + public void UpdateCurrentRole() { if (client.AuthToken is null) { - return null; + CurrentRole = null; + return; } var token = tokenHandler.ReadJwtToken(client.AuthToken.Token); @@ -32,7 +37,11 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide default: throw new UnknownRoleException(); } - return role; + if (CurrentRole != role) + { + CurrentRole = role; + OnRoleChanged?.Invoke(); + } } } } diff --git a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Layout/NavMenu.razor b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Layout/NavMenu.razor index 86b80d9..589782a 100644 --- a/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Layout/NavMenu.razor +++ b/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Layout/NavMenu.razor @@ -1,4 +1,5 @@ -