Files
schengen-visa/SchengenVisaApi/BlazorWebAssemblyVisaApiClient/Pages/CreateApplication.razor

152 lines
5.3 KiB
Plaintext

@page "/applications/new"
@using System.Net
@using AutoMapper
@using BlazorWebAssemblyVisaApiClient.Validation.VisaApplications.Models
@using BlazorWebAssemblyVisaApiClient.Components.FormComponents.Applicants
@using VisaApiClient
@using BlazorWebAssemblyVisaApiClient.Components
@using BlazorWebAssemblyVisaApiClient.Components.FormComponents.VisaApplications
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
@using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
@using FluentValidation
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@inherits BlazorWebAssemblyVisaApiClient.Components.Base.VisaClientComponentBase
<PageTitle>New Application</PageTitle>
<div class="horizontal-centered-content">
<h3>New application</h3>
<EditForm class="form" Model="requestModel" OnValidSubmit="TryCreate">
<ObjectGraphDataAnnotationsValidator/>
<div class="form-block">
<h5>Visa</h5>
<label>
Destination Country:<br/>
<InputText DisplayName="Destination Country" class="rounded" @bind-Value="requestModel.DestinationCountry"/>
</label><br/>
<ValidationMessage For="() => requestModel.DestinationCountry"></ValidationMessage><br/>
<label>
Category:
<EnumInputList Model="requestModel"
EnumProperty="r => r.VisaCategory"
OnChanged="StateHasChanged"/>
</label><br/>
<ValidationMessage For="() => requestModel.VisaCategory"></ValidationMessage><br/>
<label>
Number of entries: <EnumInputList Model="requestModel" EnumProperty="r => r.RequestedNumberOfEntries"/>
</label><br/>
<ValidationMessage For="() => requestModel.RequestedNumberOfEntries"></ValidationMessage><br/>
<label>
For group: <InputCheckbox @bind-Value="requestModel.IsForGroup"/>
</label><br/>
<ValidationMessage For="() => requestModel.IsForGroup"></ValidationMessage><br/>
<label>
Valid for days:<br/>
<InputNumber DisplayName="Valid days" class="rounded" @bind-Value="requestModel.ValidDaysRequested"/>
</label>
<ValidationMessage For="() => requestModel.ValidDaysRequested"></ValidationMessage><br/>
</div>
@if (requestModel.VisaCategory is VisaCategory.Transit)
{
<div class="form-block">
<h5>Permission to destination Country</h5>
<PermissionToDestCountryInput PermissionToDestCountry="requestModel.PermissionToDestCountry"/>
</div>
}
@if (isNonResident)
{
<div class="form-block">
<h5>Re-entry permission</h5>
<ReentryPermitInput ReentryPermit="requestModel.ReentryPermit" />
</div>
<br/>
}
<input type="submit" class="btn-outline-primary" value="Register"/>
<ValidationSummary/>
<Status @ref="status"/>
</EditForm>
</div>
//todo past visas and visits
@code {
private VisaApplicationCreateRequestModel requestModel = new();
private Status status = null!;
private bool isNonResident;
[Inject] IDateTimeProvider DateTimeProvider { get; set; } = null!;
[Inject] IUserDataProvider UserDataProvider { get; set; } = null!;
[Inject] IValidator<VisaApplicationCreateRequestModel> VisaApplicationCreateRequestValidator { get; set; } = null!;
[Inject] IMapper Mapper { get; set; } = null!;
protected override async Task OnInitializedAsync()
{
try
{
isNonResident = (await UserDataProvider.GetApplicant()).IsNonResident;
}
catch (Exception e)
{
ErrorHandler.Handle(e);
}
requestModel.PermissionToDestCountry!.ExpirationDate = DateTimeProvider.Now();
}
private async Task TryCreate()
{
var validationResult = await VisaApplicationCreateRequestValidator.ValidateAsync(requestModel);
if (!validationResult.IsValid)
{
var errorsString = validationResult.ToErrorsString();
status.SetError(errorsString);
}
status.SetMessage("Wait...");
var request = Mapper.Map<VisaApplicationCreateRequest>(requestModel);
try
{
await Client.CreateApplicationAsync(request);
status.SetSucces("Application created successfully.");
}
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)
{
ErrorHandler.Handle(new JsonException("Can't convert validation errors to list"));
return;
}
status.SetError(string.Join("<br/>", errorsList));
}
else
{
ErrorHandler.Handle(e);
}
}
catch (Exception e)
{
ErrorHandler.Handle(e);
}
}
}