From 651bdad7b9c3a677be70a9e3417c2e149dcbf522 Mon Sep 17 00:00:00 2001 From: Matthew Rieger Date: Sun, 13 Dec 2020 17:15:11 -0500 Subject: [PATCH 1/6] Fix users removing last item of confirmed order on /cart page --- .../edit_bought_order_controller.js.coffee | 23 ++++++++++++------- .../darkswarm/services/cart.js.coffee | 6 +++++ app/views/spree/orders/_bought.html.haml | 2 +- .../darkswarm/services/cart_spec.js.coffee | 10 ++++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee index 3ed9f9af386..8457d3a4ba0 100644 --- a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee @@ -1,12 +1,19 @@ -Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, Cart) -> +Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, Cart) -> $scope.showBought = false + $scope.removeEnabled = true $scope.deleteLineItem = (id) -> - params = {id: id} - success = (response) -> - $(".line-item-" + id).remove() - Cart.removeFinalisedLineItem(id) - fail = (error) -> - console.log error + if Cart.check_last_finalised_item() + $scope.removeEnabled = false + $timeout (-> + $scope.removeEnabled = true + ), 10000 + else + params = {id: id} + success = (response) -> + $(".line-item-" + id).remove() + Cart.removeFinalisedLineItem(id) + fail = (error) -> + console.log error - $resource("/line_items/:id").delete(params, success, fail) + $resource("/line_items/:id").delete(params, success, fail) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 9fbb6ca4157..6ad4e8c7f10 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -115,6 +115,12 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo @line_items = [] localStorageService.clearAll() # One day this will have to be moar GRANULAR + check_last_finalised_item: => + if @line_items_finalised.length == 1 + Messages.error(t 'orders_cannot_remove_the_final_item') + + @line_items_finalised.length == 1 + removeFinalisedLineItem: (id) => @line_items_finalised = @line_items_finalised.filter (item) -> item.id != id diff --git a/app/views/spree/orders/_bought.html.haml b/app/views/spree/orders/_bought.html.haml index e9cee63e7c7..6c5837d5c1a 100644 --- a/app/views/spree/orders/_bought.html.haml +++ b/app/views/spree/orders/_bought.html.haml @@ -28,5 +28,5 @@ = line_item.display_amount_with_adjustments.to_html unless line_item.quantity.nil? %td.bought-item-delete.text-center - %a{ng: {click: "deleteLineItem(#{line_item.id})"}} + %a{ng: {click: "removeEnabled && deleteLineItem(#{line_item.id})"}} %i.ofn-i_026-trash diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 171203499e6..08bd0f87131 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -222,6 +222,16 @@ describe 'Cart service', -> expect(li.quantity).toEqual 1 expect(li.max_quantity).toEqual 1 + describe "when modifying a confirmed order", -> + it "displays flash error when attempting to remove final item", -> + spyOn(RailsFlashLoader, 'loadFlash') + li = {variant: {id: 1}, quantity: 3} + Cart.line_items_finalised = [li] + Cart.check_last_finalised_item() + expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith( + {error: t 'orders_cannot_remove_the_final_item'} + ) + it "pops the queue", -> Cart.update_enqueued = true spyOn(Cart, 'scheduleUpdate') From 3725ba0fc43737f375eb3b5ce7ff76f3b918c04b Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 11 Jan 2021 08:36:16 -0800 Subject: [PATCH 2/6] Update app/assets/javascripts/darkswarm/services/cart.js.coffee --- app/assets/javascripts/darkswarm/services/cart.js.coffee | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 6ad4e8c7f10..1717fa3f7b4 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -115,12 +115,8 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo @line_items = [] localStorageService.clearAll() # One day this will have to be moar GRANULAR - check_last_finalised_item: => - if @line_items_finalised.length == 1 - Messages.error(t 'orders_cannot_remove_the_final_item') - - @line_items_finalised.length == 1 - + has_one_line_item: => + @line_items_finalised.length == 1 removeFinalisedLineItem: (id) => @line_items_finalised = @line_items_finalised.filter (item) -> item.id != id From 5751e247bddbe72b1dfebd0d1c88d57557da3516 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 11 Jan 2021 08:36:23 -0800 Subject: [PATCH 3/6] Update app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee --- .../controllers/edit_bought_order_controller.js.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee index 8457d3a4ba0..4e41283c1e3 100644 --- a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee @@ -3,8 +3,9 @@ Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, $scope.removeEnabled = true $scope.deleteLineItem = (id) -> - if Cart.check_last_finalised_item() - $scope.removeEnabled = false + if Cart.has_one_line_item() + Messages.error(t 'orders_cannot_remove_the_final_item') + $scope.removeEnabled = false $timeout (-> $scope.removeEnabled = true ), 10000 From 72b7fe723f7bc7b26a6d0caa6876f194cc9e1859 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 11 Jan 2021 08:41:19 -0800 Subject: [PATCH 4/6] fix indentation --- .../controllers/edit_bought_order_controller.js.coffee | 4 ++-- app/assets/javascripts/darkswarm/services/cart.js.coffee | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee index 4e41283c1e3..f6a0564ded5 100644 --- a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee @@ -4,8 +4,8 @@ Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, $scope.deleteLineItem = (id) -> if Cart.has_one_line_item() - Messages.error(t 'orders_cannot_remove_the_final_item') - $scope.removeEnabled = false + Messages.error(t 'orders_cannot_remove_the_final_item') + $scope.removeEnabled = false $timeout (-> $scope.removeEnabled = true ), 10000 diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 1717fa3f7b4..380f0ccc416 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -116,7 +116,8 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo localStorageService.clearAll() # One day this will have to be moar GRANULAR has_one_line_item: => - @line_items_finalised.length == 1 + @line_items_finalised.length == 1 + removeFinalisedLineItem: (id) => @line_items_finalised = @line_items_finalised.filter (item) -> item.id != id From 6b1a1a59b1650028a264d39fb9613537485a4df1 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Tue, 12 Jan 2021 09:02:37 -0800 Subject: [PATCH 5/6] update cart spec --- spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 08bd0f87131..23ab4730818 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -227,10 +227,7 @@ describe 'Cart service', -> spyOn(RailsFlashLoader, 'loadFlash') li = {variant: {id: 1}, quantity: 3} Cart.line_items_finalised = [li] - Cart.check_last_finalised_item() - expect(RailsFlashLoader.loadFlash).toHaveBeenCalledWith( - {error: t 'orders_cannot_remove_the_final_item'} - ) + expect(Cart.has_one_line_item()).toBe(true) it "pops the queue", -> Cart.update_enqueued = true From ad8973d74bab09eacfdfa602077e289ed1d4891b Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 1 Mar 2021 21:01:34 -0800 Subject: [PATCH 6/6] add messages object to order controller --- .../controllers/edit_bought_order_controller.js.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee index f6a0564ded5..475a95690a7 100644 --- a/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/edit_bought_order_controller.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, Cart) -> +Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, Cart, Messages) -> $scope.showBought = false $scope.removeEnabled = true