the datagrid now updates when an item is added or removed

users
maxime.batista 1 year ago
parent 0c06b19e6e
commit fd4cabbd1a

@ -1,5 +1,6 @@
@page "/users"
@using AdminPanel.Models
@using System.ComponentModel.DataAnnotations
<PageTitle>User Panel</PageTitle>
@ -9,9 +10,22 @@
<MudOverlay Visible="IsAddingUser" DarkBackground>
<MudForm id="add-account-form">
<MudTextField T="string" Label="Name" Required @bind-Value="FormAccountName"/>
<MudTextField T="string" Label="Email" Required @bind-Value="FormAccountEmail"/>
<MudTextField T="string" InputType="InputType.Password" Label="Password" Required @bind-Value="FormAccountPassword"/>
<MudTextField T="string"
Label="Name"
@bind-Value="FormAccountName"
Required
Validation="@(VerifyLength(6, 256))"/>
<MudTextField T="string"
Label="Email"
@bind-Value="FormAccountEmail"
Required
Validation="@(new EmailAddressAttribute() {ErrorMessage = "The email address is invalid"})"/>
<MudTextField T="string"
InputType="InputType.Password"
Label="Password"
@bind-Value="FormAccountPassword"
Validation="@(VerifyLength(6, 256))"
Required/>
<MudCheckBox T="bool" Label="Is Administrator" @bind-Value="FormAccountIsAdmin"/>
<MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => IsAddingUser = false)">Cancel</MudButton>
@ -27,18 +41,18 @@
<MudIconButton Icon="@Icons.Material.Filled.Remove" OnClick="RemoveSelection" Color="@Color.Error" Class="remove-item-btn"/>
<MudText Align="Align.Center">Remove Selection</MudText>
</div>
<MudDataGrid
T="User"
MultiSelection="true"
@ref="Grid"
ServerData="LoadUsersFromServer"
MultiSelection="true"
ColumnResizeMode="ResizeMode.Column"
RowsPerPage="1"
RowsPerPage="10"
ReadOnly="false"
EditMode="DataGridEditMode.Form"
@bind-SelectedItems="SelectedItems"
CommittedItemChanges="OnAccountEdited">
CommittedItemChanges="OnAccountUpdated">
<Columns>
<SelectColumn T="User"/>
<PropertyColumn Property="x => x.Name" Required/>

@ -1,5 +1,4 @@
using System.ComponentModel;
using AdminPanel.Models;
using AdminPanel.Models;
using AdminPanel.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
@ -12,6 +11,9 @@ namespace AdminPanel.Pages
[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; }
@ -33,7 +35,7 @@ namespace AdminPanel.Pages
}
private async void OnAccountEdited(User user)
private async void OnAccountUpdated(User user)
{
Console.WriteLine(user.IsAdmin);
try
@ -42,7 +44,8 @@ namespace AdminPanel.Pages
}
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.");
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.");
}
}
@ -55,7 +58,7 @@ namespace AdminPanel.Pages
{
await Service.AddUser(FormAccountName!, FormAccountEmail!, FormAccountPassword!,
FormAccountIsAdmin);
StateHasChanged();
await Grid.ReloadServerData();
}
catch (Exception)
{
@ -70,11 +73,17 @@ namespace AdminPanel.Pages
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})";
}
}
}
Loading…
Cancel
Save