diff --git a/client/client/src/address-edit/address-edit.html b/client/client/src/address-edit/address-edit.html index f703c38030..e846eadfe9 100644 --- a/client/client/src/address-edit/address-edit.html +++ b/client/client/src/address-edit/address-edit.html @@ -77,7 +77,7 @@ margin-medium-left orange icon="add_circle" - ng-if = "observation.showAddIcon" + ng-if = "observation.showAddIcon && observationsTypes.model.length > $ctrl.observations.length" ng-click="$ctrl.addObservation()" > diff --git a/client/client/src/address-edit/address-edit.js b/client/client/src/address-edit/address-edit.js index 8d7a569948..90009cbe82 100644 --- a/client/client/src/address-edit/address-edit.js +++ b/client/client/src/address-edit/address-edit.js @@ -13,7 +13,7 @@ export default class Controller { id: parseInt($state.params.addressId) }; this.observations = []; - this.observationsDictionary = {}; + this.observationsOld = {}; this.observationsRemoved = []; } @@ -60,11 +60,16 @@ export default class Controller { return this.$http.post(`/client/api/AddressObservations/crudAddressObservations`, objectObservations); } + _observationsEquals(ob1, ob2) { + return ob1.observationTypeFk === ob2.observationTypeFk && ob1.description === ob2.description; + } + submit() { this._unsetDirtyForm(); let submitWatcher = this.$scope.watcher.dataChanged(); let submitObservations; let repeatedTypes = false; + let types = []; let observationsObj = { delete: this.observationsRemoved, create: [], @@ -73,31 +78,27 @@ export default class Controller { for (let i = 0; i < this.observations.length; i++) { let observation = this.observations[i]; - // only one observation is allowed for each of its types - if (this.observationsDictionary[observation.observationTypeFk] !== undefined && // IF the dictionary contains the type - ( - // AND (is a new Observation OR is old but with distinct Id) --> repeated - !observation.id || (observation.id && this.observationsDictionary[observation.observationTypeFk].id !== observation.id) - ) - ) { + let isNewObservation = observation.id === undefined; + + if (types.indexOf(observation.observationTypeFk) !== -1) { repeatedTypes = true; break; } - if (!observation.id && observation.observationTypeFk && observation.description) { + types.push(observation.observationTypeFk); + + if (isNewObservation && observation.observationTypeFk && observation.description) { observationsObj.create.push(observation); - } else if (observation.id && this.observationsDictionary[observation.observationTypeFk].description !== observation.description) { + } else if (!isNewObservation && !this._observationsEquals(this.observationsOld[observation.id], observation)) { observationsObj.update.push(observation); } - - this.observationsDictionary[observation.observationTypeFk] = observation; } submitObservations = observationsObj.update.length > 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0; if (repeatedTypes) { this.vnApp.showMessage( - this.$translate.instant('you can not repeat the types of observations') + this.$translate.instant('The observation type must be unique') ); } else if (submitWatcher && !submitObservations) { this.$scope.watcher.submit().then(() => { @@ -126,7 +127,7 @@ export default class Controller { this.$http.get(`/client/api/AddressObservations?filter=${JSON.stringify(filter)}`).then(res => { this.observations = res.data; res.data.forEach(item => { - this.observationsDictionary[item.observationTypeFk] = Object.assign({}, item); + this.observationsOld[item.id] = Object.assign({}, item); }); }); } diff --git a/client/client/src/address-edit/locale/es.yml b/client/client/src/address-edit/locale/es.yml index aad6a3c875..78f2360394 100644 --- a/client/client/src/address-edit/locale/es.yml +++ b/client/client/src/address-edit/locale/es.yml @@ -1,2 +1,5 @@ Enabled: Activo -Is equalizated: Recargo de equivalencia \ No newline at end of file +Is equalizated: Recargo de equivalencia +Observation type: Tipo de observación +Description: Descripción +The observation type must be unique: El tipo de observación ha de ser único \ No newline at end of file