diff --git a/app/assets/javascripts/admin/bulk_order_management.js.coffee b/app/assets/javascripts/admin/bulk_order_management.js.coffee index d573dd6197..018970cb7c 100644 --- a/app/assets/javascripts/admin/bulk_order_management.js.coffee +++ b/app/assets/javascripts/admin/bulk_order_management.js.coffee @@ -67,6 +67,18 @@ orderManagementModule.controller "AdminOrderMgmtCtrl", [ if angular.equals(distributor, order.distributor) order.distributor = distributor break + + $scope.deleteLineItem = (lineItem) -> + #if confirm("Are you sure?") + $http( + method: "DELETE" + url: "/api/orders/" + lineItem.order.number + "/line_items/" + lineItem.id + ).success (data) -> + $scope.lineItems.splice $scope.lineItems.indexOf(lineItem), 1 + lineItem.order.line_items.splice lineItem.order.line_items.indexOf(lineItem), 1 + #delete $scope.dirtyProducts[product.id] if $scope.dirtyProducts.hasOwnProperty(product.id) + #$scope.displayDirtyProducts() + ] orderManagementModule.filter "selectFilter", [ diff --git a/app/views/spree/admin/orders/bulk_management.html.haml b/app/views/spree/admin/orders/bulk_management.html.haml index 132c9c3afd..437fe83eb8 100644 --- a/app/views/spree/admin/orders/bulk_management.html.haml +++ b/app/views/spree/admin/orders/bulk_management.html.haml @@ -36,4 +36,4 @@ %td.quantity {{ line_item.quantity }} %td.max {{ line_item.max }} %td.actions - %a{ :class => "delete-line-item icon-trash no-text" } \ No newline at end of file + %a{ 'ng-click' => "deleteLineItem(line_item)", :class => "delete-line-item icon-trash no-text" } \ No newline at end of file diff --git a/spec/features/admin/bulk_order_management_spec.rb b/spec/features/admin/bulk_order_management_spec.rb index e107cbbbb2..645fa4d438 100644 --- a/spec/features/admin/bulk_order_management_spec.rb +++ b/spec/features/admin/bulk_order_management_spec.rb @@ -155,6 +155,14 @@ feature %q{ it "shows a delete button for each line item" do page.should have_selector "a.delete-line-item", :count => 2 end + + it "removes a line item when the relavent delete button is clicked" do + first("a.delete-line-item").click + page.should_not have_selector "a.delete-line-item", :count => 2 + page.should have_selector "a.delete-line-item", :count => 1 + visit '/admin/orders/bulk_management' + page.should have_selector "a.delete-line-item", :count => 1 + end end end end diff --git a/spec/javascripts/unit/bulk_order_management_spec.js.coffee b/spec/javascripts/unit/bulk_order_management_spec.js.coffee index 746fbe8aee..e264092355 100644 --- a/spec/javascripts/unit/bulk_order_management_spec.js.coffee +++ b/spec/javascripts/unit/bulk_order_management_spec.js.coffee @@ -133,4 +133,29 @@ describe "AdminOrderMgmtCtrl", -> distributor: distributor1_order scope.matchDistributor order - expect(order.distributor is distributor1_list).toEqual true \ No newline at end of file + expect(order.distributor is distributor1_list).toEqual true + + describe "deleting a line item", -> + order = line_item1 = line_item2 = null + beforeEach -> + order = { number: "R12345678", line_items: [] } + line_item1 = { id: 1, order: order } + line_item2 = { id: 2, order: order } + order.line_items = [ line_item1, line_item2 ] + + it "sends a delete request via the API", -> + httpBackend.expectDELETE("/api/orders/#{line_item1.order.number}/line_items/#{line_item1.id}").respond "nothing" + scope.deleteLineItem line_item1 + httpBackend.flush() + + it "removes line_item from the line_items array of the relevant order object when request is 204", -> + httpBackend.expectDELETE("/api/orders/#{line_item1.order.number}/line_items/#{line_item1.id}").respond 204, "NO CONTENT" + scope.deleteLineItem line_item1 + httpBackend.flush() + expect(order.line_items).toEqual [line_item2] + + it "does not remove line_item from the line_items array when request is not successful", -> + httpBackend.expectDELETE("/api/orders/#{line_item1.order.number}/line_items/#{line_item1.id}").respond 404, "NO CONTENT" + scope.deleteLineItem line_item1 + httpBackend.flush() + expect(order.line_items).toEqual [line_item1, line_item2] \ No newline at end of file