mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-27 06:05:19 +00:00
Extract OrderCycle into service, create works
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
function AdminCreateOrderCycleCtrl($scope, $http, Enterprise) {
|
||||
$scope.order_cycle = {};
|
||||
$scope.order_cycle.incoming_exchanges = [];
|
||||
$scope.order_cycle.outgoing_exchanges = [];
|
||||
function AdminCreateOrderCycleCtrl($scope, $http, OrderCycle, Enterprise) {
|
||||
$scope.order_cycle = OrderCycle.order_cycle;
|
||||
|
||||
$scope.enterprises = {};
|
||||
Enterprise.index(function(data) {
|
||||
@@ -11,29 +9,11 @@ function AdminCreateOrderCycleCtrl($scope, $http, Enterprise) {
|
||||
});
|
||||
|
||||
$scope.addSupplier = function($event) {
|
||||
$event.preventDefault();
|
||||
$scope.order_cycle.incoming_exchanges.push({enterprise_id: $scope.new_supplier_id, active: true});
|
||||
OrderCycle.addSupplier($event, $scope.new_supplier_id);
|
||||
};
|
||||
|
||||
$scope.submit = function() {
|
||||
$scope.removeInactiveExchanges();
|
||||
|
||||
$http.post('/admin/order_cycles', {order_cycle: $scope.order_cycle}).success(function(data) {
|
||||
if(data['success']) {
|
||||
window.location = '/admin/order_cycles';
|
||||
} else {
|
||||
console.log('fail');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.removeInactiveExchanges = function() {
|
||||
for(var i=0; i < $scope.order_cycle.incoming_exchanges.length; i++) {
|
||||
if(!$scope.order_cycle.incoming_exchanges[i].active) {
|
||||
$scope.order_cycle.incoming_exchanges.splice(i, 1)
|
||||
i--;
|
||||
}
|
||||
}
|
||||
OrderCycle.create();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -100,10 +80,78 @@ angular.module('order_cycle', ['ngResource']).
|
||||
config(function($httpProvider) {
|
||||
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
|
||||
}).
|
||||
factory('OrderCycle', function($resource) {
|
||||
return $resource('/admin/order_cycles/:order_cycle_id.json', {},
|
||||
{'index': { method: 'GET', isArray: true},
|
||||
'show': { method: 'GET', isArray: false}});
|
||||
factory('OrderCycle', function($http, $resource) {
|
||||
var OrderCycle = $resource('/admin/order_cycles/:order_cycle_id.json', {},
|
||||
{'index': { method: 'GET', isArray: true},
|
||||
'show': { method: 'GET', isArray: false}});
|
||||
|
||||
|
||||
return {
|
||||
order_cycle: {incoming_exchanges: [],
|
||||
outgoing_exchanges: []},
|
||||
|
||||
addSupplier: function($event, new_supplier_id) {
|
||||
$event.preventDefault();
|
||||
this.order_cycle.incoming_exchanges.push({enterprise_id: new_supplier_id, active: true});
|
||||
},
|
||||
|
||||
removeInactiveExchanges: function() {
|
||||
for(var i=0; i < this.order_cycle.incoming_exchanges.length; i++) {
|
||||
if(!this.order_cycle.incoming_exchanges[i].active) {
|
||||
this.order_cycle.incoming_exchanges.splice(i, 1);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
load: function() {
|
||||
var order_cycle_id = window.location.pathname.match(/\/admin\/order_cycles\/(\d+)/)[1];
|
||||
OrderCycle.get({order_cycle_id: order_cycle_id}, function(oc) {
|
||||
this.order_cycle = oc;
|
||||
this.order_cycle.incoming_exchanges = [];
|
||||
this.order_cycle.outgoing_exchanges = [];
|
||||
for(i in this.order_cycle.exchanges) {
|
||||
var exchange = this.order_cycle.exchanges[i];
|
||||
if(exchange.sender_id == this.order_cycle.coordinator_id) {
|
||||
this.order_cycle.outgoing_exchanges.push({enterprise_id: exchange.receiver_id, active: true});
|
||||
|
||||
} else if(exchange.receiver_id == this.order_cycle.coordinator_id) {
|
||||
this.order_cycle.incoming_exchanges.push({enterprise_id: exchange.sender_id, active: true});
|
||||
|
||||
} else {
|
||||
console.log('Exchange between two enterprises, neither of which is coordinator!');
|
||||
}
|
||||
}
|
||||
|
||||
delete(this.order_cycle.exchanges);
|
||||
});
|
||||
},
|
||||
|
||||
create: function() {
|
||||
this.removeInactiveExchanges();
|
||||
|
||||
$http.post('/admin/order_cycles', {order_cycle: this.order_cycle}).success(function(data) {
|
||||
if(data['success']) {
|
||||
window.location = '/admin/order_cycles';
|
||||
} else {
|
||||
console.log('fail');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
update: function() {
|
||||
this.removeInactiveExchanges();
|
||||
|
||||
var path = '/admin/order_cycles/' + this.order_cycle.id
|
||||
$http.put(path, {order_cycle: this.order_cycle}).success(function(data) {
|
||||
if(data['success']) {
|
||||
window.location = '/admin/order_cycles';
|
||||
} else {
|
||||
console.log('fail');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}).
|
||||
factory('Enterprise', function($resource) {
|
||||
return $resource('/admin/enterprises/:enterprise_id.json', {},
|
||||
|
||||
Reference in New Issue
Block a user