From 205e68fa62f5347f85b6bba736bfb752c78fe3dc Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 12:02:58 +0300 Subject: [PATCH] added cart widget This is not done since it requires CurrentUserId to perform basket requests. --- .../Cart/CartWidgetScriptContributor.cs | 15 +++++++++ .../Toolbar/Cart/CartWidgetViewComponent.cs | 29 +++++++++++++++++ .../Components/Toolbar/Cart/Default.cshtml | 10 ++++++ .../EShopOnAbpPublicWebToolbarContributor.cs | 3 ++ .../wwwroot/components/cart/cart-widget.css | 0 .../wwwroot/components/cart/cart-widget.js | 32 +++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs new file mode 100644 index 00000000..b7027a15 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetScriptContributor.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using Volo.Abp.AspNetCore.Mvc.UI.Bundling; +using Volo.Abp.AspNetCore.Mvc.UI.Packages.SignalR; +using Volo.Abp.Modularity; + +namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; + +[DependsOn(typeof(SignalRBrowserScriptContributor))] +public class CartWidgetScriptContributor: BundleContributor +{ + public override void ConfigureBundle(BundleConfigurationContext context) + { + context.Files.AddIfNotContains("/components/cart/cart-widget.js"); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs new file mode 100644 index 00000000..5211dea0 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/CartWidgetViewComponent.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using EShopOnAbp.BasketService; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Widgets; + +namespace EShopOnAbp.PublicWeb.Components.Toolbar.Cart; + +[Widget( + AutoInitialize = true, + RefreshUrl = "/Widgets/Cart", + StyleFiles = new[] {"/components/cart/cart-widget.css"}, + ScriptTypes = new[] {typeof(CartWidgetScriptContributor)} +)] +public class CartWidgetViewComponent : AbpViewComponent +{ + private readonly IBasketAppService _basketAppService; + + public CartWidgetViewComponent(IBasketAppService basketAppService) + { + _basketAppService = basketAppService; + } + + public async Task InvokeAsync() + { + var basketDto = await _basketAppService.GetAsync(); + return View("~/Components/Toolbar/Cart/Default.cshtml", basketDto); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml new file mode 100644 index 00000000..88c4b6fa --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Toolbar/Cart/Default.cshtml @@ -0,0 +1,10 @@ +@using EShopOnAbp.Localization +@using Microsoft.AspNetCore.Mvc.Localization +@inject IHtmlLocalizer L +@model EShopOnAbp.BasketService.BasketDto + +
+ + @L["Toolbar:Basket"] @Model.Items.Count + +
diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs index cf1a59a0..03342e66 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Menus/EShopOnAbpPublicWebToolbarContributor.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using EShopOnAbp.PublicWeb.Components.Toolbar.Cart; using EShopOnAbp.PublicWeb.Components.Toolbar.LoginLink; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars; @@ -19,6 +20,8 @@ namespace EShopOnAbp.PublicWeb.Menus { context.Toolbar.Items.Add(new ToolbarItem(typeof(LoginLinkViewComponent))); } + + context.Toolbar.Items.Add(new ToolbarItem(typeof(CartWidgetViewComponent))); return Task.CompletedTask; } diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.css new file mode 100644 index 00000000..e69de29b diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js new file mode 100644 index 00000000..d3316681 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/cart/cart-widget.js @@ -0,0 +1,32 @@ +(function (){ + var connection = new signalR.HubConnectionBuilder() + .withUrl("/signalr-hubs/cart") + .build(); + + connection.on("BasketProductUpdated", function (data) { + var widgetManager = $wrapper.data('abp-widget-manager'); + debugger; + $('#data-cart-count-id').text(data); + widgetManager.refresh(); + abp.notify.info('The product "' + data.productName + '" has been changed!', 'Your basket has been updated!'); + + // $('.basket-list') + // .closest('.abp-widget-wrapper') + // .each(function(){ + // var $wrapper = $(this); + // if ($wrapper.find('[data-product-id=' + data.productId + ']').length){ + // var widgetManager = new abp.WidgetManager({ + // wrapper: $wrapper + // }); + // widgetManager.refresh(); + // abp.notify.info('The product "' + data.productName + '" has been changed!', 'Your basket has been updated!'); + // } + // }); + }); + + connection.start().then(function () { + }).catch(function (err) { + return console.error(err.toString()); + }); + +})(); \ No newline at end of file