1
0
mirror of synced 2025-01-19 00:04:05 +01:00

Search when input value changes

This commit is contained in:
shiibe 2024-06-05 21:00:24 -04:00
parent 682346521e
commit d79f141707
2 changed files with 27 additions and 2 deletions

View File

@ -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)
{ {

View File

@ -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
} }
} }