salix/client/ticket/src/notes/ticket-observations.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-03-16 14:06:42 +00:00
import ngModule from '../module';
class TicketObservations {
constructor($stateParams, $scope, $http, $translate, vnApp) {
this.params = $stateParams;
this.$scope = $scope;
this.$http = $http;
this.$translate = $translate;
this.vnApp = vnApp;
this.ticketObservations = [];
this.oldObservations = {};
this.removedObservations = [];
}
_setIconAdd() {
if (this.ticketObservations.length) {
this.ticketObservations.map(element => {
element.showAddIcon = false;
return true;
});
this.ticketObservations[this.ticketObservations.length - 1].showAddIcon = true;
}
}
_setDirtyForm() {
if (this.$scope.form) {
this.$scope.form.$setDirty();
}
}
_unsetDirtyForm() {
if (this.$scope.form) {
this.$scope.form.$setPristine();
}
}
addObservation() {
this.ticketObservations.push({description: null, ticketFk: this.params.id, showAddIcon: true});
this._setIconAdd();
}
removeObservation(index) {
let item = this.ticketObservations[index];
if (item) {
this.ticketObservations.splice(index, 1);
this._setIconAdd();
if (item.id) {
this.removedObservations.push(item.id);
this._setDirtyForm();
}
}
}
_equalObservations(oldObservation, newObservation) {
return oldObservation.id === newObservation.id && oldObservation.observationTypeFk === newObservation.observationTypeFk && oldObservation.description === newObservation.description;
}
setOldObservations(response) {
this._setIconAdd();
response.data.forEach(observation => {
this.oldObservations[observation.id] = Object.assign({}, observation);
});
}
getObservations() {
let filter = {
where: {ticketFk: this.params.id},
include: ['observationType']
};
this.$http.get(`/ticket/api/TicketObservations?filter=${JSON.stringify(filter)}`).then(response => {
this.ticketObservations = response.data;
this.setOldObservations(response);
});
}
submit() {
let typesDefined = [];
let repeatedType = false;
let canSubmit;
let observationsObj = {
delete: this.removedObservations,
create: [],
update: []
};
this.ticketObservations.forEach(observation => {
let isNewObservation = !observation.id;
delete observation.showAddIcon;
if (typesDefined.indexOf(observation.observationTypeFk) !== -1) {
repeatedType = true;
return;
}
typesDefined.push(observation.observationTypeFk);
if (isNewObservation && observation.description && observation.observationTypeFk) {
observationsObj.create.push(observation);
}
if (!isNewObservation && !this._equalObservations(this.oldObservations[observation.id], observation)) {
observationsObj.update.push(observation);
}
});
if (this.$scope.form.$invalid) {
return this.vnApp.showMessage(this.$translate.instant('Some fields are invalid'));
}
if (repeatedType) {
return this.vnApp.showMessage(this.$translate.instant('The observation type must be unique'));
}
canSubmit = observationsObj.update.length > 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0;
if (canSubmit) {
return this.$http.post(`/ticket/api/TicketObservations/crudTicketObservations`, observationsObj).then(() => {
this.getObservations();
this._unsetDirtyForm();
});
}
this.vnApp.showMessage(this.$translate.instant('No changes to save'));
}
$onInit() {
this.getObservations();
}
}
TicketObservations.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp'];
ngModule.component('vnTicketObservations', {
template: require('./ticket-observations.html'),
controller: TicketObservations,
bindings: {
ticket: '<'
}
});