73 lines
2.3 KiB
Plaintext
73 lines
2.3 KiB
Plaintext
@using SharedProject.common
|
|
@using System.Text.RegularExpressions
|
|
@inject HttpClient Client
|
|
@inject ILogger<ChangePlayerNameDialog> Logger
|
|
|
|
<MudDialog>
|
|
<TitleContent>
|
|
<MudText Typo="Typo.h6">
|
|
Change Player Name
|
|
</MudText>
|
|
</TitleContent>
|
|
<DialogContent>
|
|
<MudForm @bind-IsValid="IsValid">
|
|
<MudTextField Value="@Data.CardId" Label="CardId" ReadOnly="true"/>
|
|
<MudTextField @bind-Value="Data.PlayerName"
|
|
Immediate="true"
|
|
Counter="SharedConstants.MAX_PLAYER_NAME_LENGTH"
|
|
MaxLength="SharedConstants.MAX_PLAYER_NAME_LENGTH"
|
|
Validation="ValidatePlayerName"/>
|
|
</MudForm>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton OnClick="Cancel">Cancel</MudButton>
|
|
<MudButton Color="Color.Primary" OnClick="Submit" Disabled="@(!IsValid)">Confirm</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
|
|
@code {
|
|
|
|
[CascadingParameter]
|
|
MudDialogInstance MudDialog { get; set; } = null!;
|
|
|
|
[Parameter]
|
|
public SharedProject.models.User Data { get; set; } = null!;
|
|
|
|
private string originalName = string.Empty;
|
|
|
|
private bool IsValid { get; set; }
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
originalName = new string(Data.PlayerName);
|
|
}
|
|
|
|
async Task Submit()
|
|
{
|
|
Logger.LogInformation("Data is {cardId}, {name}", Data.CardId, Data.PlayerName);
|
|
var response = await Client.PostAsJsonAsync("api/Users/SetPlayerName", Data);
|
|
var result = await response.Content.ReadFromJsonAsync<bool>();
|
|
Logger.LogInformation("SetPlayerName result is {result}", result);
|
|
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!",
|
|
> 8 => "Player name cannot be longer than 8 characters!",
|
|
_ => !Regex.IsMatch(playerName, pattern) ? "Player name contains invalid character!" : null
|
|
};
|
|
|
|
}
|
|
} |