Merge branch 'dev' of https://git.verdnatura.es/salix into dev
This commit is contained in:
commit
0566dd5328
|
@ -3,4 +3,5 @@ Is equalizated: Recargo de equivalencia
|
||||||
Observation type: Tipo de observación
|
Observation type: Tipo de observación
|
||||||
Description: Descripción
|
Description: Descripción
|
||||||
The observation type must be unique: El tipo de observación ha de ser único
|
The observation type must be unique: El tipo de observación ha de ser único
|
||||||
Remove note: Quitar nota
|
Remove note: Quitar nota
|
||||||
|
Add note: Añadir nota
|
|
@ -10,7 +10,7 @@
|
||||||
data="contacts"
|
data="contacts"
|
||||||
form="form">
|
form="form">
|
||||||
</vn-watcher>
|
</vn-watcher>
|
||||||
<form name="form" ng-submit="$ctrl.submit()">
|
<form name="form" ng-submit="$ctrl.onSubmit()">
|
||||||
<vn-card pad-large>
|
<vn-card pad-large>
|
||||||
<vn-title>Contacts</vn-title>
|
<vn-title>Contacts</vn-title>
|
||||||
<vn-horizontal ng-repeat="contact in contacts">
|
<vn-horizontal ng-repeat="contact in contacts">
|
||||||
|
|
|
@ -7,33 +7,31 @@ class Controller {
|
||||||
this.$stateParams = $stateParams;
|
this.$stateParams = $stateParams;
|
||||||
this.$translate = $translate;
|
this.$translate = $translate;
|
||||||
this.vnApp = vnApp;
|
this.vnApp = vnApp;
|
||||||
this.removedContacts = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onDataChange() {
|
onDataChange() {
|
||||||
this.contacts = this.$scope.model.data;
|
this.contacts = this.$scope.model.data;
|
||||||
this.oldContacts = this.$scope.model.data;
|
this.oldInstances = Object.assign([], this.contacts);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the original contacts
|
|
||||||
* @param {Object} value - Default values
|
|
||||||
*/
|
|
||||||
set oldContacts(value) {
|
|
||||||
this._oldContacts = [];
|
|
||||||
this.removedContacts = [];
|
this.removedContacts = [];
|
||||||
|
|
||||||
value.forEach(item => {
|
|
||||||
this._oldContacts[item.id] = Object.assign({}, item);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get oldContacts() {
|
add() {
|
||||||
return this._oldContacts;
|
let data = {
|
||||||
|
clientFk: this.client.id,
|
||||||
|
name: this.$translate.instant('Phone'),
|
||||||
|
phone: null
|
||||||
|
};
|
||||||
|
this.contacts.push(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
submit() {
|
remove(index) {
|
||||||
let query = `/client/api/ClientContacts/crud`;
|
if (this.contacts[index] && this.contacts[index].id)
|
||||||
|
this.removedContacts.push(this.contacts[index].id);
|
||||||
|
|
||||||
|
this.contacts.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
onSubmit() {
|
||||||
let data = {
|
let data = {
|
||||||
clientFk: this.client.id,
|
clientFk: this.client.id,
|
||||||
delete: this.removedContacts,
|
delete: this.removedContacts,
|
||||||
|
@ -45,66 +43,23 @@ class Controller {
|
||||||
if (typeof item.id === 'undefined')
|
if (typeof item.id === 'undefined')
|
||||||
data.create.push(item);
|
data.create.push(item);
|
||||||
|
|
||||||
if (typeof item.id !== 'undefined' && !this.isEqual(item, this.oldContacts[item.id]))
|
if (typeof item.id !== 'undefined' && this.hasChanges(item))
|
||||||
data.update.push(item);
|
data.update.push(item);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!this.hasChanges(data))
|
this.$scope.watcher.check();
|
||||||
return this.vnApp.showError(this.$translate.instant('No changes to save'));
|
|
||||||
|
|
||||||
if (this.$scope.form.$invalid)
|
this.$http.post(`/client/api/ClientContacts/crud`, data).then(() => {
|
||||||
return this.vnApp.showError(this.$translate.instant('Some fields are invalid'));
|
this.$scope.watcher.notifySaved();
|
||||||
|
|
||||||
this.$http.post(query, data).then(() => {
|
|
||||||
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
|
|
||||||
this.$scope.model.refresh();
|
this.$scope.model.refresh();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
hasChanges(instance) {
|
||||||
* Remove contact from list
|
let oldInstance = this.oldInstances.find(item => {
|
||||||
* @param {Int} index - Contact array position
|
return item.id == instance.id;
|
||||||
*/
|
});
|
||||||
remove(index) {
|
return instance.name != oldInstance.name || instance.phone != oldInstance.phone;
|
||||||
if (this.contacts[index] && this.contacts[index].id)
|
|
||||||
this.removedContacts.push(this.contacts[index].id);
|
|
||||||
|
|
||||||
this.contacts.splice(index, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add contact to list
|
|
||||||
*/
|
|
||||||
add() {
|
|
||||||
let data = {
|
|
||||||
clientFk: this.client.id,
|
|
||||||
name: this.$translate.instant('Phone'),
|
|
||||||
phone: null
|
|
||||||
};
|
|
||||||
this.contacts.push(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true/false if the new
|
|
||||||
* contact equals the old one
|
|
||||||
* @param {Object} newContact - New contact
|
|
||||||
* @param {Object} oldContact - Old contact
|
|
||||||
* @return {Boolean} - True if are equals
|
|
||||||
*/
|
|
||||||
isEqual(newContact, oldContact) {
|
|
||||||
return newContact.name === oldContact.name && newContact.phone == oldContact.phone;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if there's any changes to do
|
|
||||||
* @param {Object} data - Crud data
|
|
||||||
* @return {Boolean} - Returns true if there's any changes to do
|
|
||||||
*/
|
|
||||||
hasChanges(data) {
|
|
||||||
if (data.create.length || data.update.length || data.delete.length)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,13 +19,32 @@ describe('Client', () => {
|
||||||
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
|
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
|
||||||
$scope = $rootScope.$new();
|
$scope = $rootScope.$new();
|
||||||
$scope.form = {$invalid: false};
|
$scope.form = {$invalid: false};
|
||||||
$scope.model = {refresh: () => {}};
|
$scope.model = {
|
||||||
|
refresh: () => {},
|
||||||
|
data: [
|
||||||
|
{id: 1, name: 'My contact 1', phone: '123456789'},
|
||||||
|
{id: 2, name: 'My contact 2', phone: '123456789'},
|
||||||
|
{id: 3, name: 'My contact 3', phone: '123456789'}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
$scope.watcher = {check: () => {}, notifySaved: () => {}};
|
||||||
controller = $componentController('vnClientContact', {$scope: $scope}, {$state: $state});
|
controller = $componentController('vnClientContact', {$scope: $scope}, {$state: $state});
|
||||||
controller.client = {
|
controller.client = {
|
||||||
id: 101
|
id: 101
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
describe('onDataChange()', () => {
|
||||||
|
it('should define contacts and oldInstances properties into controller', () => {
|
||||||
|
controller.onDataChange();
|
||||||
|
|
||||||
|
expect(controller.contacts).toBeDefined();
|
||||||
|
expect(controller.contacts.length).toEqual(3);
|
||||||
|
expect(controller.oldInstances).toBeDefined();
|
||||||
|
expect(controller.oldInstances.length).toEqual(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('add / remove tags', () => {
|
describe('add / remove tags', () => {
|
||||||
it('should add one empty contact into controller contacts collection', () => {
|
it('should add one empty contact into controller contacts collection', () => {
|
||||||
controller.contacts = [];
|
controller.contacts = [];
|
||||||
|
@ -37,6 +56,7 @@ describe('Client', () => {
|
||||||
|
|
||||||
it('should remove a contact that occupies the position in the index', () => {
|
it('should remove a contact that occupies the position in the index', () => {
|
||||||
let index = 2;
|
let index = 2;
|
||||||
|
controller.removedContacts = [];
|
||||||
controller.contacts = [
|
controller.contacts = [
|
||||||
{id: 1, name: 'My contact 1', phone: '123456789'},
|
{id: 1, name: 'My contact 1', phone: '123456789'},
|
||||||
{id: 2, name: 'My contact 2', phone: '123456789'},
|
{id: 2, name: 'My contact 2', phone: '123456789'},
|
||||||
|
@ -50,29 +70,30 @@ describe('Client', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('isEqual()', () => {
|
describe('hasChanges()', () => {
|
||||||
it('should return true if two contacts are equals', () => {
|
it('should return true if the instance has changes', () => {
|
||||||
let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'};
|
controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}];
|
||||||
let contact2 = {id: 1, name: 'My contact 1', phone: '123456789'};
|
let contact = {id: 1, name: 'My renamed contact', phone: '123456789'};
|
||||||
let equals = controller.isEqual(contact1, contact2);
|
let hasChanges = controller.hasChanges(contact);
|
||||||
|
|
||||||
expect(equals).toBeTruthy();
|
expect(hasChanges).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if two contacts aint equal', () => {
|
it(`should return false if the instance hasn't changes`, () => {
|
||||||
let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'};
|
controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}];
|
||||||
let contact2 = {id: 2, name: 'My contact 2', phone: '123456789'};
|
let contact = {id: 1, name: 'My contact 1', phone: '123456789'};
|
||||||
let equals = controller.isEqual(contact1, contact2);
|
let hasChanges = controller.hasChanges(contact);
|
||||||
|
|
||||||
expect(equals).toBeFalsy();
|
expect(hasChanges).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('submit()', () => {
|
describe('onSubmit()', () => {
|
||||||
it("should perfom a query to delete contacts", () => {
|
it("should perfom a query to delete contacts", () => {
|
||||||
controller.oldContacts = [];
|
controller.oldInstances = [
|
||||||
controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'};
|
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
|
||||||
controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'};
|
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
|
||||||
|
];
|
||||||
|
|
||||||
controller.contacts = [
|
controller.contacts = [
|
||||||
{id: 2, name: 'My contact 2', phone: '123456789'}
|
{id: 2, name: 'My contact 2', phone: '123456789'}
|
||||||
|
@ -88,14 +109,15 @@ describe('Client', () => {
|
||||||
|
|
||||||
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
||||||
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
||||||
controller.submit();
|
controller.onSubmit();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should perfom a query to update contacts", () => {
|
it("should perfom a query to update contacts", () => {
|
||||||
controller.oldContacts = [];
|
controller.oldInstances = [
|
||||||
controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'};
|
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
|
||||||
controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'};
|
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
|
||||||
|
];
|
||||||
|
|
||||||
controller.contacts = [
|
controller.contacts = [
|
||||||
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
|
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
|
||||||
|
@ -114,14 +136,15 @@ describe('Client', () => {
|
||||||
|
|
||||||
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
||||||
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
||||||
controller.submit();
|
controller.onSubmit();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should perfom a query to create new contact", () => {
|
it("should perfom a query to create new contact", () => {
|
||||||
controller.oldContacts = [];
|
controller.oldInstances = [
|
||||||
controller.oldContacts[1] = {id: 1, name: 'My contact 1', phone: '123456789'};
|
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'},
|
||||||
controller.oldContacts[2] = {id: 2, name: 'My contact 2', phone: '123456789'};
|
{id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}
|
||||||
|
];
|
||||||
|
|
||||||
controller.contacts = [
|
controller.contacts = [
|
||||||
{id: 1, name: 'My contact 1', phone: '123456789'},
|
{id: 1, name: 'My contact 1', phone: '123456789'},
|
||||||
|
@ -139,20 +162,9 @@ describe('Client', () => {
|
||||||
|
|
||||||
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
|
||||||
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
|
||||||
controller.submit();
|
controller.onSubmit();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw 'No changes to save' error when there are no changes to apply", () => {
|
|
||||||
let newData = {
|
|
||||||
delete: [],
|
|
||||||
create: [],
|
|
||||||
update: []
|
|
||||||
};
|
|
||||||
let hasChanges = controller.hasChanges(newData);
|
|
||||||
|
|
||||||
expect(hasChanges).toBeFalsy();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue