@using System.Text.RegularExpressions
@using Models.GlobalInstances
@using ShoppingAssistantWebClient.Web.Models
@inject IHttpContextAccessor httpContextAccessor;
@inject NavigationManager NavigationManager;
@errorMessage
@updateMessage
@code {
[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; }
private string phoneValidationMessage = "";
private string emailValidationMessage = "";
private bool isPhoneInvalid = false;
private bool isEmailInvalid = false;
private bool isApplyDisabled = true;
private string phone = "";
private string email = "";
private string password = "";
private void ValidatePhone(ChangeEventArgs e)
{
errorMessage = "";
phone = e.Value.ToString();
if (!string.IsNullOrWhiteSpace(phone) && !Regex.IsMatch(phone, @"^\+[0-9]{1,15}$"))
{
phoneValidationMessage = "Please enter a valid phone number";
isPhoneInvalid = true;
}
else
{
phoneValidationMessage = "";
isPhoneInvalid = false;
}
UpdateApplyButtonState();
}
private void ValidateEmail(ChangeEventArgs e)
{
errorMessage = "";
email = e.Value.ToString();
if (!string.IsNullOrWhiteSpace(email) && !Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
{
emailValidationMessage = "Please enter a valid email address.";
isEmailInvalid = true;
}
else
{
emailValidationMessage = "";
isEmailInvalid = false;
}
UpdateApplyButtonState();
}
private void UpdateApplyButtonState()
{
if(user.Roles.Any(role => role.Name == "User"))
isApplyDisabled = (string.IsNullOrWhiteSpace(phone) && string.IsNullOrWhiteSpace(email)) || isPhoneInvalid || isEmailInvalid;
else
isApplyDisabled = string.IsNullOrWhiteSpace(password) || (string.IsNullOrWhiteSpace(phone) && string.IsNullOrWhiteSpace(email)) || isPhoneInvalid || isEmailInvalid;
}
private void OnPasswordInput(ChangeEventArgs e)
{
errorMessage = "";
password = e.Value.ToString();
UpdateApplyButtonState();
}
private async Task Close() => await BlazoredModal.CloseAsync(ModalResult.Ok(true));
private async Task Cancel() => await BlazoredModal.CancelAsync();
private async Task Apply() {
await UpdateUser();
isApplyDisabled = true;
await GetUser();
StateHasChanged();
if(user.Roles.Any(role => role.Name == "User")) {
await Task.Delay(3000);
await InvokeAsync(() => {
updateMessage = "";
StateHasChanged();
});
}
}
private void RedirectToLogin() {
var url = $"/login";
NavigationManager.NavigateTo(url);
}
private string ShowErrorDivClass()
{
return string.IsNullOrEmpty(errorMessage) ? "hidden" : "alert alert-danger";
}
private string ShowUpdateDivClass() {
return string.IsNullOrEmpty(updateMessage) ? "hidden" : "alert alert-success";
}
}