Moved Home page code to code-behind

This commit is contained in:
Khwezi Mngoma
2026-05-30 10:17:47 +02:00
parent 0a3dd61ce0
commit 07749bd68c
4 changed files with 125 additions and 130 deletions
-130
View File
@@ -217,133 +217,3 @@
<polyline points="5 12 12 5 19 12"></polyline>
</svg>
</a>
@code {
public enum ViewMode { Grid, List }
private ViewMode CurrentViewMode = ViewMode.Grid;
[CascadingParameter]
public string SharedSearchQuery { get; set; } = string.Empty;
private string ActiveCategory = "All";
private bool ShowExpandedCategories = false;
private bool ShowFilterMenu = false;
private string SelectedSortOption = "default";
private string ActivePriceFilter = "all";
private bool OnlyShowNew = false;
private List<string> MainCategories = new() { "All", "Graphic Design", "Product Design", "Architecture" };
private List<string> DynamicExtendedCategories = new();
private int ItemsPerPage = 12;
private int VisibleCount = 12;
public class BookItem
{
public long Id { get; set; } // Refactored to hold unique record indices of type long
public string Title { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
public bool IsNew { get; set; }
public string Isbn { get; set; } = string.Empty;
}
private List<BookItem> BooksCollection = new();
private IEnumerable<BookItem> FilteredData
{
get
{
var data = BooksCollection.AsEnumerable();
if (!string.IsNullOrWhiteSpace(SharedSearchQuery))
{
var q = SharedSearchQuery.Trim();
data = data.Where(b =>
b.Title.Contains(q, StringComparison.OrdinalIgnoreCase) ||
b.Author.Contains(q, StringComparison.OrdinalIgnoreCase) ||
b.Isbn.Contains(q, StringComparison.OrdinalIgnoreCase)
);
}
if (ActiveCategory != "All")
{
data = data.Where(b => b.Category.Equals(ActiveCategory, StringComparison.OrdinalIgnoreCase));
}
if (OnlyShowNew) { data = data.Where(b => b.IsNew); }
data = ActivePriceFilter switch
{
"under-500" => data.Where(b => b.Price < 500),
"500-1000" => data.Where(b => b.Price >= 500 && b.Price <= 1000),
"over-1000" => data.Where(b => b.Price > 1000),
_ => data
};
return data;
}
}
private IEnumerable<BookItem> SortedAndFilteredBooks => SelectedSortOption switch
{
"price-low" => FilteredData.OrderBy(b => b.Price),
"price-high" => FilteredData.OrderByDescending(b => b.Price),
"title-asc" => FilteredData.OrderBy(b => b.Title),
_ => FilteredData
};
private IEnumerable<BookItem> PaginatedBooks => SortedAndFilteredBooks.Take(VisibleCount);
private int TotalFilteredCount => FilteredData.Count();
private bool HasMoreItems => VisibleCount < TotalFilteredCount;
protected override void OnInitialized()
{
var extraSourceCategories = new[] { "Fine Arts", "Science", "Photography", "Typography", "Interior Design", "Industrialism", "Fashion", "Curation Studies" };
DynamicExtendedCategories.AddRange(extraSourceCategories);
// Updated mock items to supply long IDs matching your screenshot items
BooksCollection.Add(new BookItem { Id = 1L, Title = "Letters from M/M (Paris)", Author = "M/M Paris", Price = 720, Category = "Graphic Design", IsNew = true, Isbn = "9782915173" });
BooksCollection.Add(new BookItem { Id = 2L, Title = "Daan Paans: Floating Signifiers", Author = "Daan Paans", Price = 540, Category = "Product Design", IsNew = true, Isbn = "9789492051" });
BooksCollection.Add(new BookItem { Id = 3L, Title = "Album Architectures, Maputo", Author = "Guedes Archive", Price = 350, Category = "Architecture", IsNew = true, Isbn = "9780620751" });
var designPrefixes = new[] { "Minimalist", "Monolithic", "Architectural", "Japanese", "Scandinavian" };
var designNouns = new[] { "Structures", "Typologies", "Forms & Spaces", "Systems Matrix", "Graphic Ephemera" };
var designers = new[] { "J. Morrison", "K. Fujita", "Studio Bouroullec", "Es Devlin", "Kenya Hara" };
var entireCategoryPool = MainCategories.Concat(DynamicExtendedCategories).Where(c => c != "All").ToArray();
var random = new Random(42);
for (int i = 4; i <= 60; i++)
{
BooksCollection.Add(new BookItem
{
Id = (long)i,
Title = $"{designPrefixes[random.Next(designPrefixes.Length)]} {designNouns[random.Next(designNouns.Length)]} (Vol. {random.Next(1, 4)})",
Author = designers[random.Next(designers.Length)],
Price = random.Next(25, 135) * 10,
Category = entireCategoryPool[random.Next(entireCategoryPool.Length)],
IsNew = random.NextDouble() > 0.7,
Isbn = $"978000000{i}"
});
}
}
// Handles the explicit page transition routing
private void NavigateToProduct(long id)
{
Navigation.NavigateTo($"/product/{id}");
}
private void SetViewMode(ViewMode targetMode) => CurrentViewMode = targetMode;
private void SelectCategory(string categoryName) { ActiveCategory = categoryName; VisibleCount = ItemsPerPage; }
private void ToggleExtraCategories() => ShowExpandedCategories = !ShowExpandedCategories;
private void ToggleFilterMenu() => ShowFilterMenu = !ShowFilterMenu;
private void ChangeSort(string sortOption) => SelectedSortOption = sortOption;
private void ChangePriceFilter(string priceBracket) { ActivePriceFilter = priceBracket; VisibleCount = ItemsPerPage; }
private void ToggleNewArrivalsOnly(ChangeEventArgs e) { OnlyShowNew = e.Value is bool b && b; VisibleCount = ItemsPerPage; }
private void ResetFilters() { SelectedSortOption = "default"; ActivePriceFilter = "all"; OnlyShowNew = false; VisibleCount = ItemsPerPage; }
private void LoadNextPage() { if (HasMoreItems) VisibleCount += ItemsPerPage; }
}
@@ -0,0 +1,112 @@
using LiteCharms.Features.MidrandBooks.AuthorBooks;
using MidrandBookshop.Models;
namespace MidrandBookshop.Components.Pages;
public partial class Home(BooksService booksService)
{
[CascadingParameter]
public string SharedSearchQuery { get; set; } = string.Empty;
public enum ViewMode { Grid, List }
private ViewMode CurrentViewMode = ViewMode.Grid;
private string ActiveCategory = "All";
private bool ShowExpandedCategories = false;
private bool ShowFilterMenu = false;
private string SelectedSortOption = "default";
private string ActivePriceFilter = "all";
private bool OnlyShowNew = false;
private List<string> MainCategories = ["All", "Graphic Design", "Product Design", "Architecture"];
private List<string> DynamicExtendedCategories = [];
private int ItemsPerPage = 12;
private int VisibleCount = 12;
private List<BookItem> BooksCollection = [];
private IEnumerable<BookItem> FilteredData
{
get
{
var data = BooksCollection.AsEnumerable();
if (!string.IsNullOrWhiteSpace(SharedSearchQuery))
{
var q = SharedSearchQuery.Trim();
data = data.Where(b =>
b.Title.Contains(q, StringComparison.OrdinalIgnoreCase) ||
b.Author.Contains(q, StringComparison.OrdinalIgnoreCase) ||
b.Isbn.Contains(q, StringComparison.OrdinalIgnoreCase)
);
}
if (ActiveCategory != "All")
{
data = data.Where(b => b.Category.Equals(ActiveCategory, StringComparison.OrdinalIgnoreCase));
}
if (OnlyShowNew) { data = data.Where(b => b.IsNew); }
data = ActivePriceFilter switch
{
"under-500" => data.Where(b => b.Price < 500),
"500-1000" => data.Where(b => b.Price >= 500 && b.Price <= 1000),
"over-1000" => data.Where(b => b.Price > 1000),
_ => data
};
return data;
}
}
private IEnumerable<BookItem> SortedAndFilteredBooks => SelectedSortOption switch
{
"price-low" => FilteredData.OrderBy(b => b.Price),
"price-high" => FilteredData.OrderByDescending(b => b.Price),
"title-asc" => FilteredData.OrderBy(b => b.Title),
_ => FilteredData
};
private IEnumerable<BookItem> PaginatedBooks => SortedAndFilteredBooks.Take(VisibleCount);
private int TotalFilteredCount => FilteredData.Count();
private bool HasMoreItems => VisibleCount < TotalFilteredCount;
protected override void OnInitialized()
{
var extraSourceCategories = new[] { "Fine Arts", "Science", "Photography", "Typography", "Interior Design", "Industrialism", "Fashion", "Curation Studies" };
DynamicExtendedCategories.AddRange(extraSourceCategories);
// Updated mock items to supply long IDs matching your screenshot items
BooksCollection.Add(new BookItem { Id = 1L, Title = "Letters from M/M (Paris)", Author = "M/M Paris", Price = 720, Category = "Graphic Design", IsNew = true, Isbn = "9782915173" });
BooksCollection.Add(new BookItem { Id = 2L, Title = "Daan Paans: Floating Signifiers", Author = "Daan Paans", Price = 540, Category = "Product Design", IsNew = true, Isbn = "9789492051" });
BooksCollection.Add(new BookItem { Id = 3L, Title = "Album Architectures, Maputo", Author = "Guedes Archive", Price = 350, Category = "Architecture", IsNew = true, Isbn = "9780620751" });
var designPrefixes = new[] { "Minimalist", "Monolithic", "Architectural", "Japanese", "Scandinavian" };
var designNouns = new[] { "Structures", "Typologies", "Forms & Spaces", "Systems Matrix", "Graphic Ephemera" };
var designers = new[] { "J. Morrison", "K. Fujita", "Studio Bouroullec", "Es Devlin", "Kenya Hara" };
var entireCategoryPool = MainCategories.Concat(DynamicExtendedCategories).Where(c => c != "All").ToArray();
var random = new Random(42);
for (int i = 4; i <= 60; i++)
{
BooksCollection.Add(new BookItem
{
Id = (long)i,
Title = $"{designPrefixes[random.Next(designPrefixes.Length)]} {designNouns[random.Next(designNouns.Length)]} (Vol. {random.Next(1, 4)})",
Author = designers[random.Next(designers.Length)],
Price = random.Next(25, 135) * 10,
Category = entireCategoryPool[random.Next(entireCategoryPool.Length)],
IsNew = random.NextDouble() > 0.7,
Isbn = $"978000000{i}"
});
}
}
private void NavigateToProduct(long id) => Navigation.NavigateTo($"/product/{id}");
private void SetViewMode(ViewMode targetMode) => CurrentViewMode = targetMode;
private void SelectCategory(string categoryName) { ActiveCategory = categoryName; VisibleCount = ItemsPerPage; }
private void ToggleExtraCategories() => ShowExpandedCategories = !ShowExpandedCategories;
private void ToggleFilterMenu() => ShowFilterMenu = !ShowFilterMenu;
private void ChangeSort(string sortOption) => SelectedSortOption = sortOption;
private void ChangePriceFilter(string priceBracket) { ActivePriceFilter = priceBracket; VisibleCount = ItemsPerPage; }
private void ToggleNewArrivalsOnly(ChangeEventArgs e) { OnlyShowNew = e.Value is bool b && b; VisibleCount = ItemsPerPage; }
private void ResetFilters() { SelectedSortOption = "default"; ActivePriceFilter = "all"; OnlyShowNew = false; VisibleCount = ItemsPerPage; }
private void LoadNextPage() { if (HasMoreItems) VisibleCount += ItemsPerPage; }
}
+1
View File
@@ -51,6 +51,7 @@
<!-- Shared Global Usings -->
<ItemGroup>
<Using Include="Blazored.Toast" />
<Using Include="Microsoft.AspNetCore.Components" />
</ItemGroup>
</Project>
+12
View File
@@ -0,0 +1,12 @@
namespace MidrandBookshop.Models;
public class BookItem
{
public long Id { get; set; } // Refactored to hold unique record indices of type long
public string Title { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
public bool IsNew { get; set; }
public string Isbn { get; set; } = string.Empty;
}