Files
game-ideas/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Users/Users.razor.cs
Maxime Adler 7a7863f789
All checks were successful
Game Ideas build for PR / build_test (pull_request) Successful in 43s
Display no element and nb element (#47)
2025-05-15 14:34:55 +02:00

164 lines
3.7 KiB
C#

using GameIdeas.BlazorApp.Pages.Users.Filters;
using GameIdeas.BlazorApp.Pages.Users.Gateways;
using GameIdeas.BlazorApp.Shared.Components.Popup;
using GameIdeas.Shared.Constants;
using GameIdeas.Shared.Dto;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
namespace GameIdeas.BlazorApp.Pages.Users;
public partial class Users
{
[Inject] private IUserGateway UserGateway { get; set; } = default!;
[Inject] private AuthenticationStateProvider StateProvider { get; set; } = default!;
[Inject] private NavigationManager NavigationManager { get; set; } = default!;
private Popup? Popup;
private bool IsLoading = false;
private readonly UserFilterParams FilterParams = new();
private UserListDto UserList = new();
private IEnumerable<RoleDto> Roles = [];
private readonly int CurrentPage = 1;
private UserDto UserAdd = new();
private UserDto? UserDelete;
private string? currentUserId;
protected override async Task OnInitializedAsync()
{
var authState = await StateProvider.GetAuthenticationStateAsync();
currentUserId = authState.User.FindFirstValue(ClaimTypes.Sid);
if (authState.User.FindFirstValue(ClaimTypes.Role) != GlobalConstants.ADMINISTRATOR || string.IsNullOrEmpty(currentUserId))
{
NavigationManager.NavigateTo("/Unauthorized");
return;
}
await FetchUsers();
await FetchRoles();
await base.OnInitializedAsync();
}
private async Task FetchUsers(bool displayLoading = true)
{
try
{
IsLoading = displayLoading;
UserList = await UserGateway.GetUsers(FilterParams, CurrentPage);
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
}
private async Task FetchRoles()
{
try
{
IsLoading = true;
Roles = await UserGateway.GetRoles();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
}
private async Task HandleSubmitUser(UserDto user)
{
try
{
IsLoading = true;
await UserGateway.CreateUser(user);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
UserAdd = new();
}
private async Task HandleUpdateUser(UserDto user)
{
try
{
IsLoading = true;
await UserGateway.UpdateUser(user);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
}
private async Task HandleRemoveUser()
{
Popup?.Close();
if (UserDelete?.Id == null)
{
return;
}
try
{
IsLoading = true;
await UserGateway.DeleteUser(UserDelete.Id);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
}
UserDelete = null;
}
private void HandleResetUser(UserDto args)
{
UserAdd = new();
}
private async Task HandleFilterChanged()
{
await FetchUsers(false);
}
private void HandleCancelPopupClicked()
{
Popup?.Close();
}
private void HandleOpenConfirmationPopup(UserDto user)
{
UserDelete = user;
Popup?.Open();
}
}