diff --git a/blazor_lab/Components/InventoryGrid.razor.cs b/blazor_lab/Components/InventoryGrid.razor.cs index 13696f3..51c4e58 100644 --- a/blazor_lab/Components/InventoryGrid.razor.cs +++ b/blazor_lab/Components/InventoryGrid.razor.cs @@ -7,8 +7,7 @@ namespace blazor_lab.Components public partial class InventoryGrid { - [Parameter] - public List Inventory { get; set; } + public List Inventory { get; set; } = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); /// /// Used by GetItemImageBase64 in this component, rather than calling our DataService every time. diff --git a/blazor_lab/Components/InventoryList.razor.cs b/blazor_lab/Components/InventoryList.razor.cs index 3a01c71..71334eb 100644 --- a/blazor_lab/Components/InventoryList.razor.cs +++ b/blazor_lab/Components/InventoryList.razor.cs @@ -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) diff --git a/blazor_lab/Pages/Inventory.razor b/blazor_lab/Pages/Inventory.razor index 8db9eac..74772ef 100644 --- a/blazor_lab/Pages/Inventory.razor +++ b/blazor_lab/Pages/Inventory.razor @@ -5,11 +5,10 @@

@Localizer["my_inventory"]

- - +

@Localizer["list_of_items"]

- +
\ No newline at end of file diff --git a/blazor_lab/Pages/Inventory.razor.cs b/blazor_lab/Pages/Inventory.razor.cs index 759fcfb..f84cfee 100644 --- a/blazor_lab/Pages/Inventory.razor.cs +++ b/blazor_lab/Pages/Inventory.razor.cs @@ -20,7 +20,5 @@ namespace blazor_lab.Pages { Items = await DataApiService.All(); } - - private List FreshInventory = Enumerable.Range(1, 18).Select(_ => new InventoryModel()).ToList(); } } diff --git a/blazor_lab/Services/DataApiService.cs b/blazor_lab/Services/DataApiService.cs index 4b90f17..2e5c4ab 100644 --- a/blazor_lab/Services/DataApiService.cs +++ b/blazor_lab/Services/DataApiService.cs @@ -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 Count() { - return await _http.GetFromJsonAsync("https://localhost:7234/api/Crafting/count"); + return await _http.GetFromJsonAsync($"{_apiBaseUrl}/count"); } public async Task> All() { - return await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/all"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/all"); } public async Task> List(int currentPage, int pageSize) { - return await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/?currentPage={currentPage}&pageSize={pageSize}"); } public async Task GetById(int id) { - return await _http.GetFromJsonAsync($"https://localhost:7234/api/Crafting/{id}"); + return await _http.GetFromJsonAsync($"{_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> GetRecipes() { - return await _http.GetFromJsonAsync>("https://localhost:7234/api/Crafting/recipe"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/recipe"); } } } diff --git a/blazor_lab/appsettings.json b/blazor_lab/appsettings.json index 2334eaa..b1cc657 100644 --- a/blazor_lab/appsettings.json +++ b/blazor_lab/appsettings.json @@ -7,7 +7,8 @@ }, "AllowedHosts": "*", "CraftingApi": { - "BaseUrl": "https://localhost:7234" - } - + "BaseUrl": "https://localhost:7234/api/Crafting" + }, + "InventoryWidth": 6, + "InventoryHeight": 3, }