mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
buildItem and removeItem are instanceMethods standingOrder objects
This commit is contained in:
@@ -22,12 +22,12 @@ angular.module("admin.standingOrders").controller "StandingOrderController", ($s
|
||||
|
||||
$scope.addStandingLineItem = ->
|
||||
$scope.standing_order_form.$setDirty()
|
||||
StandingOrder.buildItem($scope.newItem)
|
||||
$scope.standingOrder.buildItem($scope.newItem)
|
||||
|
||||
$scope.removeStandingLineItem = (item) ->
|
||||
if confirm(t('are_you_sure'))
|
||||
$scope.standing_order_form.$setDirty()
|
||||
StandingOrder.removeItem(item)
|
||||
$scope.standingOrder.removeItem(item)
|
||||
|
||||
$scope.estimatedSubtotal = ->
|
||||
$scope.standingOrder.standing_line_items.reduce (subtotal, item) ->
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $http, StatusMessage, InfoDialog, StandingOrderResource) ->
|
||||
instanceMethods =
|
||||
buildItem: (item) ->
|
||||
return false unless item.variant_id > 0
|
||||
return false unless item.quantity > 0
|
||||
data = angular.extend({}, item, { shop_id: @shop_id, schedule_id: @schedule_id })
|
||||
$http.post("/admin/standing_line_items/build", data).then (response) =>
|
||||
@standing_line_items.push response.data
|
||||
, (response) =>
|
||||
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) =>
|
||||
@standing_line_items.splice(index,1)
|
||||
, (response) ->
|
||||
InfoDialog.open 'error', response.data.errors[0]
|
||||
else
|
||||
@standing_line_items.splice(index,1)
|
||||
|
||||
new class StandingOrder
|
||||
standingOrder: new StandingOrderResource()
|
||||
errors: {}
|
||||
|
||||
constructor: ->
|
||||
if $injector.has('standingOrder')
|
||||
angular.extend(@standingOrder, $injector.get('standingOrder'))
|
||||
|
||||
buildItem: (item) ->
|
||||
return false unless item.variant_id > 0
|
||||
return false unless item.quantity > 0
|
||||
data = angular.extend({}, item, { shop_id: @standingOrder.shop_id, schedule_id: @standingOrder.schedule_id })
|
||||
$http.post("/admin/standing_line_items/build", data).then (response) =>
|
||||
@standingOrder.standing_line_items.push response.data
|
||||
, (response) =>
|
||||
InfoDialog.open 'error', response.data.errors[0]
|
||||
|
||||
removeItem: (item) ->
|
||||
index = @standingOrder.standing_line_items.indexOf(item)
|
||||
if item.id?
|
||||
$http.delete("/admin/standing_line_items/#{item.id}").then (response) =>
|
||||
@standingOrder.standing_line_items.splice(index,1)
|
||||
, (response) ->
|
||||
InfoDialog.open 'error', response.data.errors[0]
|
||||
else
|
||||
@standingOrder.standing_line_items.splice(index,1)
|
||||
angular.extend(@standingOrder, $injector.get('standingOrder'), instanceMethods)
|
||||
|
||||
create: ->
|
||||
StatusMessage.display 'progress', 'Saving...'
|
||||
delete @errors[k] for k, v of @errors
|
||||
@standingOrder.$save().then (response) =>
|
||||
angular.extend(@standingOrder, instanceMethods)
|
||||
StatusMessage.display 'success', 'Saved'
|
||||
, (response) =>
|
||||
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
|
||||
@@ -39,6 +41,7 @@ angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $htt
|
||||
StatusMessage.display 'progress', 'Saving...'
|
||||
delete @errors[k] for k, v of @errors
|
||||
@standingOrder.$update().then (response) =>
|
||||
angular.extend(@standingOrder, instanceMethods)
|
||||
StatusMessage.display 'success', 'Saved'
|
||||
, (response) =>
|
||||
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
|
||||
|
||||
Reference in New Issue
Block a user