170 lines
7.3 KiB
C#
170 lines
7.3 KiB
C#
using LiteCharms.Features.MidrandBooks;
|
|
using LiteCharms.Features.MidrandBooks.AuthorBooks;
|
|
using LiteCharms.Features.MidrandBooks.Authors;
|
|
using LiteCharms.Features.MidrandBooks.Categories;
|
|
using LiteCharms.Features.MidrandBooks.Products;
|
|
using LiteCharms.Features.MidrandBooks.Products.Models;
|
|
using LiteCharms.Features.Models;
|
|
|
|
namespace MidrandBookshop.Components.Pages;
|
|
|
|
public partial class Home : ComponentBase
|
|
{
|
|
[Inject] private ProductService ProductService { get; set; } = default!;
|
|
[Inject] private BooksService BooksService { get; set; } = default!;
|
|
[Inject] private AuthorService AuthorService { get; set; } = default!;
|
|
[Inject] private CategoryService CategoryService { get; set; } = default!;
|
|
[Inject] private NavigationManager Navigation { get; set; } = default!;
|
|
|
|
[CascadingParameter] public string SharedSearchQuery { get; set; } = string.Empty;
|
|
[SupplyParameterFromQuery] public long? AuthorId { get; set; }
|
|
|
|
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 { get; set; } = ["All"];
|
|
private List<string> DynamicExtendedCategories { get; set; } = [];
|
|
|
|
private int ItemsPerPage = 12;
|
|
private int VisibleCount = 12;
|
|
|
|
private List<Product> ProductsCollection { get; set; } = [];
|
|
|
|
protected string? ActiveAuthorFilterName { get; private set; }
|
|
|
|
private Dictionary<long, decimal> ProductPriceCache { get; set; } = [];
|
|
private Dictionary<long, string> ProductPrimaryCategoryCache { get; set; } = [];
|
|
|
|
private IEnumerable<Product> FilteredData
|
|
{
|
|
get
|
|
{
|
|
var data = ProductsCollection.AsEnumerable();
|
|
|
|
// Category filtering restricts rendering solely when checking the open catalog
|
|
if (ActiveCategory != "All" && !AuthorId.HasValue)
|
|
data = data.Where(p => ProductPrimaryCategoryCache.ContainsKey(p.Id) &&
|
|
ProductPrimaryCategoryCache[p.Id] == ActiveCategory);
|
|
|
|
// Text matching is completely restricted from evaluating author metadata properties
|
|
if (!string.IsNullOrWhiteSpace(SharedSearchQuery))
|
|
{
|
|
var q = SharedSearchQuery.Trim();
|
|
|
|
data = data.Where(p => (p.Name ?? "").Contains(q, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
|
|
private IEnumerable<Product> PaginatedBooks => FilteredData.Take(VisibleCount);
|
|
|
|
private bool HasMoreItems => FilteredData.Count() > VisibleCount;
|
|
|
|
protected override async Task OnParametersSetAsync() => await LoadCatalogDataAsync();
|
|
|
|
private async Task LoadCatalogDataAsync()
|
|
{
|
|
ProductsCollection.Clear();
|
|
ProductPriceCache.Clear();
|
|
ProductPrimaryCategoryCache.Clear();
|
|
ActiveAuthorFilterName = null;
|
|
|
|
// Pipeline A: Extract scoped books directly associated with an ID token parameter
|
|
if (AuthorId.HasValue)
|
|
{
|
|
var authorResult = await AuthorService.GetAuthorAsync(AuthorId.Value);
|
|
|
|
if (authorResult.IsSuccess && authorResult.Value != null)
|
|
{
|
|
var author = authorResult.Value;
|
|
|
|
ActiveAuthorFilterName = author.PublisherType == PublisherTypes.Company && !string.IsNullOrWhiteSpace(author.Company)
|
|
? author.Company
|
|
: $"{author.Name} {author.LastName}".Trim();
|
|
}
|
|
|
|
var authorBooksResult = await BooksService.GetBooksByAuthorAsync(AuthorId.Value);
|
|
|
|
if (authorBooksResult.IsSuccess && authorBooksResult.Value != null)
|
|
{
|
|
foreach (var authorBook in authorBooksResult.Value)
|
|
{
|
|
if (authorBook.Product != null)
|
|
{
|
|
var product = authorBook.Product;
|
|
|
|
ProductsCollection.Add(product);
|
|
|
|
ProductPriceCache[product.Id] = product.Price?.Amount ?? 0m;
|
|
|
|
var categoryResult = await ProductService.GetProductCategoriesAsync(product.Id);
|
|
|
|
ProductPrimaryCategoryCache[product.Id] = (categoryResult.IsSuccess && categoryResult.Value.Length > 0)
|
|
? (categoryResult.Value[0].Name ?? "General")
|
|
: "General";
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Pipeline B: Safe structural fallback mapping utilizing the exact backend DateRange object setup
|
|
var selectionRange = new DateRange
|
|
{
|
|
From = new DateOnly(2020, 1, 1),
|
|
To = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(1)),
|
|
MaxRecords = 100
|
|
};
|
|
|
|
var allProductsResult = await ProductService.GetProductsAsync(0, selectionRange);
|
|
|
|
if (allProductsResult.IsSuccess && allProductsResult.Value != null)
|
|
{
|
|
ProductsCollection = allProductsResult.Value.ToList();
|
|
|
|
foreach (var product in ProductsCollection)
|
|
{
|
|
var priceResult = await ProductService.GetProductPriceAsync(product.Id);
|
|
ProductPriceCache[product.Id] = priceResult.IsSuccess ? priceResult.Value.Amount : 0m;
|
|
|
|
var categoryResult = await ProductService.GetProductCategoriesAsync(product.Id);
|
|
ProductPrimaryCategoryCache[product.Id] = (categoryResult.IsSuccess && categoryResult.Value.Length > 0)
|
|
? (categoryResult.Value[0].Name ?? "General")
|
|
: "General";
|
|
}
|
|
}
|
|
|
|
var categoriesResult = await CategoryService.GetCategoriesAsync();
|
|
|
|
if (categoriesResult.IsSuccess && categoriesResult.Value != null)
|
|
{
|
|
var cleanNames = categoriesResult.Value
|
|
.Select(c => c.Name)
|
|
.Where(n => !string.IsNullOrEmpty(n)).Cast<string>()
|
|
.ToList();
|
|
|
|
MainCategories = ["All", .. cleanNames.Take(3)];
|
|
DynamicExtendedCategories = [.. cleanNames.Skip(3)];
|
|
}
|
|
}
|
|
|
|
private void ClearAuthorFilter() => Navigation.NavigateTo("/");
|
|
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; }
|
|
} |