Merge remote-tracking branch 'origin/12-client' into 12-client

This commit is contained in:
2024-09-06 09:59:46 +03:00
11 changed files with 93 additions and 11 deletions

View File

@@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.DataAnnotations.Validation" Version="3.2.0-rc1.20223.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.1"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.1" PrivateAssets="all"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.2" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,11 +1,16 @@
@using System.Net
@using System.IdentityModel.Tokens.Jwt
@using System.Security.Claims
@using BlazorWebAssemblyVisaApiClient.Components.Auth.Exceptions
@using BlazorWebAssemblyVisaApiClient.ErrorHandling
@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();
[CascadingParameter] private GlobalErrorHandler ErrorHandler { get; set; } = null!;
@@ -24,6 +29,10 @@
{
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;
Status?.SetSucces("Logged in successfully.");

View File

@@ -1,6 +1,6 @@
@using System.ComponentModel.DataAnnotations
@using System.Linq.Expressions
@using System.Linq.Expressions
@using System.Reflection
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
@typeparam TItem where TItem : class
@typeparam TMember where TMember : struct, Enum
@@ -22,18 +22,12 @@
protected override void OnInitialized()
{
var enumMembers = typeof(TMember).GetMembers();
var modelMemberName = ((MemberExpression)EnumProperty.Body).Member.Name;
modelMemberInfo = typeof(TItem).GetProperty(modelMemberName)!;
foreach (var value in Enum.GetValues<TMember>())
{
var member = enumMembers.First(info => info.Name == value.ToString());
var displayAttribute = (DisplayAttribute?)member
.GetCustomAttributes(typeof(DisplayAttribute), false)
.FirstOrDefault();
var displayName = displayAttribute?.Name ?? value.ToString();
enumValues.Add(value, displayName);
enumValues.Add(value, value.GetDisplayName());
}
}

View File

@@ -1,4 +1,5 @@
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
@using VisaApiClient
<div>

View File

@@ -1,4 +1,5 @@
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider;
using FluentValidation;
using VisaApiClient;

View File

@@ -1,5 +1,6 @@
using BlazorWebAssemblyVisaApiClient.FluentValidation.Applicants.Models;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider;
using FluentValidation;
using VisaApiClient;
using PlaceOfWorkModel = BlazorWebAssemblyVisaApiClient.FluentValidation.Applicants.Models.PlaceOfWorkModel;

View File

@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
{
public static class EnumExtensions
{
public static string GetDisplayName(this Enum value)
{
var enumMembers = value.GetType().GetMembers();
var member = enumMembers.First(info => info.Name == value.ToString());
var displayAttribute = (DisplayAttribute?)member
.GetCustomAttributes(typeof(DisplayAttribute), false)
.FirstOrDefault();
var displayName = displayAttribute?.Name ?? value.ToString();
return displayName;
}
}
}

View File

@@ -1,4 +1,4 @@
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
{
public class DateTimeProvider : IDateTimeProvider
{

View File

@@ -1,4 +1,4 @@
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
{
public interface IDateTimeProvider
{

View File

@@ -0,0 +1,56 @@
@page "/applications"
@using System.Net
@using System.Text
@using BlazorWebAssemblyVisaApiClient.Components
@using BlazorWebAssemblyVisaApiClient.Components.Auth
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
@using VisaApiClient
@inherits BlazorWebAssemblyVisaApiClient.Components.Base.VisaClientComponentBase
<PageTitle>@(AuthComponent.CurrentRole ?? "bruh")</PageTitle>
@((MarkupString)htmlMarkup)
@code {
private string htmlMarkup = "bruh";
protected override async Task OnInitializedAsync()
{
var stringBuilder = new StringBuilder();
try
{
switch (AuthComponent.CurrentRole)
{
case "Applicant":
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>");
}
stringBuilder.AppendLine("</table >");
htmlMarkup = stringBuilder.ToString();
break;
default:
htmlMarkup = AuthComponent.CurrentRole;
break;
}
}
catch (Exception e)
{
if (e is ApiException<ProblemDetails> { Result.Status: (int)HttpStatusCode.Unauthorized } problemDetailsException)
{
htmlMarkup = problemDetailsException.Result.Detail!;
}
else
{
//ErrorHandler.Handle(e);
throw;
}
}
}
}

View File

@@ -10,6 +10,7 @@
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@using BlazorWebAssemblyVisaApiClient.Components
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
@inherits BlazorWebAssemblyVisaApiClient.Components.Base.VisaClientComponentBase
<PageTitle>Registration</PageTitle>