Applications.razor for applicants

This commit is contained in:
2024-09-06 21:37:16 +03:00
parent 53d5758527
commit f90a0e7e82
13 changed files with 227 additions and 125 deletions

View File

@@ -1,16 +1,10 @@
@using System.Net
@using System.IdentityModel.Tokens.Jwt
@using System.Security.Claims
@using BlazorWebAssemblyVisaApiClient.Components.Auth.Exceptions
@using BlazorWebAssemblyVisaApiClient.ErrorHandling
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
@using VisaApiClient
@code {
public static bool LoggedIn;
public static ApplicantModel? CurrentApplicant; //todo api action
public static string? CurrentRole;
private static AuthData savedData = null!;
private static readonly JwtSecurityTokenHandler TokenHandler = new();
public static AuthData? AuthData;
[CascadingParameter] private GlobalErrorHandler ErrorHandler { get; set; } = null!;
@@ -20,6 +14,8 @@
[Inject] private NavigationManager Nav { get; set; } = null!;
[Inject] private IUserDataProvider UserDataProvider { get; set; } = null!;
///Authorize with email and password
/// <returns>Message to user</returns>
public async Task TryAuthorize(AuthData authData)
@@ -28,12 +24,8 @@
try
{
var token = await Client.LoginAsync(authData.Email, authData.Password);
Client.SetAuthToken(token);
CurrentRole = TokenHandler.ReadJwtToken(token.Token)
.Claims
.FirstOrDefault(claim => claim.Type == ClaimTypes.Role)?
.Value;
savedData = authData;
Client.AuthToken = token;
AuthData = authData;
Status?.SetSucces("Logged in successfully.");
}
@@ -57,20 +49,18 @@
}
///Re-auth if token expired or something
public async Task ReAuthenticate(bool redirectOnFailure = true)
public async Task ReAuthenticate()
{
if (!LoggedIn)
if (AuthData is not null)
{
if (redirectOnFailure)
{
Nav.NavigateTo("/");
return;
}
throw new NotLoggedInException();
await TryAuthorize(AuthData);
}
else
{
Client.AuthToken = null;
AuthData = null;
Nav.NavigateTo("/");
}
await TryAuthorize(savedData);
}
}

View File

@@ -0,0 +1,12 @@
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
{
public interface IUserDataProvider
{
public ApplicantModel? GetApplicant();
public string? GetCurrentRole();
}
}

View File

@@ -0,0 +1,30 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
{
public class UserDataProvider(Client client) : IUserDataProvider
{
private static readonly JwtSecurityTokenHandler tokenHandler = new ();
public ApplicantModel? GetApplicant()
{
//todo api action
return null;
}
public string? GetCurrentRole()
{
if (client.AuthToken is null)
{
return null;
}
var token = tokenHandler.ReadJwtToken(client.AuthToken.Token);
var role = token.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Role)?.Value;
return role;
}
}
}

View File

@@ -15,6 +15,13 @@
</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>
</div>
@code {

View File

@@ -1,17 +1,30 @@
@page "/applications"
@using System.Net
@using System.Text
@using BlazorWebAssemblyVisaApiClient.Components.Auth
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
@using VisaApiClient
@inherits BlazorWebAssemblyVisaApiClient.Components.Base.VisaClientComponentBase
<PageTitle>@(AuthComponent.CurrentRole ?? "bruh")</PageTitle>
<PageTitle>Applications</PageTitle>
@((MarkupString)htmlMarkup)
<table class="table table-bordered">
<thead>
@((MarkupString)htmlHead)
</thead>
<tbody>
@((MarkupString)htmlBody)
</tbody>
</table >
@code {
private string htmlMarkup = "bruh";
private string htmlBody = null!;
private string htmlHead = null!;
[Inject] private IUserDataProvider UserDataProvider { get; set; } = null!;
[Inject] private NavigationManager Nav { get; set; } = null!;
protected override async Task OnInitializedAsync()
{
@@ -19,35 +32,42 @@
try
{
switch (AuthComponent.CurrentRole)
{
case "Applicant":
var applications = await Client.GetForApplicantAsync();
switch (UserDataProvider.GetCurrentRole())
{
case "Applicant":
htmlHead = "<tr><th>Destination Country</th><th>Visa Category</th><th>Request date</th><th>Days requested</th><th>Status</th></tr>";
var applications = await Client.GetForApplicantAsync();
stringBuilder.AppendLine("<table><tr><th>Destination Country</th><th>Visa Category</th><th>Request date</th><th>Days requested</th><th>Status</th></tr>");
foreach (var application in applications)
{
stringBuilder.AppendLine($"<tr><th>{application.DestinationCountry}</th><th>{application.VisaCategory.GetDisplayName()}</th><th>{application.RequestDate.ToString("d")}</th><th>{application.ValidDaysRequested}</th><th>{application.Status.GetDisplayName()}</th></tr>");
}
foreach (var application in applications)
{
var rowClass = application.Status switch
{
ApplicationStatus.Pending => "",
ApplicationStatus.Approved => "table-success",
ApplicationStatus.Rejected => "table-danger",
ApplicationStatus.Closed => "table-danger",
_ => throw new ArgumentOutOfRangeException()
};
stringBuilder.AppendLine("</table >");
htmlMarkup = stringBuilder.ToString();
break;
stringBuilder.AppendLine($"<tr class=\"{rowClass}\"><th>{application.DestinationCountry}</th><th>{application.VisaCategory.GetDisplayName()}</th><th>{application.RequestDate.ToString("d")}</th><th>{application.ValidDaysRequested}</th><th>{application.Status.GetDisplayName()}</th></tr>");
}
htmlBody = stringBuilder.ToString();
break;
default:
htmlMarkup = AuthComponent.CurrentRole;
break;
}
Nav.NavigateTo("/"); //todo feedback
break;
}
}
catch (Exception e)
{
if (e is ApiException<ProblemDetails> { Result.Status: (int)HttpStatusCode.Unauthorized } problemDetailsException)
{
htmlMarkup = problemDetailsException.Result.Detail!;
htmlBody = $"<p>{problemDetailsException.Result.Detail!}</p>";
}
else
{
//ErrorHandler.Handle(e);
throw;
ErrorHandler.Handle(e);
}
}
}

View File

@@ -1,5 +1,6 @@
using System.Reflection;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider;
using FluentValidation;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -23,6 +24,7 @@ public class Program
builder.Services.AddScoped<Client>(sp => new Client(baseAddress, sp.GetRequiredService<HttpClient>()));
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
builder.Services.AddScoped<IUserDataProvider, UserDataProvider>();
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());