Wired up CartDrawel and ProductView to cart service and local storage
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-06-10 23:01:21 +02:00
parent 3bce80c963
commit 64e0fcba27
5 changed files with 85 additions and 61 deletions
@@ -3,6 +3,7 @@ using LiteCharms.Features.MidrandBooks.Authors;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products;
using LiteCharms.Features.MidrandBooks.Products.Models;
using Microsoft.AspNetCore.Cors.Infrastructure;
using MidrandBookshop.Services.ShoppingCart;
namespace MidrandBookshop.Components.Pages;
@@ -13,8 +14,10 @@ public partial class ProductView : ComponentBase
[Inject] private ProductService ProductService { get; set; } = default!;
[Inject] private AuthorService AuthorService { get; set; } = default!;
[Inject] private CartService CartService { get; set; } = default!;
[Inject] private NavigationManager Navigation { get; set; } = default!;
[Inject] private CartService CartService { get; set; } = default!;
protected Services.ShoppingCart.Models.Cart ShoppingCart => CartService?.ShoppingCart!;
protected bool IsLoading { get; private set; } = true;
protected Product? CurrentProduct { get; private set; }
@@ -29,6 +32,9 @@ public partial class ProductView : ComponentBase
protected Author? CurrentAuthor { get; private set; }
private static Func<Services.ShoppingCart.Models.Cart, long, int> getCartItemQuantity = (shoppingCart, productPriceId) =>
shoppingCart.Items.FirstOrDefault(p => p.Price!.Id == productPriceId)?.Quantity ?? 1;
protected override async Task OnParametersSetAsync()
{
try
@@ -85,20 +91,26 @@ public partial class ProductView : ComponentBase
}
}
protected void IncreaseQty()
protected async void IncreaseQty()
{
Quantity++;
if (CurrentPrice is not null)
CartService.UpdateQuantity(CurrentPrice!.Id, Quantity);
}
protected void DecreaseQty()
{
if (Quantity > 1)
{
Quantity--;
CartService.UpdateQuantity(CurrentPrice!.Id, 1);
Quantity = getCartItemQuantity(ShoppingCart, CurrentPrice.Id);
CartService.UpdateQuantity(CurrentPrice!.Id, Quantity);
await CartService.SaveCartToStorageAsync();
}
}
protected async void DecreaseQty()
{
if (Quantity >= 1)
{
CartService.UpdateQuantity(CurrentPrice!.Id, -1);
Quantity = getCartItemQuantity(ShoppingCart, CurrentPrice.Id);
await CartService.SaveCartToStorageAsync();
}
}
@@ -108,8 +120,20 @@ public partial class ProductView : ComponentBase
if (CurrentPrice is not null)
{
CartService.AddItem(CurrentPrice);
Quantity++;
if(ShoppingCart.Items.Any(p => p.Price!.Id == CurrentPrice.Id))
{
CartService.UpdateQuantity(CurrentPrice.Id, 1);
await CartService.SaveCartToStorageAsync();
return;
}
CartService.AddItem(CurrentPrice, CurrentProduct, CurrentAuthor!);
Quantity = getCartItemQuantity(ShoppingCart, CurrentPrice.Id);
await CartService.SaveCartToStorageAsync();
}
}