Files
FoodsharingOnboarding/FoodsharingSiegen.Server/Pages/Users.razor.cs
2022-04-08 16:10:26 +02:00

158 lines
4.6 KiB
C#

using Blazorise.DataGrid;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Pages
{
/// <summary>
/// The users class (a. beging, 01.04.2022)
/// </summary>
public partial class Users
{
#region Dependencies (Injected)
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets or sets the user service. </summary>
///
/// <value> The user service. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
[Inject] public UserService UserService { get; set; } = null!;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the value of the user data grid (ab)
/// </summary>
public DataGrid<User> UserDataGrid { get; set; }
#endregion
#region Private Properties
/// <summary>
/// Gets or sets the value of the user list (ab)
/// </summary>
private List<User>? UserList { get; set; }
public User? SelectedUser { get; set; }
public IEnumerable<UserGroup> UserGroups => Enum.GetValues<UserGroup>();
public List<Company> Companies { get; set; }
public List<UserGroup> SelectedCompanies { get; set; }
public List<string> SelectedCompanyTexts { get; set; } = new();
#endregion
#region Override OnAfterRenderAsync
/// <summary>
/// Ons the after render using the specified first render (a. beging, 01.04.2022)
/// </summary>
/// <param name="firstRender">The first render</param>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
await LoadUsers();
await base.OnAfterRenderAsync(firstRender);
}
#endregion
protected override void OnInitialized()
{
Companies = new List<Company>
{
new Company{Name = "Germany", ISO = "DE"},
new Company{Name = "USA", ISO = "US"},
new Company{Name = "England", ISO = "GB"},
new Company{Name = "Austria", ISO = "AT"}
};
SelectedCompanies = new List<UserGroup> {UserGroups.ElementAt(2)};
base.OnInitialized();
}
#region Private Method Chang
/// <summary>
/// Changs the context (a. beging, 01.04.2022)
/// </summary>
/// <param name="context">The context</param>
/// <param name="type">The type</param>
private void Chang(CellEditContext<User> context, UserType type)
{
context.Item.Type = type;
}
#endregion
#region Private Method LoadUsers
/// <summary>
/// Loads the users (a. beging, 01.04.2022)
/// </summary>
private async Task LoadUsers()
{
var usersR = await UserService.GetUsersAsync();
if (usersR.Success) UserList = usersR.Data;
await InvokeAsync(StateHasChanged);
}
#endregion
#region Private Method RowInserted
/// <summary>
/// Rows the inserted using the specified arg (a. beging, 01.04.2022)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowInserted(SavedRowItem<User, Dictionary<string, object>> arg)
{
var addUserR = await UserService.AddUserAsync(arg.Item);
if (!addUserR.Success)
{
//Todo Error Toast [01.04.22 - Andre Beging]
}
else
{
await LoadUsers();
}
}
#endregion
#region Private Method RowUpdated
/// <summary>
/// Rows the updated using the specified arg (a. beging, 01.04.2022)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowUpdated(SavedRowItem<User, Dictionary<string, object>> arg)
{
if (arg.Item?.Id == null || arg.Item.Id.Equals(Guid.Empty) || arg.Values?.Any() != true) return;
var result = await UserService.Update(arg.Item);
}
#endregion
private async Task SelectedChanged(List<UserGroup> arg)
{
}
}
public class Company
{
public string Name { get; set; }
public string ISO { get; set; }
}
}