Refactoring standing line item delete logic

Using StandingOrderForm rather than separate endpoint
This commit is contained in:
Rob Harrington
2016-12-01 16:33:55 +11:00
parent 4c754e2cdb
commit a57815edbb
12 changed files with 121 additions and 56 deletions

View File

@@ -2,20 +2,30 @@ angular.module("admin.standingOrders").controller "StandingLineItemsController",
$scope.newItem = { variant_id: 0, quantity: 1 }
$scope.addStandingLineItem = ->
existing_ids = $scope.standingOrder.standing_line_items.map (sli) -> sli.variant_id
if $scope.newItem.variant_id in existing_ids
InfoDialog.open 'error', t('admin.standing_orders.product_already_in_order')
match = $scope.match()
if match
if match._destroy
angular.extend(match, $scope.newItem)
delete match._destroy
else
InfoDialog.open 'error', t('admin.standing_orders.product_already_in_order')
else
$scope.standing_order_form.$setDirty()
$scope.standingOrder.buildItem($scope.newItem)
$scope.removeStandingLineItem = (item) ->
if confirm(t('are_you_sure'))
$scope.standing_order_form.$setDirty()
$scope.standingOrder.removeItem(item)
$scope.standing_order_form.$setDirty()
$scope.standingOrder.removeItem(item)
$scope.match = ->
matching = $scope.standingOrder.standing_line_items.filter (sli) ->
sli.variant_id == $scope.newItem.variant_id
return matching[0] if matching.length > 0
null
$scope.estimatedSubtotal = ->
$scope.standingOrder.standing_line_items.reduce (subtotal, item) ->
return subtotal if item._destroy
subtotal += item.price_estimate * item.quantity
, 0

View File

@@ -11,15 +11,7 @@ angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http,
InfoDialog.open 'error', response.data.errors[0]
removeItem: (item) ->
index = @standing_line_items.indexOf(item)
if item.id?
$http.delete("/admin/standing_line_items/#{item.id}").then (response) =>
$injector.get('StandingOrders').afterRemoveItem(@id, item.id) if $injector.has('StandingOrders')
@standing_line_items.splice(index,1)
, (response) ->
InfoDialog.open 'error', response.data.errors[0]
else
@standing_line_items.splice(index,1)
item._destroy = true
create: ->
StatusMessage.display 'progress', 'Saving...'