@using System.Text.RegularExpressions @using Shared.Dto.Api @using Shared.Models @using Throw @inject HttpClient Client @inject ILogger Logger @{ #pragma warning disable CS8974 } Change Player Name Cancel Confirm @code { [CascadingParameter] MudDialogInstance MudDialog { get; set; } = null!; [Parameter] public ClientCardDto Data { get; set; } = null!; private string originalName = string.Empty; private bool IsValid { get; set; } private const int PLAYER_NAME_MAX_LENGTH = 8; protected override void OnInitialized() { base.OnInitialized(); originalName = new string(Data.PlayerName); } async Task Submit() { if (originalName.Equals(Data.PlayerName)) { MudDialog.Close(DialogResult.Ok(true)); return; } Logger.LogInformation("Data is {CardId}, {Name}", Data.CardId, Data.PlayerName); var response = await Client.PostAsJsonAsync("api/Profiles/PlayerName", Data); var result = await response.Content.ReadFromJsonAsync>(); result.ThrowIfNull(); Logger.LogInformation("SetPlayerName result is {Result}", result.Succeeded); MudDialog.Close(DialogResult.Ok(result)); } void Cancel() { Data.PlayerName = originalName; MudDialog.Cancel(); } private static string? ValidatePlayerName(string playerName) { const string pattern = @"^[a-zA-Z0-9!?,./\-+:<>_\\@*#&=() ]{1,8}$"; return playerName.Length switch { 0 => "Player name cannot be empty!", > PLAYER_NAME_MAX_LENGTH => "Player name cannot be longer than 8 characters!", _ => !Regex.IsMatch(playerName, pattern) ? "Player name contains invalid character!" : null }; } }