From 1c17adcd253ac5ca8bfdb57af455b03e3c8c82fe Mon Sep 17 00:00:00 2001 From: Joan Date: Mon, 27 Aug 2018 11:22:59 +0200 Subject: [PATCH 1/2] refactor client contact --- client/client/src/contact/index.html | 2 +- client/client/src/contact/index.js | 93 +++++++------------------ client/client/src/contact/index.spec.js | 86 +++++++++++++---------- 3 files changed, 77 insertions(+), 104 deletions(-) diff --git a/client/client/src/contact/index.html b/client/client/src/contact/index.html index f4c1299cc..ff43bae93 100644 --- a/client/client/src/contact/index.html +++ b/client/client/src/contact/index.html @@ -10,7 +10,7 @@ data="contacts" form="form"> -
+ Contacts diff --git a/client/client/src/contact/index.js b/client/client/src/contact/index.js index 06b500802..860833d78 100644 --- a/client/client/src/contact/index.js +++ b/client/client/src/contact/index.js @@ -7,33 +7,31 @@ class Controller { this.$stateParams = $stateParams; this.$translate = $translate; this.vnApp = vnApp; - this.removedContacts = []; } onDataChange() { this.contacts = this.$scope.model.data; - this.oldContacts = this.$scope.model.data; - } - -/** - * Saves the original contacts - * @param {Object} value - Default values - */ - set oldContacts(value) { - this._oldContacts = []; + this.oldInstances = Object.assign([], this.contacts); this.removedContacts = []; - - value.forEach(item => { - this._oldContacts[item.id] = Object.assign({}, item); - }); } - get oldContacts() { - return this._oldContacts; + add() { + let data = { + clientFk: this.client.id, + name: this.$translate.instant('Phone'), + phone: null + }; + this.contacts.push(data); } - submit() { - let query = `/client/api/ClientContacts/crud`; + remove(index) { + if (this.contacts[index] && this.contacts[index].id) + this.removedContacts.push(this.contacts[index].id); + + this.contacts.splice(index, 1); + } + + onSubmit() { let data = { clientFk: this.client.id, delete: this.removedContacts, @@ -45,66 +43,29 @@ class Controller { if (typeof item.id === 'undefined') 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); }); - if (!this.hasChanges(data)) - return this.vnApp.showError(this.$translate.instant('No changes to save')); + this.$scope.watcher.check(); - if (this.$scope.form.$invalid) - return this.vnApp.showError(this.$translate.instant('Some fields are invalid')); - - this.$http.post(query, data).then(() => { - this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.$http.post(`/client/api/ClientContacts/crud`, data).then(() => { + this.$scope.watcher.notifySaved(); this.$scope.model.refresh(); }); } -/** - * Remove contact from list - * @param {Int} index - Contact array position - */ - remove(index) { - 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 + * instance equals the old one + * @param {Object} instance - New instance * @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; + hasChanges(instance) { + let oldInstance = this.oldInstances.find(item => { + return item.id == instance.id; + }); + return instance.name != oldInstance.name || instance.phone != oldInstance.phone; } } diff --git a/client/client/src/contact/index.spec.js b/client/client/src/contact/index.spec.js index 6d8279c83..bf4d1584a 100644 --- a/client/client/src/contact/index.spec.js +++ b/client/client/src/contact/index.spec.js @@ -1,6 +1,6 @@ import './index.js'; -describe('Client', () => { +fdescribe('Client', () => { describe('Component vnClientContact', () => { let $componentController; let $scope; @@ -19,13 +19,32 @@ describe('Client', () => { $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = $rootScope.$new(); $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.client = { 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', () => { it('should add one empty contact into controller contacts collection', () => { controller.contacts = []; @@ -37,6 +56,7 @@ describe('Client', () => { it('should remove a contact that occupies the position in the index', () => { let index = 2; + controller.removedContacts = []; controller.contacts = [ {id: 1, name: 'My contact 1', phone: '123456789'}, {id: 2, name: 'My contact 2', phone: '123456789'}, @@ -50,29 +70,30 @@ describe('Client', () => { }); }); - describe('isEqual()', () => { - it('should return true if two contacts are equals', () => { - let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'}; - let contact2 = {id: 1, name: 'My contact 1', phone: '123456789'}; - let equals = controller.isEqual(contact1, contact2); + describe('hasChanges()', () => { + it('should return true if the instance has changes', () => { + controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}]; + let contact = {id: 1, name: 'My renamed contact', phone: '123456789'}; + let hasChanges = controller.hasChanges(contact); - expect(equals).toBeTruthy(); + expect(hasChanges).toBeTruthy(); }); - it('should return false if two contacts aint equal', () => { - let contact1 = {id: 1, name: 'My contact 1', phone: '123456789'}; - let contact2 = {id: 2, name: 'My contact 2', phone: '123456789'}; - let equals = controller.isEqual(contact1, contact2); + it(`should return false if the instance hasn't changes`, () => { + controller.oldInstances = [{id: 1, name: 'My contact 1', phone: '123456789'}]; + let contact = {id: 1, name: 'My contact 1', phone: '123456789'}; + let hasChanges = controller.hasChanges(contact); - expect(equals).toBeFalsy(); + expect(hasChanges).toBeFalsy(); }); }); - describe('submit()', () => { + describe('onSubmit()', () => { it("should perfom a query to delete contacts", () => { - controller.oldContacts = []; - controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}; - controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}; + controller.oldInstances = [ + {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}, + {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'} + ]; controller.contacts = [ {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.expectPOST(`/client/api/ClientContacts/crud`, newData); - controller.submit(); + controller.onSubmit(); $httpBackend.flush(); }); it("should perfom a query to update contacts", () => { - controller.oldContacts = []; - controller.oldContacts[1] = {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}; - controller.oldContacts[2] = {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'}; + controller.oldInstances = [ + {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}, + {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'} + ]; controller.contacts = [ {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.expectPOST(`/client/api/ClientContacts/crud`, newData); - controller.submit(); + controller.onSubmit(); $httpBackend.flush(); }); it("should perfom a query to create new contact", () => { - controller.oldContacts = []; - controller.oldContacts[1] = {id: 1, name: 'My contact 1', phone: '123456789'}; - controller.oldContacts[2] = {id: 2, name: 'My contact 2', phone: '123456789'}; + controller.oldInstances = [ + {id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}, + {id: 2, clientFk: 101, name: 'My contact 2', phone: '123456789'} + ]; controller.contacts = [ {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.expectPOST(`/client/api/ClientContacts/crud`, newData); - controller.submit(); + controller.onSubmit(); $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(); - }); }); }); }); From 26d2054be14c67088627961a9d7fd92bacafac0d Mon Sep 17 00:00:00 2001 From: Joan Date: Mon, 27 Aug 2018 11:27:32 +0200 Subject: [PATCH 2/2] removed focus --- client/client/src/address/edit/locale/es.yml | 3 ++- client/client/src/contact/index.js | 6 ------ client/client/src/contact/index.spec.js | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/client/client/src/address/edit/locale/es.yml b/client/client/src/address/edit/locale/es.yml index 992621365..f1aa52834 100644 --- a/client/client/src/address/edit/locale/es.yml +++ b/client/client/src/address/edit/locale/es.yml @@ -3,4 +3,5 @@ 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 -Remove note: Quitar nota \ No newline at end of file +Remove note: Quitar nota +Add note: Añadir nota \ No newline at end of file diff --git a/client/client/src/contact/index.js b/client/client/src/contact/index.js index 860833d78..00de287e3 100644 --- a/client/client/src/contact/index.js +++ b/client/client/src/contact/index.js @@ -55,12 +55,6 @@ class Controller { }); } -/** - * Returns true/false if the new - * instance equals the old one - * @param {Object} instance - New instance - * @return {Boolean} - True if are equals - */ hasChanges(instance) { let oldInstance = this.oldInstances.find(item => { return item.id == instance.id; diff --git a/client/client/src/contact/index.spec.js b/client/client/src/contact/index.spec.js index bf4d1584a..febe8b5da 100644 --- a/client/client/src/contact/index.spec.js +++ b/client/client/src/contact/index.spec.js @@ -1,6 +1,6 @@ import './index.js'; -fdescribe('Client', () => { +describe('Client', () => { describe('Component vnClientContact', () => { let $componentController; let $scope;