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..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,12 +1,20 @@ -Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, Cart) -> +Darkswarm.controller "EditBoughtOrderController", ($scope, $resource, $timeout, Cart, Messages) -> $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.has_one_line_item() + Messages.error(t 'orders_cannot_remove_the_final_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..380f0ccc416 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -115,6 +115,9 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo @line_items = [] localStorageService.clearAll() # One day this will have to be moar GRANULAR + has_one_line_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..23ab4730818 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -222,6 +222,13 @@ 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] + expect(Cart.has_one_line_item()).toBe(true) + it "pops the queue", -> Cart.update_enqueued = true spyOn(Cart, 'scheduleUpdate')