fixes #87: refactory of local add/remove of an attendee

This commit is contained in:
Davide Alberani 2016-04-17 15:28:02 +02:00
parent 40d396d872
commit 9fe7f21a8d
2 changed files with 52 additions and 48 deletions

View file

@ -103,7 +103,7 @@
<input name="job_title" class="form-control" placeholder="{{'Evil Ruler' | translate}}" ng-model="newPerson.job_title">
</div>
<button reset-focus ng-disabled="!(newPerson.name && newPerson.surname)" ng-click="fastAddPerson(newPerson, true)" class="btn btn-success top5">
<button reset-focus ng-disabled="!(newPerson.name && newPerson.surname)" ng-click="fastAddAttendee(newPerson, true)" class="btn btn-success top5">
<span class="fa fa-plus-circle vcenter"></span>
{{'Add' | translate}}
</button>
@ -129,7 +129,7 @@
{{person.email}}
</td>
<td class="text-left">
<button reset-focus ng-click="fastAddPerson(person)" type="button" class="btn btn-link fa fa-plus-circle vcenter"></button>
<button reset-focus ng-click="fastAddAttendee(person)" type="button" class="btn btn-link fa fa-plus-circle vcenter"></button>
</td>
</tr>
</tbody>

View file

@ -103,8 +103,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.message = {};
$scope.event = {};
$scope.event.persons = [];
$scope.customFields = Setting.query({setting: 'person_custom_field',
in_event_details: true});
$scope.customFields = Setting.query({setting: 'person_custom_field', in_event_details: true});
if ($stateParams.id) {
$scope.event = Event.get($stateParams, function() {
@ -128,6 +127,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
return;
}
var data = $scope.EventUpdates.data.update;
$log.debug('received ' + data.action + ' action from websocket');
if (!$scope.event.persons) {
$scope.event.persons = [];
}
@ -143,9 +143,9 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
$scope.event.persons[person_idx] = data.person;
} else if (data.action == 'add' && person_idx == -1) {
$scope.event.persons.push(data.person);
$scope._localAddAttendee(data.person, true);
} else if (data.action == 'delete' && person_idx != -1) {
$scope.event.persons.splice(person_idx, 1);
$scope._localRemoveAttendee({person_id: data.person_id});
}
}
);
@ -187,6 +187,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.calcAttendees = function() {
if (!($scope.event && $scope.event.persons)) {
$scope.countAttendees = 0;
return;
}
var attendees = 0;
@ -198,47 +199,45 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.countAttendees = attendees;
};
$scope._addPerson = function(person_data) {
var original_data = angular.copy(person_data);
person_data.person_id = person_data._id;
person_data._id = $stateParams.id;
Event.addPerson(person_data, function() {
if (!$scope.event.persons) {
$scope.event.persons = [];
}
$scope.event.persons.push(person_data);
$scope.setPersonAttribute(person_data, 'attended', true, function() {
var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
return person_data.person_id == el.person_id;
});
if (all_person_idx != -1) {
$scope.allPersons.splice(all_person_idx, 1);
}
$translate('{{person_name}} {{person_surname}} successfully added to event {{event_title}}',
{person_name: person_data.name, person_surname: person_data.surname, event_title: $scope.event.title}).then(
function (translation) {
$scope.showMessage({message: translation});
}
);
/* Stuff to do when an attendee is added locally. */
$scope._localAddAttendee = function(person, hideMessage) {
if (!$scope.event.persons) {
$scope.event.persons = [];
}
$scope.event.persons.push(person);
$scope.setPersonAttribute(person, 'attended', true, function() {
var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
return person.person_id == el.person_id;
});
if (all_person_idx != -1) {
$scope.allPersons.splice(all_person_idx, 1);
}
}, hideMessage);
};
$scope._addAttendee = function(person) {
person.person_id = person._id;
person._id = $stateParams.id;
Event.addPerson(person, function() {
$scope._localAddAttendee(person);
});
$scope.query = '';
};
$scope.fastAddPerson = function(person, isNew) {
$log.debug('EventDetailsCtrl.fastAddPerson.person:');
$scope.fastAddAttendee = function(person, isNew) {
$log.debug('EventDetailsCtrl.fastAddAttendee.person:');
$log.debug(person);
if (isNew) {
var personObj = new Person(person);
personObj.$save(function(p) {
$scope._addPerson(angular.copy(p));
$scope._addAttendee(angular.copy(p));
});
} else {
$scope._addPerson(angular.copy(person));
$scope._addAttendee(angular.copy(person));
}
};
$scope.setPersonAttribute = function(person, key, value, callback) {
$scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
$log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
$log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
$log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
@ -262,7 +261,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
if (callback) {
callback(data);
}
if (key === 'attended') {
if (key === 'attended' && !hideMessage) {
var msg = {};
if (value) {
msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
@ -280,24 +279,29 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.query = '';
};
/* Stuff to do when an attendee is removed locally. */
$scope._localRemoveAttendee = function(person) {
$log.debug('_localRemoveAttendee');
$log.debug(person);
if (!(person && person.person_id && $scope.event.persons)) {
return;
}
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return person.person_id == el.person_id;
});
if (person_idx == -1) {
$log.warn('unable to find and delete person_id ' + person.person_id);
return;
}
$scope.event.persons.splice(person_idx, 1);
};
$scope.removeAttendee = function(person) {
Event.deletePerson({
_id: $stateParams.id,
person_id: person.person_id
},
function(data) {
if (!(data && data.person_id && $scope.event.persons)) {
return;
}
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return data.person_id == el.person_id;
});
if (person_idx == -1) {
$log.warn('unable to find and delete person_id ' + data.person_id);
return;
}
$scope.event.persons.splice(person_idx, 1);
$scope.allPersons.push(person);
}, function() {
$scope._localRemoveAttendee(person);
});
};