diff --git a/ShopOnline.Web/Pages/Checkout.razor b/ShopOnline.Web/Pages/Checkout.razor index af2045b..3632bb8 100644 --- a/ShopOnline.Web/Pages/Checkout.razor +++ b/ShopOnline.Web/Pages/Checkout.razor @@ -3,66 +3,60 @@

Checkout

- @if (ShoppingCartItems.Count() == 0) - { -

You currently have no items in your shopping cart.

- } - else - { -
-
-
-

Payment Method

-
- - -
-
+ +
+
+
+

Payment Method

+
+ + +
+
+
+ @if (ShoppingCartItems == null) + { +
+
+
- @if (ShoppingCartItems == null) +
+ } + else + { +
+

Payment Summary

+ @if (ShoppingCartItems.Count() > 0) { -
-
- -
-
+ + + + + + + + + @foreach (var item in ShoppingCartItems) + { + + + + + } + + + + + +
ProductPrice
@item.Quantity x @item.ProductName@item.TotalPrice.ToString("C")
Total@PaymentAmount.ToString("C")
} else { -
-

Payment Summary

- @if (ShoppingCartItems.Count() > 0) - { - - - - - - - - - @foreach (var item in ShoppingCartItems) - { - - - - - } - - - - - -
ProductPrice
@item.Quantity x @item.ProductName@item.TotalPrice.ToString("C")
Total@PaymentAmount.ToString("C")
- } - else - { -

You currently have no items in your shopping cart.

- } -
+

You currently have no items in your shopping cart.

}
-
- } + } +
+
diff --git a/ShopOnline.Web/Pages/CheckoutBase.cs b/ShopOnline.Web/Pages/CheckoutBase.cs index 932c095..41937bf 100644 --- a/ShopOnline.Web/Pages/CheckoutBase.cs +++ b/ShopOnline.Web/Pages/CheckoutBase.cs @@ -21,11 +21,14 @@ namespace ShopOnline.Web.Pages [Inject] public IShoppingCartService ShoppingCartService { get; set; } + [Inject] + public IManageCartItemsLocalStorageService ManageCartItemsLocalStorage { get; set; } + protected override async Task OnInitializedAsync() { try { - ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); + ShoppingCartItems = await ManageCartItemsLocalStorage.GetCollection(); if(ShoppingCartItems != null) { @@ -49,7 +52,6 @@ namespace ShopOnline.Web.Pages { if(firstRender) { - Console.WriteLine("HIT"); await Js.InvokeVoidAsync("initPayPalButton"); } } diff --git a/ShopOnline.Web/Pages/DisplayProducts.razor b/ShopOnline.Web/Pages/DisplayProducts.razor index db4d364..75c51b7 100644 --- a/ShopOnline.Web/Pages/DisplayProducts.razor +++ b/ShopOnline.Web/Pages/DisplayProducts.razor @@ -2,7 +2,6 @@ @foreach (var item in Products) { - Console.WriteLine(@item.Price);
diff --git a/ShopOnline.Web/Pages/FetchData.razor b/ShopOnline.Web/Pages/FetchData.razor deleted file mode 100644 index 7d004a5..0000000 --- a/ShopOnline.Web/Pages/FetchData.razor +++ /dev/null @@ -1,57 +0,0 @@ -@page "/fetchdata" -@inject HttpClient Http - -Weather forecast - -

Weather forecast

- -

This component demonstrates fetching data from the server.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await Http.GetFromJsonAsync("sample-data/weather.json"); - } - - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public string? Summary { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/ShopOnline.Web/Pages/ProductDetailsBase.cs b/ShopOnline.Web/Pages/ProductDetailsBase.cs index 272cca8..e3c892d 100644 --- a/ShopOnline.Web/Pages/ProductDetailsBase.cs +++ b/ShopOnline.Web/Pages/ProductDetailsBase.cs @@ -13,16 +13,26 @@ namespace ShopOnline.Web.Pages public IProductService ProductService { get; set; } [Inject] public IShoppingCartService ShoppingCartService { get; set; } + + [Inject] + public IManageProductsLocalStorageService ManageProductsLocalStorageService { get; set; } + + [Inject] + public IManageCartItemsLocalStorageService ManageCartItemsLocalStorageService { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } public ProductDto Product { get; set; } public string ErrorMessage { get; set; } + private List ShoppingCartItems { get; set; } + protected override async Task OnInitializedAsync() { try { - Product = await ProductService.GetItem(Id); + ShoppingCartItems = await ManageCartItemsLocalStorageService.GetCollection(); + Product = await GetProductById(Id); } catch(Exception ex) { @@ -35,6 +45,13 @@ namespace ShopOnline.Web.Pages try { var cartItemDto = await ShoppingCartService.AddItem(cartItemToAddDto); + + if(cartItemDto != null) + { + ShoppingCartItems.Add(cartItemDto); + await ManageCartItemsLocalStorageService.SaveCollection(ShoppingCartItems); + } + NavigationManager.NavigateTo("/ShoppingCart"); } catch (Exception) @@ -43,5 +60,15 @@ namespace ShopOnline.Web.Pages } } + private async Task GetProductById(int id) + { + var productDtos = await ManageProductsLocalStorageService.GetCollection(); + + if(productDtos != null) + { + return productDtos.SingleOrDefault(p => p.Id == id); + } + return null; + } } } diff --git a/ShopOnline.Web/Pages/ProductsBase.cs b/ShopOnline.Web/Pages/ProductsBase.cs index 0b1a9c4..5640f4d 100644 --- a/ShopOnline.Web/Pages/ProductsBase.cs +++ b/ShopOnline.Web/Pages/ProductsBase.cs @@ -13,6 +13,13 @@ namespace ShopOnline.Web.Pages [Inject] public IShoppingCartService ShoppingCartService { get; set; } + [Inject] + public IManageProductsLocalStorageService ManageProductsLocalStorageService { get; set; } + + [Inject] + public IManageCartItemsLocalStorageService ManageCartItemsLocalStorageService { get; set; } + + public IEnumerable Products { get; set; } [Inject] @@ -24,9 +31,11 @@ namespace ShopOnline.Web.Pages { try { - Products = await ProductService.GetItems(); + await ClearLocalStorage(); - var shoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); + Products = await ManageProductsLocalStorageService.GetCollection(); + + var shoppingCartItems = await ManageCartItemsLocalStorageService.GetCollection(); var totalQuantity = shoppingCartItems.Sum(i => i.Quantity); ShoppingCartService.RaiseEventOnShoppingCartChanged(totalQuantity); @@ -51,5 +60,11 @@ namespace ShopOnline.Web.Pages return groupedProductDtos.FirstOrDefault(pg => pg.CategoryId == groupedProductDtos.Key).CategoryName; } + private async Task ClearLocalStorage() + { + await ManageProductsLocalStorageService.RemoveCollection(); + await ManageCartItemsLocalStorageService.RemoveCollection(); + } + } } diff --git a/ShopOnline.Web/Pages/ProductsByCategoryBase.cs b/ShopOnline.Web/Pages/ProductsByCategoryBase.cs index 7bc4fdf..2c63ca6 100644 --- a/ShopOnline.Web/Pages/ProductsByCategoryBase.cs +++ b/ShopOnline.Web/Pages/ProductsByCategoryBase.cs @@ -12,6 +12,12 @@ namespace ShopOnline.Web.Pages [Inject] public IProductService ProductService { get; set; } + [Inject] + public IManageProductsLocalStorageService ManageProductsLocalStorageService { get; set; } + + [Inject] + + public IEnumerable Products { get; set; } public string CategoryName { get; set; } public string ErrorMessage { get; set; } @@ -20,7 +26,7 @@ namespace ShopOnline.Web.Pages { try { - Products = await ProductService.GetItemsByCategory(CategoryId); + Products = await GetProductCollectionByCategoryId(CategoryId); if(Products != null && Products.Count() > 0) { var productDto = Products.FirstOrDefault(p => p.CategoryId == CategoryId); @@ -35,5 +41,19 @@ namespace ShopOnline.Web.Pages ErrorMessage = ex.Message; } } + + private async Task> GetProductCollectionByCategoryId(int categoryId) + { + var productCollection = await ManageProductsLocalStorageService.GetCollection(); + + if(productCollection != null) + { + return productCollection.Where(p => p.CategoryId == categoryId); + } + else + { + return await ProductService.GetItemsByCategory(categoryId); + } + } } } diff --git a/ShopOnline.Web/Pages/ShoppingCartBase.cs b/ShopOnline.Web/Pages/ShoppingCartBase.cs index 680f057..df3db30 100644 --- a/ShopOnline.Web/Pages/ShoppingCartBase.cs +++ b/ShopOnline.Web/Pages/ShoppingCartBase.cs @@ -13,6 +13,9 @@ namespace ShopOnline.Web.Pages [Inject] public IShoppingCartService ShoppingCartService { get; set; } + [Inject] + public IManageCartItemsLocalStorageService ManageCartItemsLocalStorageService { get; set; } + public List ShoppingCartItems { get; set; } public string ErrorMessage { get; set; } @@ -24,7 +27,7 @@ namespace ShopOnline.Web.Pages { try { - ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); + ShoppingCartItems = await ManageCartItemsLocalStorageService.GetCollection(); CartChanged(); } catch (Exception ex) @@ -56,7 +59,7 @@ namespace ShopOnline.Web.Pages var returnedUpdateItemDto = await this.ShoppingCartService.UpdateQuantity(updateItemDto); - UpdateItemTotalPrice(returnedUpdateItemDto); + await UpdateItemTotalPrice(returnedUpdateItemDto); CartChanged(); await MakeUpdateQuantityButtonVisible(id, false); @@ -90,7 +93,7 @@ namespace ShopOnline.Web.Pages } - private void UpdateItemTotalPrice(CartItemDto cartItemDto) + private async Task UpdateItemTotalPrice(CartItemDto cartItemDto) { var item = GetCartItem(cartItemDto.Id); @@ -98,6 +101,8 @@ namespace ShopOnline.Web.Pages { item.TotalPrice = cartItemDto.Price * cartItemDto.Quantity; } + + await ManageCartItemsLocalStorageService.SaveCollection(ShoppingCartItems); } private void CalculateCartSummaryTotals() @@ -121,11 +126,13 @@ namespace ShopOnline.Web.Pages return ShoppingCartItems.FirstOrDefault(i => i.Id == id); } - private void RemoveCartItem(int id) + private async Task RemoveCartItem(int id) { var CartItemDto = GetCartItem(id); ShoppingCartItems.Remove(CartItemDto); + + await ManageCartItemsLocalStorageService.SaveCollection(ShoppingCartItems); } private void CartChanged() diff --git a/ShopOnline.Web/Program.cs b/ShopOnline.Web/Program.cs index f63bae7..0914ebf 100644 --- a/ShopOnline.Web/Program.cs +++ b/ShopOnline.Web/Program.cs @@ -1,3 +1,4 @@ +using Blazored.LocalStorage; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using ShopOnline.Web; @@ -12,4 +13,8 @@ builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https:/ builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + await builder.Build().RunAsync(); diff --git a/ShopOnline.Web/Services/Contracts/IManageCartItemsLocalStorageService.cs b/ShopOnline.Web/Services/Contracts/IManageCartItemsLocalStorageService.cs new file mode 100644 index 0000000..3e5e3fc --- /dev/null +++ b/ShopOnline.Web/Services/Contracts/IManageCartItemsLocalStorageService.cs @@ -0,0 +1,11 @@ +using ShopOnline.Models.Dtos; + +namespace ShopOnline.Web.Services.Contracts +{ + public interface IManageCartItemsLocalStorageService + { + Task> GetCollection(); + Task SaveCollection(List cartItemDtos); + Task RemoveCollection(); + } +} diff --git a/ShopOnline.Web/Services/Contracts/IManageProductsLocalStorageService.cs b/ShopOnline.Web/Services/Contracts/IManageProductsLocalStorageService.cs new file mode 100644 index 0000000..b35d0f0 --- /dev/null +++ b/ShopOnline.Web/Services/Contracts/IManageProductsLocalStorageService.cs @@ -0,0 +1,10 @@ +using ShopOnline.Models.Dtos; + +namespace ShopOnline.Web.Services.Contracts +{ + public interface IManageProductsLocalStorageService + { + Task> GetCollection(); + Task RemoveCollection(); + } +} diff --git a/ShopOnline.Web/Services/ManageCartItemsLocalStorageService.cs b/ShopOnline.Web/Services/ManageCartItemsLocalStorageService.cs new file mode 100644 index 0000000..0856c4b --- /dev/null +++ b/ShopOnline.Web/Services/ManageCartItemsLocalStorageService.cs @@ -0,0 +1,47 @@ +using Blazored.LocalStorage; +using ShopOnline.Models.Dtos; +using ShopOnline.Web.Services.Contracts; + +namespace ShopOnline.Web.Services +{ + public class ManageCartItemsLocalStorageService : IManageCartItemsLocalStorageService + { + private readonly ILocalStorageService localStorageService; + private readonly IShoppingCartService shoppingCartService; + + const string key = "CartItemCollection"; + + public ManageCartItemsLocalStorageService(ILocalStorageService localStorageService, IShoppingCartService shoppingCartService) + { + this.localStorageService = localStorageService; + this.shoppingCartService = shoppingCartService; + } + public async Task> GetCollection() + { + return await this.localStorageService.GetItemAsync>(key) ?? await AddCollection(); + } + + public async Task RemoveCollection() + { + await this.localStorageService.RemoveItemAsync(key); + } + + public async Task SaveCollection(List cartItemDtos) + { + await this.localStorageService.SetItemAsync(key, cartItemDtos); + + } + + private async Task> AddCollection() + { + var shoppingCartCollection = await this.shoppingCartService.GetItems(HardCoded.UserId); + + if(shoppingCartCollection != null) + { + await this.localStorageService.SetItemAsync(key, shoppingCartCollection); + } + + return shoppingCartCollection; + } + } +} diff --git a/ShopOnline.Web/Services/ManageProductsLocalStorageService.cs b/ShopOnline.Web/Services/ManageProductsLocalStorageService.cs new file mode 100644 index 0000000..7e4d141 --- /dev/null +++ b/ShopOnline.Web/Services/ManageProductsLocalStorageService.cs @@ -0,0 +1,41 @@ +using Blazored.LocalStorage; +using ShopOnline.Models.Dtos; +using ShopOnline.Web.Services.Contracts; + +namespace ShopOnline.Web.Services +{ + public class ManageProductsLocalStorageService : IManageProductsLocalStorageService + { + private readonly ILocalStorageService localStorageService; + private readonly IProductService productService; + + private const string key = "ProductCollection"; + + public ManageProductsLocalStorageService(ILocalStorageService localStorageService, IProductService productService) + { + this.localStorageService = localStorageService; + this.productService = productService; + } + public async Task> GetCollection() + { + return await this.localStorageService.GetItemAsync>(key) ?? await AddCollection(); + } + + public async Task RemoveCollection() + { + await this.localStorageService.RemoveItemAsync(key); + } + + private async Task> AddCollection() + { + var productCollection = await this.productService.GetItems(); + + if(productCollection != null) + { + await this.localStorageService.SetItemAsync(key, productCollection); + } + + return productCollection; + } + } +} diff --git a/ShopOnline.Web/Shared/SurveyPrompt.razor b/ShopOnline.Web/Shared/SurveyPrompt.razor deleted file mode 100644 index 962027f..0000000 --- a/ShopOnline.Web/Shared/SurveyPrompt.razor +++ /dev/null @@ -1,16 +0,0 @@ -
- - @Title - - - Please take our - brief survey - - and tell us what you think. -
- -@code { - // Demonstrates how a parent component can supply parameters - [Parameter] - public string? Title { get; set; } -} diff --git a/ShopOnline.Web/ShopOnline.Web.csproj b/ShopOnline.Web/ShopOnline.Web.csproj index 9db1809..0ee2c67 100644 --- a/ShopOnline.Web/ShopOnline.Web.csproj +++ b/ShopOnline.Web/ShopOnline.Web.csproj @@ -7,6 +7,7 @@ +