diff --git a/ShopOnline.Web/Pages/ProductsBase.cs b/ShopOnline.Web/Pages/ProductsBase.cs index 76556b6..0b1a9c4 100644 --- a/ShopOnline.Web/Pages/ProductsBase.cs +++ b/ShopOnline.Web/Pages/ProductsBase.cs @@ -10,11 +10,32 @@ namespace ShopOnline.Web.Pages [Inject] public IProductService ProductService { get; set; } + [Inject] + public IShoppingCartService ShoppingCartService { get; set; } + public IEnumerable Products { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + + public string ErrorMessage { get; set; } + protected override async Task OnInitializedAsync() { - Products = await ProductService.GetItems(); + try + { + Products = await ProductService.GetItems(); + + var shoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); + var totalQuantity = shoppingCartItems.Sum(i => i.Quantity); + + ShoppingCartService.RaiseEventOnShoppingCartChanged(totalQuantity); + } + catch (Exception ex) + { + ErrorMessage = ex.Message; + } + } protected IOrderedEnumerable> GetGroupedProductsByCategory() diff --git a/ShopOnline.Web/Pages/ShoppingCart.razor.css b/ShopOnline.Web/Pages/ShoppingCart.razor.css index 4526ba5..83f0c01 100644 --- a/ShopOnline.Web/Pages/ShoppingCart.razor.css +++ b/ShopOnline.Web/Pages/ShoppingCart.razor.css @@ -1,4 +1,3 @@ .update-qty{ display: none; - background-color: green; } \ No newline at end of file diff --git a/ShopOnline.Web/Pages/ShoppingCartBase.cs b/ShopOnline.Web/Pages/ShoppingCartBase.cs index 055cb01..680f057 100644 --- a/ShopOnline.Web/Pages/ShoppingCartBase.cs +++ b/ShopOnline.Web/Pages/ShoppingCartBase.cs @@ -25,7 +25,7 @@ namespace ShopOnline.Web.Pages try { ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); - CalculateCartSummaryTotals(); + CartChanged(); } catch (Exception ex) { @@ -38,7 +38,7 @@ namespace ShopOnline.Web.Pages var cartItemDto = await ShoppingCartService.DeleteItem(id); RemoveCartItem(id); - CalculateCartSummaryTotals(); + CartChanged(); } @@ -57,7 +57,7 @@ namespace ShopOnline.Web.Pages var returnedUpdateItemDto = await this.ShoppingCartService.UpdateQuantity(updateItemDto); UpdateItemTotalPrice(returnedUpdateItemDto); - CalculateCartSummaryTotals(); + CartChanged(); await MakeUpdateQuantityButtonVisible(id, false); } @@ -128,6 +128,11 @@ namespace ShopOnline.Web.Pages ShoppingCartItems.Remove(CartItemDto); } + private void CartChanged() + { + CalculateCartSummaryTotals(); + ShoppingCartService.RaiseEventOnShoppingCartChanged(TotalQuantity); + } } } diff --git a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs index 0a9fbaa..402d786 100644 --- a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs +++ b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs @@ -8,5 +8,8 @@ namespace ShopOnline.Web.Services.Contracts Task AddItem(CartItemToAddDto cartItemToAddDto); Task DeleteItem(int id); Task UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto); + + event Action OnShoppingCartChanged; + void RaiseEventOnShoppingCartChanged(int totalQuantity); } } diff --git a/ShopOnline.Web/Services/ShoppingCartService.cs b/ShopOnline.Web/Services/ShoppingCartService.cs index 542c95b..f41965c 100644 --- a/ShopOnline.Web/Services/ShoppingCartService.cs +++ b/ShopOnline.Web/Services/ShoppingCartService.cs @@ -15,6 +15,9 @@ namespace ShopOnline.Web.Services { this.httpClient = httpClient; } + + public event Action OnShoppingCartChanged; + public async Task AddItem(CartItemToAddDto cartItemToAddDto) { try @@ -85,6 +88,14 @@ namespace ShopOnline.Web.Services } } + public void RaiseEventOnShoppingCartChanged(int totalQuantity) + { + if(OnShoppingCartChanged != null) + { + OnShoppingCartChanged.Invoke(totalQuantity); + } + } + public async Task UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto) { try diff --git a/ShopOnline.Web/Shared/CartMenu.razor b/ShopOnline.Web/Shared/CartMenu.razor new file mode 100644 index 0000000..e5fb982 --- /dev/null +++ b/ShopOnline.Web/Shared/CartMenu.razor @@ -0,0 +1,26 @@ +@implements IDisposable +@inject IShoppingCartService shoppingCartService + + +  Cart + @shoppingCartItemCount + + +@code { + private int shoppingCartItemCount = 0; + + protected override void OnInitialized() + { + shoppingCartService.OnShoppingCartChanged += ShoppingCartChanged; //subscribe to event + } + + protected void ShoppingCartChanged(int totalQuantity){ + shoppingCartItemCount = totalQuantity; + StateHasChanged(); + } + + void IDisposable.Dispose() + { + shoppingCartService.OnShoppingCartChanged -= ShoppingCartChanged; //subscribe from event + } +} diff --git a/ShopOnline.Web/Shared/MainLayout.razor b/ShopOnline.Web/Shared/MainLayout.razor index 839b8fe..f3167c1 100644 --- a/ShopOnline.Web/Shared/MainLayout.razor +++ b/ShopOnline.Web/Shared/MainLayout.razor @@ -7,7 +7,7 @@
- About +
diff --git a/ShopOnline.Web/Shared/NavMenu.razor b/ShopOnline.Web/Shared/NavMenu.razor index f87fca2..ba8f970 100644 --- a/ShopOnline.Web/Shared/NavMenu.razor +++ b/ShopOnline.Web/Shared/NavMenu.razor @@ -1,4 +1,7 @@ -