Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9629d9ddf9 | |||
| 64e0fcba27 |
@@ -6,7 +6,7 @@
|
|||||||
<div class="cart-drawer @(IsCartOpen ? "is-open" : "") d-flex flex-column bg-white shadow-lg">
|
<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">
|
<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;">
|
<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>
|
</h5>
|
||||||
<button class="btn btn-sm text-dark p-1 border-0" @onclick="ToggleCart" type="button">
|
<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">
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="cart-body flex-grow-1 overflow-y-auto p-4">
|
<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">
|
<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">
|
<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
|
else
|
||||||
{
|
{
|
||||||
<div class="d-flex flex-column gap-4">
|
<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 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;">
|
<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>
|
||||||
<div class="flex-grow-1">
|
<div class="flex-grow-1">
|
||||||
<h6 class="text-dark small fw-bold mb-0 text-truncate" style="max-width: 180px;">@item.Title</h6>
|
<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</p>
|
<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="d-flex align-items-center justify-content-between">
|
||||||
<div class="quantity-picker d-flex align-items-center border rounded-pill bg-light">
|
<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>
|
<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>
|
<button class="btn btn-sm py-0 px-2 text-dark border-0" @onclick="() => ChangeQuantity(item, 1)" type="button">+</button>
|
||||||
</div>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
<button class="btn text-muted p-0 border-0 mt-1 align-self-start" style="background: none;" @onclick="() => RemoveFromCart(item)" type="button">
|
<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>
|
</div>
|
||||||
|
|
||||||
@if (CartItems.Any())
|
@if (ShoppingCart.Items.Any())
|
||||||
{
|
{
|
||||||
<div class="cart-footer p-4 bg-light border-top mt-auto">
|
<div class="cart-footer p-4 bg-light border-top mt-auto">
|
||||||
<div class="d-flex align-items-center justify-content-between mb-4">
|
<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">
|
<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" />
|
<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>
|
</svg>
|
||||||
@if (CartItems.Any())
|
@if (ShoppingCart.Items.Any())
|
||||||
{
|
{
|
||||||
<span class="cart-badge">@ShoppingCart.Items.Count</span>
|
<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 IsSearchActive { get; set; } = false;
|
||||||
private bool IsCartOpen { 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()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
AuthState = await AuthStateProvider.GetAuthenticationStateAsync();
|
AuthState = await AuthStateProvider.GetAuthenticationStateAsync();
|
||||||
@@ -111,16 +104,23 @@ public partial class MainLayout(CartService cartService) : IDisposable
|
|||||||
|
|
||||||
private void ToggleCart() => IsCartOpen = !IsCartOpen;
|
private void ToggleCart() => IsCartOpen = !IsCartOpen;
|
||||||
|
|
||||||
private void ChangeQuantity(CartItem item, int delta)
|
private async void ChangeQuantity(CartItem item, int delta)
|
||||||
{
|
{
|
||||||
item.Quantity += delta;
|
var peekQuantity = item.Quantity + delta;
|
||||||
if (item.Quantity <= 0)
|
|
||||||
{
|
if (peekQuantity < 1) return;
|
||||||
CartItems.Remove(item);
|
|
||||||
}
|
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;
|
private decimal GetCartTotal() => ShoppingCart?.TotalPrice ?? 0.00m;
|
||||||
|
|
||||||
@@ -143,13 +143,4 @@ public partial class MainLayout(CartService cartService) : IDisposable
|
|||||||
|
|
||||||
GC.SuppressFinalize(this);
|
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.Authors.Models;
|
||||||
using LiteCharms.Features.MidrandBooks.Products;
|
using LiteCharms.Features.MidrandBooks.Products;
|
||||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||||
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||||
using MidrandBookshop.Services.ShoppingCart;
|
using MidrandBookshop.Services.ShoppingCart;
|
||||||
|
|
||||||
namespace MidrandBookshop.Components.Pages;
|
namespace MidrandBookshop.Components.Pages;
|
||||||
@@ -13,8 +14,10 @@ public partial class ProductView : ComponentBase
|
|||||||
|
|
||||||
[Inject] private ProductService ProductService { get; set; } = default!;
|
[Inject] private ProductService ProductService { get; set; } = default!;
|
||||||
[Inject] private AuthorService AuthorService { 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 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 bool IsLoading { get; private set; } = true;
|
||||||
protected Product? CurrentProduct { get; private set; }
|
protected Product? CurrentProduct { get; private set; }
|
||||||
@@ -29,6 +32,9 @@ public partial class ProductView : ComponentBase
|
|||||||
|
|
||||||
protected Author? CurrentAuthor { get; private set; }
|
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()
|
protected override async Task OnParametersSetAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -85,20 +91,26 @@ public partial class ProductView : ComponentBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void IncreaseQty()
|
protected async void IncreaseQty()
|
||||||
{
|
{
|
||||||
Quantity++;
|
|
||||||
|
|
||||||
if (CurrentPrice is not null)
|
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)
|
if (CurrentPrice is not null)
|
||||||
{
|
{
|
||||||
CartService.AddItem(CurrentPrice);
|
if(ShoppingCart.Items.Any(p => p.Price!.Id == CurrentPrice.Id))
|
||||||
Quantity++;
|
{
|
||||||
|
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.Browser;
|
||||||
using LiteCharms.Features.Hasher;
|
using LiteCharms.Features.Hasher;
|
||||||
|
using LiteCharms.Features.MidrandBooks.Authors.Models;
|
||||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||||
using MidrandBookshop.Services.ShoppingCart.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 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;
|
var itemExists = false;
|
||||||
|
|
||||||
@@ -50,6 +51,8 @@ public sealed class CartService(LocalStorageService localStorage)
|
|||||||
if (!itemExists)
|
if (!itemExists)
|
||||||
ShoppingCart.Items.Add(new CartItem
|
ShoppingCart.Items.Add(new CartItem
|
||||||
{
|
{
|
||||||
|
Product = product,
|
||||||
|
Author = author,
|
||||||
Price = productPrice,
|
Price = productPrice,
|
||||||
Amount = productPrice.Amount,
|
Amount = productPrice.Amount,
|
||||||
Quantity = 1,
|
Quantity = 1,
|
||||||
@@ -59,16 +62,8 @@ public sealed class CartService(LocalStorageService localStorage)
|
|||||||
NotifyStateChanged();
|
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++)
|
for (var i = 0; i < ShoppingCart.Items.Count; i++)
|
||||||
{
|
{
|
||||||
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
|
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
|
||||||
@@ -76,8 +71,8 @@ public sealed class CartService(LocalStorageService localStorage)
|
|||||||
var oldQuantity = ShoppingCart.Items[i].Quantity;
|
var oldQuantity = ShoppingCart.Items[i].Quantity;
|
||||||
var pricePerUnit = ShoppingCart.Items[i].Price!.Amount;
|
var pricePerUnit = ShoppingCart.Items[i].Price!.Amount;
|
||||||
|
|
||||||
ShoppingCart.Items[i].Quantity = newQuantity;
|
ShoppingCart.Items[i].Quantity += delta;
|
||||||
ShoppingCart.Items[i].Amount = pricePerUnit * newQuantity;
|
ShoppingCart.Items[i].Amount = pricePerUnit * ShoppingCart.Items[i].Quantity;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,7 +89,9 @@ public sealed class CartService(LocalStorageService localStorage)
|
|||||||
{
|
{
|
||||||
if (ShoppingCart.Items[i].Quantity <= 1)
|
if (ShoppingCart.Items[i].Quantity <= 1)
|
||||||
{
|
{
|
||||||
ShoppingCart.Items.RemoveAt(i);
|
ShoppingCart.Items.Remove(ShoppingCart.Items[i]);
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
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;
|
namespace MidrandBookshop.Services.ShoppingCart.Models;
|
||||||
|
|
||||||
public sealed class CartItem
|
public sealed class CartItem
|
||||||
{
|
{
|
||||||
|
public Author? Author { get; set; }
|
||||||
|
|
||||||
|
public Product? Product { get; set; }
|
||||||
|
|
||||||
public ProductPrice? Price { get; set; }
|
public ProductPrice? Price { get; set; }
|
||||||
|
|
||||||
public long Quantity { get; set; }
|
public int Quantity { get; set; }
|
||||||
|
|
||||||
public decimal Amount { get; set; }
|
public decimal Amount { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user