Files
game-ideas/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Users/Users.razor.cs
Egamorf 1baa2a73fe
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m16s
Fix gitea issues (#54)
Co-authored-by: Maxime Adler <madler@sqli.com>
Reviewed-on: #54
2025-05-18 16:27:56 +02:00

173 lines
4.0 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;
StateHasChanged();
UserList = await UserGateway.GetUsers(FilterParams, CurrentPage);
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
StateHasChanged();
}
}
private async Task FetchRoles()
{
try
{
IsLoading = true;
StateHasChanged();
Roles = await UserGateway.GetRoles();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
StateHasChanged();
}
}
private async Task HandleSubmitUser(UserDto user)
{
try
{
IsLoading = true;
StateHasChanged();
await UserGateway.CreateUser(user);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
StateHasChanged();
UserAdd = new();
}
}
private async Task HandleUpdateUser(UserDto user)
{
try
{
IsLoading = true;
StateHasChanged();
await UserGateway.UpdateUser(user);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
StateHasChanged();
}
}
private async Task HandleRemoveUser()
{
Popup?.Close();
if (UserDelete?.Id == null)
{
return;
}
try
{
IsLoading = true;
StateHasChanged();
await UserGateway.DeleteUser(UserDelete.Id);
await FetchUsers();
}
catch (Exception)
{
throw;
}
finally
{
IsLoading = false;
StateHasChanged();
}
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();
}
}