@page "/administration/user/{UserId}"
@using HopFrame.Database.Models
@using HopFrame.Security.Services
@using HopFrame.Web.Pages.Administration.Layout
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Forms
@layout AdminLayout
@rendermode InteractiveServer
Edit @User.Username
Edit @User.Username (@User.Id)
@**@
@inject IUserService Users
@code {
[Parameter]
public string UserId { get; set; }
private EditContext _context;
private ValidationMessageStore _messages;
[SupplyParameterFromForm]
public User User { get; set; }
protected override async Task OnInitializedAsync() {
User = await Users.GetUser(Guid.Parse(UserId));
_context = new EditContext(User);
_context.OnValidationRequested += ValidateForm;
_messages = new ValidationMessageStore(_context);
}
private async Task OnEdit() {
var hasConflict = false;
if (await Users.GetUserByEmail(User.Email) is not null) {
_messages.Add(() => User.Email, "Email is already in use");
hasConflict = true;
}
if (await Users.GetUserByUsername(User.Username) is not null) {
_messages.Add(() => User.Username, "Username is already in use");
hasConflict = true;
}
if (hasConflict) return;
}
private void ValidateForm(object sender, ValidationRequestedEventArgs e) {
_messages.Clear();
if (!User.Email.Contains("@") || !User.Email.Contains(".") || User.Email.EndsWith(".")) {
_messages.Add(() => User.Email, "Please enter a valid email address");
}
}
}