Search when input value changes
This commit is contained in:
parent
682346521e
commit
d79f141707
@ -10,7 +10,7 @@
|
|||||||
<MudGrid Class="my-8">
|
<MudGrid Class="my-8">
|
||||||
@if (!AuthService.LoginRequired || (AuthService.LoginRequired && AuthService.IsAdmin)) {
|
@if (!AuthService.LoginRequired || (AuthService.LoginRequired && AuthService.IsAdmin)) {
|
||||||
<MudItem xs="12">
|
<MudItem xs="12">
|
||||||
<MudInput Style="width: 100%" @bind-Value="searchTerm" Clearable="true" FullWidth="true" TextChanged="(term => OnSearch(term))" Margin="Margin.Dense" Placeholder="@Localizer["User ID, Name, or Access Code"]" Label="@Localizer["Search"]" Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" />
|
<MudInput Immediate="true" Style="width: 100%" @bind-Value="searchTerm" Clearable="true" FullWidth="true" TextChanged="(term => OnSearch(term))" Margin="Margin.Dense" Placeholder="@Localizer["User ID, Name, or Access Code"]" Label="@Localizer["Search"]" Variant="Variant.Outlined" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" />
|
||||||
</MudItem>
|
</MudItem>
|
||||||
@if (isLoading || response == null)
|
@if (isLoading || response == null)
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@ public partial class Users
|
|||||||
{
|
{
|
||||||
private UsersResponse? response = new();
|
private UsersResponse? response = new();
|
||||||
|
|
||||||
|
private CancellationTokenSource? cts;
|
||||||
private int TotalPages { get; set; } = 0;
|
private int TotalPages { get; set; } = 0;
|
||||||
private bool isLoading = true;
|
private bool isLoading = true;
|
||||||
private int currentPage = 1;
|
private int currentPage = 1;
|
||||||
@ -39,11 +40,35 @@ public partial class Users
|
|||||||
currentPage = page;
|
currentPage = page;
|
||||||
await GetUsersData();
|
await GetUsersData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task OnSearch(string search)
|
private async Task OnSearch(string search)
|
||||||
{
|
{
|
||||||
searchTerm = search;
|
searchTerm = search;
|
||||||
currentPage = 1;
|
currentPage = 1;
|
||||||
await GetUsersData();
|
|
||||||
|
// Debounce the GetUsersData method
|
||||||
|
await Debounce(GetUsersData, 500); // 500 milliseconds delay
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user