Files
old-wiaas-legacy/api-wiaas/client/js/components/suppliers/suppliers-add-edit.directive.js
2018-06-11 11:09:35 +02:00

53 lines
2.0 KiB
JavaScript

(function () {
global.dashModule
.controller('suppliersAddEditCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', suppliersAddEditCtrl])
.directive('suppliersAddEdit', [suppliersAddEditDirective]);
function suppliersAddEditDirective() {
return {
restrict: 'E',
templateUrl: 'suppliers/html/suppliersAddEditFormTemplate'
};
}
function suppliersAddEditCtrl($scope, $http, $, $translate, utilsService) {
$scope.suppliersFormInit = suppliersFormInit;
$scope.addEditSupplier = addEditSupplier;
$scope.data = $scope.data || {};
function suppliersFormInit(action) {
$scope.formAction = action;
}
function addEditSupplier() {
const params = $.param({
idSupplier: $scope.data.id || 0,
name: $scope.data.name || '',
phone: $scope.data.phone || '',
mail: $scope.data.mail || ''
});
const url = 'suppliers/api/' + $scope.formAction.toLowerCase() + 'Supplier';
$http({
method: 'POST',
url: url,
data: params
}).then(showUpdateMessage, utilsService.onHttpError);
}
function showUpdateMessage(response) {
if (typeof response.data.messages !== 'undefined') {
response.data.messages.forEach((messageObj) => {
const key = messageObj.key ? $translate.instant('suppliers.tables.headers.' + messageObj.key) : '';
let translatedMessage = $translate.instant('suppliers.messages.' + messageObj.message);
translatedMessage = key !== '' ? key + ': ' + translatedMessage : translatedMessage;
utilsService.displayMessage(messageObj.code, translatedMessage);
if (typeof $scope.onUpdated !== 'undefined' && messageObj.code === 'success') {
$scope.onUpdated();
}
});
}
}
}
})();