WIP: Adding panel for editing items to standing order index

This commit is contained in:
Rob Harrington
2016-11-25 08:14:41 +11:00
parent 05bc2bd293
commit 1bd01c83a7
11 changed files with 98 additions and 69 deletions

View File

@@ -0,0 +1,3 @@
angular.module("admin.standingOrders").controller "ProductsPanelController", ($scope) ->
$scope.standingOrder = $scope.object
$scope.distributor_id = $scope.standingOrder.shop.id

View File

@@ -7,3 +7,8 @@ angular.module("admin.standingOrders").controller "StandingOrdersController", ($
if $scope.shop_id?
# CurrentShop.shop = $filter('filter')($scope.shops, {id: $scope.shop_id})[0]
$scope.standingOrders = StandingOrders.index("q[shop_id_eq]": $scope.shop_id, ams_prefix: 'index')
$scope.itemCount = (standingOrder) ->
standingOrder.standing_line_items.reduce (sum, sli) ->
return sum + sli.quantity
, 0

View File

@@ -1,45 +1,6 @@
angular.module("admin.standingOrders").factory "StandingOrder", ($injector, $http, StatusMessage, InfoDialog, StandingOrderResource) ->
angular.module("admin.standingOrders").factory "StandingOrder", ($injector, StandingOrderResource) ->
class StandingOrder extends StandingOrderResource
errors: {}
constructor: (obj={}) ->
angular.extend(@, obj)
constructor: ->
if $injector.has('standingOrder')
angular.extend(@, $injector.get('standingOrder'))
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)
create: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@$save().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
angular.extend(@errors, response.data.errors)
update: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@$update().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
angular.extend(@errors, response.data.errors)

View File

@@ -0,0 +1,39 @@
angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http, InfoDialog, StatusMessage) ->
errors: {}
buildItem: (item, ams_prefix) ->
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)
create: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@$save().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
angular.extend(@errors, response.data.errors)
update: ->
StatusMessage.display 'progress', 'Saving...'
delete @errors[k] for k, v of @errors
@$update().then (response) =>
StatusMessage.display 'success', 'Saved'
, (response) =>
StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.'
angular.extend(@errors, response.data.errors)

View File

@@ -1,5 +1,5 @@
angular.module("admin.standingOrders").factory 'StandingOrderResource', ($resource) ->
$resource('/admin/standing_orders/:id/:action.json', {}, {
angular.module("admin.standingOrders").factory 'StandingOrderResource', ($resource, StandingOrderPrototype) ->
resource = $resource('/admin/standing_orders/:id/:action.json', {}, {
'index':
method: 'GET'
isArray: true
@@ -8,3 +8,7 @@ angular.module("admin.standingOrders").factory 'StandingOrderResource', ($resour
params:
id: '@id'
})
angular.extend(resource.prototype, StandingOrderPrototype)
resource

View File

@@ -9,6 +9,5 @@ angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOr
load: (standingOrders) ->
for standingOrder in standingOrders
standingOrder = new StandingOrder(standingOrder)
@byID[standingOrder.id] = standingOrder
@pristineByID[standingOrder.id] = angular.copy(standingOrder)