updated contacts

This commit is contained in:
Joan Sanchez 2018-08-27 15:11:57 +02:00
parent 26d2054be1
commit 1d14ca1a3f
4 changed files with 26 additions and 130 deletions

View File

@ -1,6 +1,7 @@
<vn-crud-model <vn-crud-model
vn-id="model" vn-id="model"
url="/client/api/ClientContacts" url="/client/api/ClientContacts"
fields="['id', 'clientFk', 'name', 'phone']"
link="{clientFk: $ctrl.$stateParams.id}" link="{clientFk: $ctrl.$stateParams.id}"
data="contacts" on-data-change="$ctrl.onDataChange()"> data="contacts" on-data-change="$ctrl.onDataChange()">
</vn-crud-model> </vn-crud-model>

View File

@ -11,8 +11,6 @@ class Controller {
onDataChange() { onDataChange() {
this.contacts = this.$scope.model.data; this.contacts = this.$scope.model.data;
this.oldInstances = Object.assign([], this.contacts);
this.removedContacts = [];
} }
add() { add() {
@ -25,41 +23,14 @@ class Controller {
} }
remove(index) { remove(index) {
if (this.contacts[index] && this.contacts[index].id)
this.removedContacts.push(this.contacts[index].id);
this.contacts.splice(index, 1); this.contacts.splice(index, 1);
} }
onSubmit() { onSubmit() {
let data = {
clientFk: this.client.id,
delete: this.removedContacts,
create: [],
update: []
};
this.contacts.forEach(item => {
if (typeof item.id === 'undefined')
data.create.push(item);
if (typeof item.id !== 'undefined' && this.hasChanges(item))
data.update.push(item);
});
this.$scope.watcher.check(); this.$scope.watcher.check();
this.$scope.watcher.realSubmit()
this.$http.post(`/client/api/ClientContacts/crud`, data).then(() => { .then(() => this.$scope.model.save(true))
this.$scope.watcher.notifySaved(); .then(() => this.$scope.watcher.notifySaved());
this.$scope.model.refresh();
});
}
hasChanges(instance) {
let oldInstance = this.oldInstances.find(item => {
return item.id == instance.id;
});
return instance.name != oldInstance.name || instance.phone != oldInstance.phone;
} }
} }

View File

@ -1,4 +1,5 @@
import './index.js'; import './index.js';
import {watcher} from '../helpers/watcherHelper.js';
describe('Client', () => { describe('Client', () => {
describe('Component vnClientContact', () => { describe('Component vnClientContact', () => {
@ -27,7 +28,7 @@ describe('Client', () => {
{id: 3, name: 'My contact 3', phone: '123456789'} {id: 3, name: 'My contact 3', phone: '123456789'}
] ]
}; };
$scope.watcher = {check: () => {}, notifySaved: () => {}}; $scope.watcher = watcher;
controller = $componentController('vnClientContact', {$scope: $scope}, {$state: $state}); controller = $componentController('vnClientContact', {$scope: $scope}, {$state: $state});
controller.client = { controller.client = {
id: 101 id: 101
@ -40,8 +41,6 @@ describe('Client', () => {
expect(controller.contacts).toBeDefined(); expect(controller.contacts).toBeDefined();
expect(controller.contacts.length).toEqual(3); expect(controller.contacts.length).toEqual(3);
expect(controller.oldInstances).toBeDefined();
expect(controller.oldInstances.length).toEqual(3);
}); });
}); });
@ -56,13 +55,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.onDataChange();
controller.contacts = [
{id: 1, name: 'My contact 1', phone: '123456789'},
{id: 2, name: 'My contact 2', phone: '123456789'},
{id: 3, name: 'My contact 3', phone: '123456789'}
];
controller.remove(index); controller.remove(index);
expect(controller.contacts.length).toEqual(2); expect(controller.contacts.length).toEqual(2);
@ -70,100 +63,17 @@ describe('Client', () => {
}); });
}); });
describe('hasChanges()', () => { xdescribe('onSubmit()', () => {
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(hasChanges).toBeTruthy();
});
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(hasChanges).toBeFalsy();
});
});
describe('onSubmit()', () => {
it("should perfom a query to delete contacts", () => { it("should perfom a query to delete contacts", () => {
controller.oldInstances = [ controller.$scope.model.data = [
{id: 1, clientFk: 101, name: 'My contact 1', phone: '123456789'}, {id: 1, 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'} {id: 2, name: 'My contact 2', phone: '123456789'}
]; ];
controller.removedContacts = [1]; spyOn(controller.$scope.watcher, 'notifySaved');
let newData = {
clientFk: 101,
delete: [1],
create: [],
update: []
};
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.onSubmit(); controller.onSubmit();
$httpBackend.flush();
});
it("should perfom a query to update contacts", () => { expect(controller.$scope.watcher.notifySaved).toHaveBeenCalledWith();
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'},
{id: 2, clientFk: 101, name: 'My contact 2', phone: '111111111'}
];
controller.removedContacts = [];
let newData = {
clientFk: 101,
delete: [],
create: [],
update: [
{id: 2, clientFk: 101, name: 'My contact 2', phone: '111111111'}
]
};
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.onSubmit();
$httpBackend.flush();
});
it("should perfom a query to create new contact", () => {
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'},
{id: 2, name: 'My contact 2', phone: '123456789'},
{name: 'My contact 3', phone: '123456789'}
];
controller.removedContacts = [];
let newData = {
clientFk: 101,
delete: [],
create: [{name: 'My contact 3', phone: '123456789'}],
update: []
};
$httpBackend.whenPOST(`/client/api/ClientContacts/crud`, newData).respond(200, true);
$httpBackend.expectPOST(`/client/api/ClientContacts/crud`, newData);
controller.onSubmit();
$httpBackend.flush();
}); });
}); });
}); });

View File

@ -0,0 +1,14 @@
module.exports.watcher = {
realSubmit: () => {
return {
then: () => {}
};
},
check: () => {},
save: () => {
return {
then: () => {}
};
},
notifySaved: () => {}
};