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" @page "/users"
@using AdminPanel.Models @using AdminPanel.Models
@using System.ComponentModel.DataAnnotations
<PageTitle>User Panel</PageTitle> <PageTitle>User Panel</PageTitle>
@ -9,9 +10,22 @@
<MudOverlay Visible="IsAddingUser" DarkBackground> <MudOverlay Visible="IsAddingUser" DarkBackground>
<MudForm id="add-account-form"> <MudForm id="add-account-form">
<MudTextField T="string" Label="Name" Required @bind-Value="FormAccountName"/> <MudTextField T="string"
<MudTextField T="string" Label="Email" Required @bind-Value="FormAccountEmail"/> Label="Name"
<MudTextField T="string" InputType="InputType.Password" Label="Password" Required @bind-Value="FormAccountPassword"/> @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"/> <MudCheckBox T="bool" Label="Is Administrator" @bind-Value="FormAccountIsAdmin"/>
<MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => IsAddingUser = false)">Cancel</MudButton> <MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => IsAddingUser = false)">Cancel</MudButton>
@ -28,17 +42,17 @@
<MudText Align="Align.Center">Remove Selection</MudText> <MudText Align="Align.Center">Remove Selection</MudText>
</div> </div>
<MudDataGrid <MudDataGrid
T="User" T="User"
MultiSelection="true" @ref="Grid"
ServerData="LoadUsersFromServer" ServerData="LoadUsersFromServer"
MultiSelection="true"
ColumnResizeMode="ResizeMode.Column" ColumnResizeMode="ResizeMode.Column"
RowsPerPage="1" RowsPerPage="10"
ReadOnly="false" ReadOnly="false"
EditMode="DataGridEditMode.Form" EditMode="DataGridEditMode.Form"
@bind-SelectedItems="SelectedItems" @bind-SelectedItems="SelectedItems"
CommittedItemChanges="OnAccountEdited"> CommittedItemChanges="OnAccountUpdated">
<Columns> <Columns>
<SelectColumn T="User"/> <SelectColumn T="User"/>
<PropertyColumn Property="x => x.Name" Required/> <PropertyColumn Property="x => x.Name" Required/>

@ -1,5 +1,4 @@
using System.ComponentModel; using AdminPanel.Models;
using AdminPanel.Models;
using AdminPanel.Services; using AdminPanel.Services;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
@ -12,6 +11,9 @@ namespace AdminPanel.Pages
[Inject] public ISnackbar Snackbar { get; private init; } [Inject] public ISnackbar Snackbar { get; private init; }
[Inject] public IUsersService Service { get; private init; } [Inject] public IUsersService Service { get; private init; }
private MudDataGrid<User> Grid { get; set; }
private HashSet<User> SelectedItems { get; set; } = new(); private HashSet<User> SelectedItems { get; set; } = new();
private string? FormAccountName { get; set; } 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); Console.WriteLine(user.IsAdmin);
try try
@ -42,7 +44,8 @@ namespace AdminPanel.Pages
} }
catch (Exception) 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!, await Service.AddUser(FormAccountName!, FormAccountEmail!, FormAccountPassword!,
FormAccountIsAdmin); FormAccountIsAdmin);
StateHasChanged(); await Grid.ReloadServerData();
} }
catch (Exception) catch (Exception)
{ {
@ -70,11 +73,17 @@ namespace AdminPanel.Pages
try try
{ {
await Service.RemoveUsers(items); await Service.RemoveUsers(items);
await Grid.ReloadServerData();
} }
catch (Exception) catch (Exception)
{ {
Snackbar.Add("Server responded with errors"); 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