You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.9 KiB
89 lines
2.9 KiB
using AdminPanel.Models;
|
|
using AdminPanel.Services;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
using MudBlazor;
|
|
|
|
namespace AdminPanel.Pages
|
|
{
|
|
public partial class UserListPanel
|
|
{
|
|
[Inject] public ISnackbar Snackbar { get; private init; }
|
|
[Inject] public IUsersService Service { get; private init; }
|
|
|
|
private MudDataGrid<User> Grid { get; set; }
|
|
|
|
|
|
private HashSet<User> SelectedItems { get; set; } = new();
|
|
|
|
private string? FormAccountName { get; set; }
|
|
private string? FormAccountEmail { get; set; }
|
|
private string? FormAccountPassword { get; set; }
|
|
private bool FormAccountIsAdmin { get; set; }
|
|
|
|
public bool IsAddingUser { get; set; }
|
|
|
|
|
|
private async Task<GridData<User>> LoadUsersFromServer(GridState<User> state)
|
|
{
|
|
var (count, users) = await Service.ListUsers((uint)(state.Page * state.PageSize), (uint)state.PageSize);
|
|
return new GridData<User>
|
|
{
|
|
TotalItems = (int)count,
|
|
Items = users
|
|
};
|
|
}
|
|
|
|
|
|
private async void OnAccountUpdated(User user)
|
|
{
|
|
Console.WriteLine(user.IsAdmin);
|
|
try
|
|
{
|
|
await Service.UpdateUser(user);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Snackbar.Add(
|
|
"Server responded with errors, your given input may be incorrect.\nIf you entered a new email, verify that the email is not used by another member.");
|
|
}
|
|
}
|
|
|
|
private async void ConfirmAddAccount(MouseEventArgs e)
|
|
{
|
|
// We no longer add an account if it is confirmed
|
|
IsAddingUser = false;
|
|
|
|
try
|
|
{
|
|
await Service.AddUser(FormAccountName!, FormAccountEmail!, FormAccountPassword!,
|
|
FormAccountIsAdmin);
|
|
await Grid.ReloadServerData();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Snackbar.Add("Server responded with errors, your given input may be incorrect.");
|
|
}
|
|
}
|
|
|
|
private async void RemoveSelection(MouseEventArgs e)
|
|
{
|
|
var items = SelectedItems.ToList().ConvertAll(u => u.Id);
|
|
Console.WriteLine(items.Count);
|
|
try
|
|
{
|
|
await Service.RemoveUsers(items);
|
|
await Grid.ReloadServerData();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Snackbar.Add("Server responded with errors");
|
|
}
|
|
}
|
|
|
|
private Func<string, string?> VerifyLength(uint min, uint max)
|
|
{
|
|
return s => (s.Length >= min && s.Length <= max) ? null : $"length is incorrect (must be between {min} and {max})";
|
|
}
|
|
}
|
|
} |