Implemented AddToCart functionality on home page
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
namespace MidrandBookshop.Components;
|
||||
using LiteCharms.Features.MidrandBooks.AuthorBooks;
|
||||
using LiteCharms.Features.MidrandBooks.Authors;
|
||||
using LiteCharms.Features.MidrandBooks.Payments;
|
||||
using LiteCharms.Features.MidrandBooks.Products;
|
||||
|
||||
namespace MidrandBookshop.Components;
|
||||
|
||||
public partial class BookCard
|
||||
{
|
||||
@@ -11,4 +16,29 @@ public partial class BookCard
|
||||
[Parameter] public string BookImageUrl { get; set; } = string.Empty;
|
||||
|
||||
[Parameter] public EventCallback OnCardClick { get; set; }
|
||||
|
||||
[Inject] private CartService CartService { get; set; } = default!;
|
||||
[Inject] private ProductService ProductService { get; set; } = default!;
|
||||
[Inject] private AuthorService AuthorService { get; set; } = default!;
|
||||
[Inject] private BooksService BooksService { get; set; } = default!;
|
||||
[Inject] private IToastService ToastService { get; set; } = default!;
|
||||
[Inject] private CancellationToken CancellationToken { get; set; } = default!;
|
||||
|
||||
private async Task HandleAddToCart()
|
||||
{
|
||||
try
|
||||
{
|
||||
var bookFetch = await BooksService.GetBookByProductIdAsync(Id, CancellationToken);
|
||||
var authorFetch = await AuthorService.GetAuthorAsync(bookFetch.Value.AuthorId, CancellationToken);
|
||||
var productPriceFetch = await ProductService.GetProductPriceAsync(Id, CancellationToken);
|
||||
|
||||
CartService.AddItem(productPriceFetch.Value, bookFetch.Value.Product!, authorFetch.Value);
|
||||
|
||||
ToastService.ShowSuccess($"Added '{Title}' to your order.", "Add To Cart");
|
||||
}
|
||||
catch
|
||||
{
|
||||
ToastService.ShowError("Could not update cart. Please try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user