Register page
This commit is contained in:
		| @@ -1,31 +1,38 @@ | ||||
| @page "/login" | ||||
| @page "/" | ||||
| @using VisaApiClient | ||||
| @using BlazorWebAssemblyVisaApiClient.Components.FormComponents.Applicants | ||||
|  | ||||
| <PageTitle>Authentication</PageTitle> | ||||
|  | ||||
| <EditForm Model="loginData" OnValidSubmit="TryLogin"> | ||||
|     <DataAnnotationsValidator/> | ||||
|     <label >Email: <InputText @bind-Value="loginData.Email"/></label> | ||||
|     <label >Password: <InputText @bind-Value="loginData.Password"/></label> | ||||
|     <input type="submit" value="Login"/> | ||||
| </EditForm> | ||||
| <p>@loginResult</p> | ||||
| <div class="with-centered-content"> | ||||
|     <EditForm class="form" Model="loginData" OnValidSubmit="TryLogin"> | ||||
|         <DataAnnotationsValidator/> | ||||
|  | ||||
|         <AuthDataInput AuthData="loginData"/><br/> | ||||
|  | ||||
|         <input class="btn-outline-primary rounded" type="submit" value="Login"/> | ||||
|         or | ||||
|         <NavLink href="register">Register</NavLink > | ||||
|         <p>@loginResult</p> | ||||
|     </EditForm> | ||||
| </div> | ||||
|  | ||||
| @code | ||||
| { | ||||
|     private AuthData loginData = new(); | ||||
|     private string loginResult = string.Empty; | ||||
|  | ||||
|     [Inject] | ||||
|     private Client Client { get; set; } = null!; | ||||
|     [Inject] private Client Client { get; set; } = null!; | ||||
|  | ||||
|     private async Task TryLogin(EditContext obj) | ||||
|     private async Task TryLogin() | ||||
|     { | ||||
|         loginResult = "Wait..."; | ||||
|         StateHasChanged(); | ||||
|         try | ||||
|         { | ||||
|             var token = await Client.LoginAsync(loginData.Email, loginData.Password); | ||||
|             Client.SetAuthToken(token); | ||||
|             loginResult = "Logged in successfully"; | ||||
|             loginResult = "Logged in successfully."; | ||||
|         } | ||||
|         catch (ApiException<ProblemDetails> e) | ||||
|         { | ||||
|   | ||||
| @@ -1,19 +0,0 @@ | ||||
| @page "/counter" | ||||
|  | ||||
| <PageTitle>Counter</PageTitle> | ||||
|  | ||||
| <h1>Counter</h1> | ||||
|  | ||||
| <p role="status">Current count: @currentCount</p> | ||||
|  | ||||
| <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||||
|  | ||||
| @code { | ||||
|     private int currentCount = 0; | ||||
|  | ||||
|     private void IncrementCount() | ||||
|     { | ||||
|         currentCount++; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,7 +0,0 @@ | ||||
| @page "/" | ||||
|  | ||||
| <PageTitle>Home</PageTitle> | ||||
|  | ||||
| <h1>Hello, world!</h1> | ||||
|  | ||||
| Welcome to your new app. | ||||
| @@ -0,0 +1,218 @@ | ||||
| @page "/register" | ||||
| @using System.Net | ||||
| @using System.Text | ||||
| @using AutoMapper | ||||
| @using VisaApiClient | ||||
| @using BlazorWebAssemblyVisaApiClient.Components.FormComponents.Applicants | ||||
| @using BlazorWebAssemblyVisaApiClient.FluentValidation.Applicants.Models | ||||
| @using BlazorWebAssemblyVisaApiClient.Infrastructure.Services | ||||
| @using global::FluentValidation | ||||
| @using Newtonsoft.Json | ||||
| @using Newtonsoft.Json.Linq | ||||
|  | ||||
| <PageTitle>Registration</PageTitle> | ||||
|  | ||||
| <div class="horizontal-centered-content"> | ||||
|     <h3>Registration data</h3> | ||||
|     <EditForm class="form" Model="requestModel" OnValidSubmit="TryRegisterApplicant"> | ||||
|         <ObjectGraphDataAnnotationsValidator/> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Authentication data</h5> | ||||
|             <AuthDataInput AuthData="requestModel.RegisterRequest.AuthData"/> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Your Fullname</h5> | ||||
|             <NameInput Name="requestModel.ApplicantName"/> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Fullname of your mother</h5> | ||||
|             <NameInput Name="requestModel.MotherName"/> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Fullname of your father</h5> | ||||
|             <NameInput Name="requestModel.FatherName"/> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Your passport</h5> | ||||
|             <PassportInput Passport="requestModel.Passport"/> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Birth data</h5> | ||||
|             <div > | ||||
|                 <label> | ||||
|                     Country of birth:<br/> | ||||
|                     <InputText DisplayName="Country of birth" class="rounded" @bind-Value="requestModel.CountryOfBirth"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.CountryOfBirth"></ValidationMessage><br/> | ||||
|                 <label> | ||||
|                     City of birth:<br/> | ||||
|                     <InputText DisplayName="City of birth" class="rounded" @bind-Value="requestModel.CityOfBirth"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.CityOfBirth"></ValidationMessage><br/> | ||||
|                 <label> | ||||
|                     Birth date:<br/> | ||||
|                     <InputDate DisplayName="Birth date" class="rounded" @bind-Value="requestModel.BirthDate" max="@DateTimeProvider.Now()"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.BirthDate"></ValidationMessage> | ||||
|             </div> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Citizenship</h5> | ||||
|             <div > | ||||
|                 <label> | ||||
|                     Citizenship:<br/> | ||||
|                     <InputText class="rounded" @bind-Value="requestModel.Citizenship"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.Citizenship"></ValidationMessage><br/> | ||||
|                 <label> | ||||
|                     Citizenship by birth:<br/> | ||||
|                     <InputText DisplayName="Citizenship by birth" class="rounded" @bind-Value="requestModel.CitizenshipByBirth"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.CitizenshipByBirth"></ValidationMessage> | ||||
|             </div> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Address of your place of work</h5> | ||||
|             <div > | ||||
|                 <AddressInput Address="requestModel.PlaceOfWork.Address"/> | ||||
|             </div> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Place of work data</h5> | ||||
|             <div > | ||||
|                 <PlaceOfWorkInput PlaceOfWork="requestModel.PlaceOfWork"/><br/> | ||||
|  | ||||
|                 <label> | ||||
|                     Job title:<br/> | ||||
|                     <InputText DisplayName="Job title" class="rounded" @bind-Value="requestModel.JobTitle"/> | ||||
|                 </label><br/> | ||||
|                 <ValidationMessage For="() => requestModel.JobTitle"></ValidationMessage> | ||||
|             </div> | ||||
|         </div> | ||||
|  | ||||
|         <div class="form-block"> | ||||
|             <h5>Other</h5> | ||||
|  | ||||
|             <div > | ||||
|                 <label> | ||||
|                     Gender: <EnumInputList Model="requestModel" EnumProperty="r => r.Gender"/> | ||||
|                 </label> | ||||
|             </div><br/> | ||||
|  | ||||
|             <div > | ||||
|                 <label> | ||||
|                     Marital status: <EnumInputList Model="requestModel" EnumProperty="r => r.MaritalStatus"/> | ||||
|                 </label> | ||||
|             </div><br/> | ||||
|  | ||||
|             <div > | ||||
|                 <label> | ||||
|                     Non-resident: <InputCheckbox @bind-Value="requestModel.IsNonResident"/> | ||||
|                 </label> | ||||
|             </div> | ||||
|         </div><br/> | ||||
|  | ||||
|         <input type="submit" class="btn-outline-primary" value="Register"/> | ||||
|         <p class="@requestResultClass">@((MarkupString)requestResult)</p> | ||||
|     </EditForm> | ||||
| </div> | ||||
|  | ||||
| @code | ||||
| { | ||||
|     private RegisterApplicantRequestModel requestModel = new(); | ||||
|     private string requestResult = string.Empty; | ||||
|     private string requestResultClass = string.Empty; | ||||
|  | ||||
|     [Inject] public Client Client { get; set; } = null!; | ||||
|  | ||||
|     [Inject] IValidator<RegisterApplicantRequestModel> RegisterApplicantRequestValidator { get; set; } = null!; | ||||
|  | ||||
|     [Inject] IMapper Mapper { get; set; } = null!; | ||||
|  | ||||
|     [Inject] IDateTimeProvider DateTimeProvider { get; set; } = null!; | ||||
|  | ||||
|     protected override void OnInitialized() | ||||
|     { | ||||
|         requestModel.BirthDate = DateTime.Now; | ||||
|     } | ||||
|  | ||||
|     private void SetRequestResultMessage(string message) | ||||
|     { | ||||
|         requestResult = message; | ||||
|         requestResultClass = string.Empty; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
|  | ||||
|     private void SetRequestResultSuccess(string message) | ||||
|     { | ||||
|         requestResult = message; | ||||
|         requestResultClass = "text-success"; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
|  | ||||
|     private void SetRequestResultError(string message) | ||||
|     { | ||||
|         requestResult = message; | ||||
|         requestResultClass = "validation-message"; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
|  | ||||
|     private string ErrorsToString(IEnumerable<string> errors) | ||||
|     { | ||||
|         var stringBuilder = new StringBuilder(); | ||||
|         foreach (var error in errors) | ||||
|         { | ||||
|             stringBuilder.Append($"{error}<br/>"); | ||||
|         } | ||||
|  | ||||
|         return stringBuilder.ToString(); | ||||
|     } | ||||
|  | ||||
|     private async void TryRegisterApplicant() | ||||
|     { | ||||
|         var validationResult = await RegisterApplicantRequestValidator.ValidateAsync(requestModel); | ||||
|         if (!validationResult.IsValid) | ||||
|         { | ||||
|             var errorsString = ErrorsToString(validationResult.Errors.Select(e => e.ErrorMessage)); | ||||
|             SetRequestResultError(errorsString); | ||||
|  | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         SetRequestResultMessage("Wait..."); | ||||
|  | ||||
|         var request = Mapper.Map<RegisterApplicantRequest>(requestModel); | ||||
|         try | ||||
|         { | ||||
|             await Client.RegisterAsync(request); | ||||
|             SetRequestResultSuccess("Register successful. Now log in."); | ||||
|         } | ||||
|         catch (ApiException<ProblemDetails> e) | ||||
|         { | ||||
|             if (e.StatusCode == (int)HttpStatusCode.BadRequest | ||||
|                 && e.Result.AdditionalProperties.TryGetValue("errors", out var errors)) | ||||
|             { | ||||
|                 var errorsList = ((JArray)errors).ToObject<List<string>>(); | ||||
|                 if (errorsList is null) | ||||
|                 { | ||||
|                     throw new JsonException(); | ||||
|                 } | ||||
|  | ||||
|                 SetRequestResultError(ErrorsToString(errorsList)); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 SetRequestResultError(e.Result.Detail!); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,60 +0,0 @@ | ||||
| @page "/weather" | ||||
| @inject HttpClient Http | ||||
|  | ||||
| <PageTitle>Weather</PageTitle> | ||||
|  | ||||
| <h1>Weather</h1> | ||||
|  | ||||
| <p>This component demonstrates fetching data from the server.</p> | ||||
|  | ||||
| @if (forecasts == null) | ||||
| { | ||||
|     <p> | ||||
|         <em>Loading...</em> | ||||
|     </p> | ||||
| } | ||||
| else | ||||
| { | ||||
|     <table class="table"> | ||||
|         <thead> | ||||
|         <tr> | ||||
|             <th>Date</th> | ||||
|             <th>Temp. (C)</th> | ||||
|             <th>Temp. (F)</th> | ||||
|             <th>Summary</th> | ||||
|         </tr> | ||||
|         </thead> | ||||
|         <tbody> | ||||
|         @foreach (var forecast in forecasts) | ||||
|         { | ||||
|             <tr> | ||||
|                 <td>@forecast.Date.ToShortDateString()</td> | ||||
|                 <td>@forecast.TemperatureC</td> | ||||
|                 <td>@forecast.TemperatureF</td> | ||||
|                 <td>@forecast.Summary</td> | ||||
|             </tr> | ||||
|         } | ||||
|         </tbody> | ||||
|     </table> | ||||
| } | ||||
|  | ||||
| @code { | ||||
|     private WeatherForecast[]? forecasts; | ||||
|  | ||||
|     protected override async Task OnInitializedAsync() | ||||
|     { | ||||
|         forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json"); | ||||
|     } | ||||
|  | ||||
|     public class WeatherForecast | ||||
|     { | ||||
|         public DateOnly Date { get; set; } | ||||
|  | ||||
|         public int TemperatureC { get; set; } | ||||
|  | ||||
|         public string? Summary { get; set; } | ||||
|  | ||||
|         public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user