Navmenu related to current authorized user's role
This commit is contained in:
		| @@ -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."); | ||||
|         } | ||||
|   | ||||
| @@ -4,9 +4,12 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide | ||||
| { | ||||
|     public interface IUserDataProvider | ||||
|     { | ||||
|         public string? CurrentRole { get; } | ||||
|  | ||||
|         public Action? OnRoleChanged { get; set; } | ||||
|  | ||||
|         public Task<ApplicantModel> GetApplicant(); | ||||
|  | ||||
|         public string? GetCurrentRole(); | ||||
|         public void UpdateCurrentRole(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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<ApplicantModel> 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(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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"> | ||||
|         <a class="navbar-brand" href="">Schengen Visa</a> | ||||
|         <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu"> | ||||
| @@ -15,13 +16,18 @@ | ||||
|             </NavLink> | ||||
|         </div> | ||||
|     </nav> | ||||
|     <nav class="flex-column"> | ||||
|         <div class="nav-item px-3"> | ||||
|             <NavLink class="nav-link" href="applications" Match="NavLinkMatch.All"> | ||||
|                 <span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Applications | ||||
|             </NavLink> | ||||
|         </div> | ||||
|     </nav> | ||||
|     @if (UserDataProvider.CurrentRole is Constants.ApplicantRole or Constants.ApprovingAuthorityRole) | ||||
|     { | ||||
|         <nav class="flex-column"> | ||||
|             <div class="nav-item px-3"> | ||||
|                 <NavLink class="nav-link" href="applications" Match="NavLinkMatch.All"> | ||||
|                     <span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Applications | ||||
|                 </NavLink> | ||||
|             </div> | ||||
|         </nav> | ||||
|     } | ||||
|     @if(UserDataProvider.CurrentRole is Constants.ApplicantRole) | ||||
|     { | ||||
|     <nav class="flex-column"> | ||||
|         <div class="nav-item px-3"> | ||||
|             <NavLink class="nav-link" href="applications/new" Match="NavLinkMatch.All"> | ||||
| @@ -29,13 +35,22 @@ | ||||
|             </NavLink> | ||||
|         </div> | ||||
|     </nav> | ||||
|     } | ||||
| </div> | ||||
|  | ||||
| @code { | ||||
|     private bool collapseNavMenu = true; | ||||
|     private string? currentRole = null!; | ||||
|  | ||||
|     private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null; | ||||
|  | ||||
|     [Inject] private IUserDataProvider UserDataProvider { get; set; } = null!; | ||||
|  | ||||
|     protected override void OnInitialized() | ||||
|     { | ||||
|         UserDataProvider.OnRoleChanged += StateHasChanged; | ||||
|     } | ||||
|  | ||||
|     private void ToggleNavMenu() | ||||
|     { | ||||
|         collapseNavMenu = !collapseNavMenu; | ||||
|   | ||||
| @@ -199,7 +199,7 @@ | ||||
|         try | ||||
|         { | ||||
|             var applicationId = Guid.Parse(ApplicationId); | ||||
|             currentRole = UserDataProvider.GetCurrentRole() ?? throw new NotLoggedInException(); | ||||
|             currentRole = UserDataProvider.CurrentRole ?? throw new NotLoggedInException(); | ||||
|  | ||||
|                 application = currentRole switch | ||||
|                 { | ||||
|   | ||||
| @@ -60,7 +60,14 @@ | ||||
|  | ||||
|     protected override async Task OnInitializedAsync() | ||||
|     { | ||||
|         currentRole = UserDataProvider.GetCurrentRole()!; | ||||
|         try | ||||
|         { | ||||
|             currentRole = UserDataProvider.CurrentRole ?? throw new NotLoggedInException(); | ||||
|         } | ||||
|         catch (Exception e) | ||||
|         { | ||||
|             ErrorHandler.Handle(e); | ||||
|         } | ||||
|         await Fetch(); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -178,7 +178,7 @@ | ||||
|  | ||||
|         @if (isNonResident) | ||||
|         { | ||||
|             requestModel.ReentryPermit = NewReentryPermit(); | ||||
|             requestModel.ReentryPermit ??= NewReentryPermit(); | ||||
|             <div class="form-block"> | ||||
|                 <h5>Re-entry permission@(Constants.RequiredFieldMarkup)</h5> | ||||
|                 <ReentryPermitInput ReentryPermit="requestModel.ReentryPermit"/> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user