2024-03-16 10:46:06 +01:00
|
|
|
|
namespace TaikoWebUI.Pages;
|
2022-09-11 18:33:58 +02:00
|
|
|
|
|
2023-11-11 22:04:11 +01:00
|
|
|
|
public partial class Users
|
2022-09-11 18:33:58 +02:00
|
|
|
|
{
|
2024-06-05 17:33:37 +02:00
|
|
|
|
private UsersResponse? response = new();
|
2024-06-06 03:00:24 +02:00
|
|
|
|
private CancellationTokenSource? cts;
|
2024-06-05 17:33:37 +02:00
|
|
|
|
private int TotalPages { get; set; } = 0;
|
|
|
|
|
private bool isLoading = true;
|
|
|
|
|
private int currentPage = 1;
|
2024-06-06 03:34:13 +02:00
|
|
|
|
private int currentPageSize = 12;
|
2024-06-06 02:38:52 +02:00
|
|
|
|
private string? searchTerm = null;
|
2024-06-05 17:33:37 +02:00
|
|
|
|
|
|
|
|
|
private async Task GetUsersData()
|
|
|
|
|
{
|
|
|
|
|
isLoading = true;
|
2024-06-06 03:34:13 +02:00
|
|
|
|
response = await Client.GetFromJsonAsync<UsersResponse>($"api/Users?page={currentPage}&limit={currentPageSize}&searchTerm={searchTerm}");
|
2024-06-05 17:33:37 +02:00
|
|
|
|
response.ThrowIfNull();
|
|
|
|
|
|
|
|
|
|
TotalPages = response.TotalPages;
|
|
|
|
|
isLoading = false;
|
|
|
|
|
}
|
2022-09-11 18:33:58 +02:00
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
await base.OnInitializedAsync();
|
2024-05-25 19:12:30 +02:00
|
|
|
|
if (AuthService.LoginRequired && !AuthService.IsLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
await AuthService.LoginWithAuthToken();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 00:32:46 +02:00
|
|
|
|
if (AuthService.IsAdmin || !AuthService.LoginRequired)
|
|
|
|
|
{
|
2024-06-05 17:33:37 +02:00
|
|
|
|
await GetUsersData();
|
2024-05-17 00:32:46 +02:00
|
|
|
|
}
|
2022-09-11 18:33:58 +02:00
|
|
|
|
}
|
2024-06-05 17:33:37 +02:00
|
|
|
|
|
|
|
|
|
private async Task OnPageChange(int page)
|
|
|
|
|
{
|
|
|
|
|
currentPage = page;
|
|
|
|
|
await GetUsersData();
|
|
|
|
|
}
|
2024-06-06 03:00:24 +02:00
|
|
|
|
|
|
|
|
|
private async Task Debounce(Func<Task> action, int delayInMilliseconds)
|
|
|
|
|
{
|
|
|
|
|
// Cancel the previous task
|
|
|
|
|
cts?.Cancel();
|
|
|
|
|
|
|
|
|
|
// Create a new CancellationTokenSource
|
|
|
|
|
cts = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Wait for the delay
|
|
|
|
|
await Task.Delay(delayInMilliseconds, cts.Token);
|
|
|
|
|
|
|
|
|
|
// Execute the action
|
|
|
|
|
await action();
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
// Ignore the exception
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-06 02:38:52 +02:00
|
|
|
|
|
|
|
|
|
private async Task OnSearch(string search)
|
|
|
|
|
{
|
|
|
|
|
searchTerm = search;
|
|
|
|
|
currentPage = 1;
|
2024-06-06 03:00:24 +02:00
|
|
|
|
|
|
|
|
|
// Debounce the GetUsersData method
|
|
|
|
|
await Debounce(GetUsersData, 500); // 500 milliseconds delay
|
2024-06-06 02:38:52 +02:00
|
|
|
|
}
|
2022-09-11 18:33:58 +02:00
|
|
|
|
}
|