mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-04 02:31:33 +00:00
Angularising Order Cycles Index
This commit is contained in:
@@ -5,4 +5,9 @@ angular.module("admin.resources").factory 'OrderCycleResource', ($resource) ->
|
||||
isArray: true
|
||||
'update':
|
||||
method: 'PUT'
|
||||
'bulkUpdate':
|
||||
method: 'POST'
|
||||
isArray: true
|
||||
params:
|
||||
action: 'bulk_update'
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module("admin.resources").factory 'OrderCycles', ($q, $injector, OrderCycleResource) ->
|
||||
angular.module("admin.resources").factory 'OrderCycles', ($q, $injector, OrderCycleResource, StatusMessage, Enterprises, blankOption) ->
|
||||
new class OrderCycles
|
||||
all: []
|
||||
byID: {}
|
||||
@@ -30,14 +30,53 @@ angular.module("admin.resources").factory 'OrderCycles', ($q, $injector, OrderCy
|
||||
deferred.reject(response)
|
||||
deferred.promise
|
||||
|
||||
saveChanges: (form) ->
|
||||
changed = {}
|
||||
for id, orderCycle of @orderCyclesByID when not @saved(orderCycle)
|
||||
changed[Object.keys(changed).length] = @changesFor(orderCycle)
|
||||
if Object.keys(changed).length > 0
|
||||
StatusMessage.display('progress', "Saving...")
|
||||
OrderCycleResource.bulkUpdate { order_cycle_set: { collection_attributes: changed } }, (data) =>
|
||||
for orderCycle in data
|
||||
angular.extend(@orderCyclesByID[orderCycle.id], orderCycle)
|
||||
angular.extend(@pristineByID[orderCycle.id], orderCycle)
|
||||
@linkToEnterprises(orderCycle)
|
||||
form.$setPristine() if form?
|
||||
StatusMessage.display('success', "Order cycles have been updated.")
|
||||
, (response) =>
|
||||
StatusMessage.display('failure', "Oh no! I was unable to save your changes.")
|
||||
|
||||
saved: (order_cycle) ->
|
||||
@diff(order_cycle).length == 0
|
||||
|
||||
diff: (order_cycle) ->
|
||||
changed = []
|
||||
for attr, value of order_cycle when not angular.equals(value, @pristineByID[order_cycle.id][attr])
|
||||
changed.push attr unless attr is "$$hashKey"
|
||||
changed.push attr if attr in @attrsToSave()
|
||||
changed
|
||||
|
||||
changesFor: (orderCycle) ->
|
||||
changes = { id: orderCycle.id }
|
||||
for attr, value of orderCycle when not angular.equals(value, @pristineByID[orderCycle.id][attr])
|
||||
changes[attr] = orderCycle[attr] if attr in @attrsToSave()
|
||||
changes
|
||||
|
||||
attrsToSave: ->
|
||||
['orders_open_at','orders_close_at']
|
||||
|
||||
resetAttribute: (order_cycle, attribute) ->
|
||||
order_cycle[attribute] = @pristineByID[order_cycle.id][attribute]
|
||||
|
||||
linkAllToEnterprises: ->
|
||||
for id, orderCycle of @orderCyclesByID
|
||||
@linkToEnterprises(orderCycle)
|
||||
|
||||
linkToEnterprises: (orderCycle) ->
|
||||
coordinator = Enterprises.enterprisesByID[orderCycle.coordinator.id]
|
||||
orderCycle.coordinator = coordinator if coordinator?
|
||||
for producer, i in orderCycle.producers
|
||||
producer = Enterprises.enterprisesByID[producer.id]
|
||||
orderCycle.producers[i] = producer if producer?
|
||||
for shop, i in orderCycle.shops
|
||||
shop = Enterprises.enterprisesByID[shop.id]
|
||||
orderCycle.shops[i] = shop if shop?
|
||||
|
||||
Reference in New Issue
Block a user