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
@@ -6,7 +6,7 @@
<div class="cart-drawer @(IsCartOpen ? "is-open" : "") d-flex flex-column bg-white shadow-lg">
<div class="cart-header d-flex align-items-center justify-content-between p-4 border-bottom">
<h5 class="fw-bold m-0 text-dark tracking-tight" style="font-family: 'Inter', sans-serif; font-size: 1.1rem;">
YOUR CART (@CartItems.Sum(i => i.Quantity))
YOUR CART (@ShoppingCart.Items.Count())
</h5>
<button class="btn btn-sm text-dark p-1 border-0" @onclick="ToggleCart" type="button">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
@@ -17,7 +17,7 @@
</div>
<div class="cart-body flex-grow-1 overflow-y-auto p-4">
@if (!CartItems.Any())
@if (!ShoppingCart.Items.Any())
{
<div class="h-100 d-flex flex-column align-items-center justify-content-center text-muted py-5">
<svg class="mb-3 opacity-50" width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
@@ -29,22 +29,29 @@
else
{
<div class="d-flex flex-column gap-4">
@foreach (var item in CartItems)
@foreach (var item in ShoppingCart.Items)
{
<div class="cart-item d-flex gap-3 align-items-start pb-3 border-bottom-dashed">
<div class="cart-item-thumb bg-dark text-white-50 d-flex align-items-center justify-content-center px-2 text-center" style="width: 54px; height: 74px; font-size: 0.45rem; letter-spacing: 0.5px;">
[ COVER ]
@if (!string.IsNullOrWhiteSpace(item.Product!.ImageUrl))
{
<img src="@item.Product!.ImageUrl" class="img-fluid book-shadow" style="max-height: 240px; object-fit: contain;" alt="@item.Product.Name" />
}
else
{
@:[COVER]
}
</div>
<div class="flex-grow-1">
<h6 class="text-dark small fw-bold mb-0 text-truncate" style="max-width: 180px;">@item.Title</h6>
<p class="text-muted xx-small mb-2">by @item.Author</p>
<h6 class="text-dark small fw-bold mb-0 text-truncate" style="max-width: 180px;">@item.Product!.Name</h6>
<p class="text-muted xx-small mb-2">by @($"{item.Author!.Name} {item.Author.LastName}")</p>
<div class="d-flex align-items-center justify-content-between">
<div class="quantity-picker d-flex align-items-center border rounded-pill bg-light">
<button class="btn btn-sm py-0 px-2 text-dark border-0" @onclick="() => ChangeQuantity(item, -1)" type="button">-</button>
<span class="px-1 text-dark fw-medium" style="font-size: 0.75rem;">@item.Quantity</span>
<span class="px-1 text-dark fw-medium" style="font-size: 0.75rem;">@ShoppingCart.Items.FirstOrDefault(i => i.Price!.Id == item.Price!.Id)!.Quantity</span>
<button class="btn btn-sm py-0 px-2 text-dark border-0" @onclick="() => ChangeQuantity(item, 1)" type="button">+</button>
</div>
<span class="small fw-semibold text-dark">R @(item.Price * item.Quantity)</span>
<span class="small fw-semibold text-dark">R @(item.Price!.Amount * item.Quantity)</span>
</div>
</div>
<button class="btn text-muted p-0 border-0 mt-1 align-self-start" style="background: none;" @onclick="() => RemoveFromCart(item)" type="button">
@@ -56,7 +63,7 @@
}
</div>
@if (CartItems.Any())
@if (ShoppingCart.Items.Any())
{
<div class="cart-footer p-4 bg-light border-top mt-auto">
<div class="d-flex align-items-center justify-content-between mb-4">
@@ -156,7 +163,7 @@
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75">
<path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z M3 6h18 M16 10a4 4 0 0 1-8 0" />
</svg>
@if (CartItems.Any())
@if (ShoppingCart.Items.Any())
{
<span class="cart-badge">@ShoppingCart.Items.Count</span>
}
@@ -19,13 +19,6 @@ public partial class MainLayout(CartService cartService) : IDisposable
private bool IsSearchActive { get; set; } = false;
private bool IsCartOpen { get; set; } = false;
private List<CartItem> CartItems = new()
{
new CartItem { Id = 1, Title = "Letters from M/M (Paris)", Author = "M/M Paris", Price = 720, Quantity = 1 },
new CartItem { Id = 2, Title = "Daan Paans: Floating Signifiers", Author = "Daan Paans", Price = 540, Quantity = 1 },
new CartItem { Id = 3, Title = "Album Architectures, Maputo", Author = "Guedes Archive", Price = 350, Quantity = 1 }
};
protected override async Task OnInitializedAsync()
{
AuthState = await AuthStateProvider.GetAuthenticationStateAsync();
@@ -111,16 +104,23 @@ public partial class MainLayout(CartService cartService) : IDisposable
private void ToggleCart() => IsCartOpen = !IsCartOpen;
private void ChangeQuantity(CartItem item, int delta)
private async void ChangeQuantity(CartItem item, int delta)
{
item.Quantity += delta;
if (item.Quantity <= 0)
{
CartItems.Remove(item);
}
var peekQuantity = item.Quantity + delta;
if (peekQuantity < 1) return;
cartService.UpdateQuantity(item.Price!.Id, delta);
await cartService.SaveCartToStorageAsync();
}
private void RemoveFromCart(CartItem item) => CartItems.Remove(item);
private async void RemoveFromCart(CartItem item)
{
cartService.RemoveOneItem(item.Price!.Id);
await cartService.SaveCartToStorageAsync();
}
private decimal GetCartTotal() => ShoppingCart?.TotalPrice ?? 0.00m;
@@ -143,13 +143,4 @@ public partial class MainLayout(CartService cartService) : IDisposable
GC.SuppressFinalize(this);
}
public class CartItem
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public int Price { get; set; }
public int Quantity { get; set; }
}
}
@@ -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);
CartService.UpdateQuantity(CurrentPrice!.Id, Quantity);
Quantity = getCartItemQuantity(ShoppingCart, CurrentPrice.Id);
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();
}
}
@@ -1,5 +1,6 @@
using LiteCharms.Features.Browser;
using LiteCharms.Features.Hasher;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products.Models;
using MidrandBookshop.Services.ShoppingCart.Models;
@@ -30,7 +31,7 @@ public sealed class CartService(LocalStorageService localStorage)
public async Task SaveCartToStorageAsync() => await localStorage.SaveAsync(CartStorageKey, ShoppingCart);
public void AddItem(ProductPrice productPrice)
public void AddItem(ProductPrice productPrice, Product product, Author author)
{
var itemExists = false;
@@ -50,6 +51,8 @@ public sealed class CartService(LocalStorageService localStorage)
if (!itemExists)
ShoppingCart.Items.Add(new CartItem
{
Product = product,
Author = author,
Price = productPrice,
Amount = productPrice.Amount,
Quantity = 1,
@@ -59,16 +62,8 @@ public sealed class CartService(LocalStorageService localStorage)
NotifyStateChanged();
}
public void UpdateQuantity(long productPriceId, int newQuantity)
public void UpdateQuantity(long productPriceId, int delta)
{
if (newQuantity <= 0)
{
RemoveAllSameItem(productPriceId);
NotifyStateChanged();
return;
}
for (var i = 0; i < ShoppingCart.Items.Count; i++)
{
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
@@ -76,8 +71,8 @@ public sealed class CartService(LocalStorageService localStorage)
var oldQuantity = ShoppingCart.Items[i].Quantity;
var pricePerUnit = ShoppingCart.Items[i].Price!.Amount;
ShoppingCart.Items[i].Quantity = newQuantity;
ShoppingCart.Items[i].Amount = pricePerUnit * newQuantity;
ShoppingCart.Items[i].Quantity += delta;
ShoppingCart.Items[i].Amount = pricePerUnit * ShoppingCart.Items[i].Quantity;
break;
}
}
@@ -94,7 +89,9 @@ public sealed class CartService(LocalStorageService localStorage)
{
if (ShoppingCart.Items[i].Quantity <= 1)
{
ShoppingCart.Items.RemoveAt(i);
ShoppingCart.Items.Remove(ShoppingCart.Items[i]);
break;
}
else
{
@@ -1,12 +1,17 @@
using LiteCharms.Features.MidrandBooks.Products.Models;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products.Models;
namespace MidrandBookshop.Services.ShoppingCart.Models;
public sealed class CartItem
{
public Author? Author { get; set; }
public Product? Product { get; set; }
public ProductPrice? Price { get; set; }
public long Quantity { get; set; }
public int Quantity { get; set; }
public decimal Amount { get; set; }
}