Navmenu related to current authorized user's role

This commit is contained in:
2024-09-12 00:08:22 +03:00
parent 96c70ef0ba
commit 18e02792ec
7 changed files with 50 additions and 15 deletions

View File

@@ -26,6 +26,7 @@
var token = await Client.LoginAsync(authData.Email, authData.Password); var token = await Client.LoginAsync(authData.Email, authData.Password);
Client.AuthToken = token; Client.AuthToken = token;
AuthData = authData; AuthData = authData;
UserDataProvider.UpdateCurrentRole();
Status?.SetSuccess("Logged in successfully."); Status?.SetSuccess("Logged in successfully.");
} }

View File

@@ -4,9 +4,12 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide
{ {
public interface IUserDataProvider public interface IUserDataProvider
{ {
public string? CurrentRole { get; }
public Action? OnRoleChanged { get; set; }
public Task<ApplicantModel> GetApplicant(); public Task<ApplicantModel> GetApplicant();
public string? GetCurrentRole(); public void UpdateCurrentRole();
} }
} }

View File

@@ -9,16 +9,21 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide
{ {
private readonly static JwtSecurityTokenHandler tokenHandler = new(); private readonly static JwtSecurityTokenHandler tokenHandler = new();
public string? CurrentRole { get; private set; }
public Action? OnRoleChanged { get; set; }
public async Task<ApplicantModel> GetApplicant() public async Task<ApplicantModel> GetApplicant()
{ {
return await client.GetApplicantAsync(); return await client.GetApplicantAsync();
} }
public string? GetCurrentRole() public void UpdateCurrentRole()
{ {
if (client.AuthToken is null) if (client.AuthToken is null)
{ {
return null; CurrentRole = null;
return;
} }
var token = tokenHandler.ReadJwtToken(client.AuthToken.Token); var token = tokenHandler.ReadJwtToken(client.AuthToken.Token);
@@ -32,7 +37,11 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide
default: throw new UnknownRoleException(); default: throw new UnknownRoleException();
} }
return role; if (CurrentRole != role)
{
CurrentRole = role;
OnRoleChanged?.Invoke();
}
} }
} }
} }

View File

@@ -1,4 +1,5 @@
<div class="top-row ps-3 navbar navbar-dark"> @using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" href="">Schengen Visa</a> <a class="navbar-brand" href="">Schengen Visa</a>
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu"> <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
@@ -15,6 +16,8 @@
</NavLink> </NavLink>
</div> </div>
</nav> </nav>
@if (UserDataProvider.CurrentRole is Constants.ApplicantRole or Constants.ApprovingAuthorityRole)
{
<nav class="flex-column"> <nav class="flex-column">
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="applications" Match="NavLinkMatch.All"> <NavLink class="nav-link" href="applications" Match="NavLinkMatch.All">
@@ -22,6 +25,9 @@
</NavLink> </NavLink>
</div> </div>
</nav> </nav>
}
@if(UserDataProvider.CurrentRole is Constants.ApplicantRole)
{
<nav class="flex-column"> <nav class="flex-column">
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="applications/new" Match="NavLinkMatch.All"> <NavLink class="nav-link" href="applications/new" Match="NavLinkMatch.All">
@@ -29,13 +35,22 @@
</NavLink> </NavLink>
</div> </div>
</nav> </nav>
}
</div> </div>
@code { @code {
private bool collapseNavMenu = true; private bool collapseNavMenu = true;
private string? currentRole = null!;
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null; private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;
[Inject] private IUserDataProvider UserDataProvider { get; set; } = null!;
protected override void OnInitialized()
{
UserDataProvider.OnRoleChanged += StateHasChanged;
}
private void ToggleNavMenu() private void ToggleNavMenu()
{ {
collapseNavMenu = !collapseNavMenu; collapseNavMenu = !collapseNavMenu;

View File

@@ -199,7 +199,7 @@
try try
{ {
var applicationId = Guid.Parse(ApplicationId); var applicationId = Guid.Parse(ApplicationId);
currentRole = UserDataProvider.GetCurrentRole() ?? throw new NotLoggedInException(); currentRole = UserDataProvider.CurrentRole ?? throw new NotLoggedInException();
application = currentRole switch application = currentRole switch
{ {

View File

@@ -60,7 +60,14 @@
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
currentRole = UserDataProvider.GetCurrentRole()!; try
{
currentRole = UserDataProvider.CurrentRole ?? throw new NotLoggedInException();
}
catch (Exception e)
{
ErrorHandler.Handle(e);
}
await Fetch(); await Fetch();
} }

View File

@@ -178,7 +178,7 @@
@if (isNonResident) @if (isNonResident)
{ {
requestModel.ReentryPermit = NewReentryPermit(); requestModel.ReentryPermit ??= NewReentryPermit();
<div class="form-block"> <div class="form-block">
<h5>Re-entry permission@(Constants.RequiredFieldMarkup)</h5> <h5>Re-entry permission@(Constants.RequiredFieldMarkup)</h5>
<ReentryPermitInput ReentryPermit="requestModel.ReentryPermit"/> <ReentryPermitInput ReentryPermit="requestModel.ReentryPermit"/>