🐛 Prevent pagination from breaking filtering #4

Merged
alexis.drai merged 2 commits from feat/implement-grid-and-drag-and-drop into main 2 years ago

@ -7,8 +7,7 @@ namespace blazor_lab.Components
public partial class InventoryGrid
{
[Parameter]
public List<InventoryModel> Inventory { get; set; }
public List<InventoryModel> Inventory { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList();
/// <summary>
/// Used by GetItemImageBase64 in this component, rather than calling our DataService every time.

@ -22,11 +22,13 @@ namespace blazor_lab.Components
private string searchQuery = "";
private int currentPage = 1;
private int pageSize = 10;
private readonly int pageSize = 10;
private void UpdateFilteredItems()
{
_filteredItems = string.IsNullOrEmpty(searchQuery) ? Items : Items.Where(i => i.DisplayName.ToLower().Contains(searchQuery.ToLower())).ToList();
_filteredItems = string.IsNullOrEmpty(searchQuery)
? Items
: Items.Where(i => i.DisplayName.ToLower().Contains(searchQuery.ToLower())).ToList();
SortItems();
VisibleItems = _filteredItems.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
@ -77,6 +79,7 @@ namespace blazor_lab.Components
{
searchQuery = e.Value.ToString();
await Task.Delay(250); // debounce the search to avoid excessive API requests
currentPage = 1; // Go back to page 1 when user is searching
UpdateFilteredItems();
}
private void OnSortOptionChanged(ChangeEventArgs e)

@ -5,11 +5,10 @@
<div class="side-by-side">
<div>
<h2>@Localizer["my_inventory"]</h2>
<InventoryGrid Inventory="FreshInventory" Items="Items" />
<InventoryGrid Items="Items" />
</div>
<div>
<h2>@Localizer["list_of_items"]</h2>
<InventoryList Items="Items" />
<InventoryList Items="Items" />
</div>
</div>

@ -20,7 +20,5 @@ namespace blazor_lab.Pages
{
Items = await DataApiService.All();
}
private List<InventoryModel> FreshInventory = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList();
}
}

@ -7,11 +7,14 @@ namespace blazor_lab.Services
public class DataApiService : IDataService
{
private readonly HttpClient _http;
private readonly string _apiBaseUrl;
public DataApiService(
HttpClient http)
HttpClient http,
IConfiguration config)
{
_http = http;
_apiBaseUrl = config.GetSection("CraftingApi")["BaseUrl"];
}
public async Task Add(ItemModel model)
@ -20,27 +23,27 @@ namespace blazor_lab.Services
var item = ItemFactory.Create(model);
// Save the data
await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item);
await _http.PostAsJsonAsync($"{_apiBaseUrl}/", item);
}
public async Task<int> Count()
{
return await _http.GetFromJsonAsync<int>("https://localhost:7234/api/Crafting/count");
return await _http.GetFromJsonAsync<int>($"{_apiBaseUrl}/count");
}
public async Task<List<Item>> All()
{
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all");
return await _http.GetFromJsonAsync<List<Item>>($"{_apiBaseUrl}/all");
}
public async Task<List<Item>> List(int currentPage, int pageSize)
{
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
return await _http.GetFromJsonAsync<List<Item>>($"{_apiBaseUrl}/?currentPage={currentPage}&pageSize={pageSize}");
}
public async Task<Item> GetById(int id)
{
return await _http.GetFromJsonAsync<Item>($"https://localhost:7234/api/Crafting/{id}");
return await _http.GetFromJsonAsync<Item>($"{_apiBaseUrl}/{id}");
}
public async Task Update(int id, ItemModel model)
@ -48,17 +51,17 @@ namespace blazor_lab.Services
// Get the item
var item = ItemFactory.Create(model);
await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item);
await _http.PutAsJsonAsync($"{_apiBaseUrl}/{id}", item);
}
public async Task Delete(int id)
{
await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}");
await _http.DeleteAsync($"{_apiBaseUrl}/{id}");
}
public async Task<List<CraftingRecipe>> GetRecipes()
{
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
return await _http.GetFromJsonAsync<List<CraftingRecipe>>($"{_apiBaseUrl}/recipe");
}
}
}

@ -7,7 +7,8 @@
},
"AllowedHosts": "*",
"CraftingApi": {
"BaseUrl": "https://localhost:7234"
}
"BaseUrl": "https://localhost:7234/api/Crafting"
},
"InventoryWidth": 6,
"InventoryHeight": 3,
}

Loading…
Cancel
Save