using Blazorise;
using Blazorise.DataGrid;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper;
using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Server.Dialogs;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Pages
{
///
/// The users class (a. beging, 01.04.2022)
///
public partial class Users
{
#region Dependencies
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets or sets the user service.
/// The user service.
////////////////////////////////////////////////////////////////////////////////////////////////////
[Inject]
public UserService UserService { get; set; } = null!;
#endregion
#region Private Properties
///
/// Gets or sets the value of the password modal (ab)
///
private SetPasswordModal? PasswordModal { get; set; }
///
/// Gets or sets the value of the selected company texts (ab)
///
private List SelectedCompanyTexts { get; set; } = new();
///
/// Gets or sets the value of the selected user (ab)
///
private User? SelectedUser { get; set; }
///
/// Gets or sets the value of the user data grid (ab)
///
private DataGrid? UserDataGrid { get; set; }
///
/// Gets the value of the user groups (ab)
///
private IEnumerable UserGroups => Enum.GetValues();
///
/// Gets or sets the value of the user list (ab)
///
private List? UserList { get; set; }
#endregion
#region Override InitializeDataAsync
///
protected override async Task InitializeDataAsync()
{
await LoadUsers();
}
#endregion
#region Private Method LoadUsers
///
/// Loads the users (a. beging, 01.04.2022)
///
private async Task LoadUsers()
{
var usersR = await UserService.GetUsersAsync();
if (usersR.Success) UserList = usersR.Data;
await InvokeAsync(StateHasChanged);
}
#endregion
#region Private Method OnPasswordSet
///
/// Ons the password set using the specified user (a. beging, 23.05.2022)
///
/// The user
private async Task OnPasswordSet(User user)
{
var setPasswordR = await UserService.SetPassword(user);
if (setPasswordR.Success)
await Notification.Success("Passwort gespeichert");
}
#endregion
#region Private Method RowInserted
///
/// Rows the inserted using the specified arg (a. beging, 01.04.2022)
///
/// The arg
private async Task RowInserted(SavedRowItem> arg)
{
var addUserR = await UserService.AddUserAsync(arg.Item);
if (!addUserR.Success)
await Notification.Error($"Fehler beim Anlegen: {addUserR.ErrorMessage}")!;
else
await LoadUsers();
}
#endregion
#region Private Method RowRemoved
///
/// Rows the removed using the specified arg (a. beging, 08.02.2023)
///
/// The arg
private async Task RowRemoved(User arg)
{
var removeR = await UserService.RemoveAsync(arg.Id);
if (!removeR.Success)
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
else
await LoadUsers();
}
#endregion
#region Private Method RowRemoving
///
/// Rows the removing using the specified arg (a. beging, 08.02.2023)
///
/// The arg
private async Task RowRemoving(CancellableRowChange arg)
{
if (arg.Item.IsAdmin())
{
await Notification.Error("Admins können nicht gelöscht werden!");
arg.Cancel = true;
return;
}
var confirm = await Message.Confirm($"User {arg.Item.Mail} löschen?", "Bestätigen", o =>
{
o.ConfirmButtonText = "Löschen";
o.CancelButtonText = "Abbrechen";
o.ShowMessageIcon = false;
o.ConfirmButtonColor = Color.Danger;
});
arg.Cancel = !confirm;
}
#endregion
#region Private Method RowUpdated
///
/// Rows the updated using the specified arg (a. beging, 01.04.2022)
///
/// The arg
private async Task RowUpdated(SavedRowItem> arg)
{
if (arg.Item?.Id == null || arg.Item.Id.Equals(Guid.Empty) || arg.Values?.Any() != true) return;
var updateR = await UserService.Update(arg.Item);
if (!updateR.Success)
await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!;
}
#endregion
}
}