vrfs1008: added existingInvites storage for updates
This commit is contained in:
parent
cfeed312bf
commit
61a7dcd50f
|
|
@ -10,9 +10,9 @@
|
|||
var userPhotoUrls = [];
|
||||
var friendSelectorDialog = null;
|
||||
var selectedFriendIds = {};
|
||||
var existingInvites = [];
|
||||
var autoComplete = null;
|
||||
var rest = context.JK.Rest();
|
||||
var mySaveCallback;
|
||||
var inviteAction = 'create'; // create/update
|
||||
|
||||
this.inviteSessionCreate = function(elemSelector) {
|
||||
|
|
@ -21,6 +21,7 @@
|
|||
};
|
||||
|
||||
this.inviteSessionUpdate = function(elemSelector, sessionId) {
|
||||
friendSelectorDialog.setCallback(friendSelectorCallback);
|
||||
inviteAction = 'update';
|
||||
if (0 == $(elemSelector + ' .friendbox').length) {
|
||||
_appendFriendSelector($(elemSelector));
|
||||
|
|
@ -32,9 +33,9 @@
|
|||
data: { session_id: sessionId, sender: context.JK.currentUserId }
|
||||
}).done(function(response) {
|
||||
response.map(function(item) {
|
||||
var dd = item['receiver']
|
||||
selectedFriendIds[dd['id']] = true;
|
||||
addInvitation(dd['name'], dd['id']);
|
||||
var dd = item['receiver'];
|
||||
existingInvites.push(dd.id);
|
||||
addInvitation(dd.name, dd.id);
|
||||
});
|
||||
}).fail(app.ajaxError);
|
||||
}
|
||||
|
|
@ -44,6 +45,7 @@
|
|||
userIds = [];
|
||||
userPhotoUrls = [];
|
||||
selectedFriendIds = {};
|
||||
existingInvites = [];
|
||||
$('.selected-friends').empty();
|
||||
};
|
||||
|
||||
|
|
@ -83,14 +85,19 @@
|
|||
function friendSelectorCallback(newSelections) {
|
||||
var keys = Object.keys(newSelections);
|
||||
for (var i=0; i < keys.length; i++) {
|
||||
addInvitation(newSelections[keys[i]].userName, newSelections[keys[i]].userId);
|
||||
var dd = newSelections[keys[i]];
|
||||
addInvitation(dd.userName, dd.userId);
|
||||
}
|
||||
}
|
||||
|
||||
function _inviteExists(userID) {
|
||||
return 0 <= existingInvites.indexOf(userID);
|
||||
}
|
||||
|
||||
function addInvitation(value, data) {
|
||||
if ($('.selected-friends div[user-id=' + data + ']').length === 0) {
|
||||
var template = $('#template-added-invitation').html();
|
||||
var imgStyle = inviteAction == 'update' ? 'display:none' : '';
|
||||
var imgStyle = _inviteExists(data) ? 'display:none' : '';
|
||||
var invitationHtml = context.JK.fillTemplate(template,
|
||||
{userId: data,
|
||||
userName: value,
|
||||
|
|
@ -98,36 +105,39 @@
|
|||
$('.selected-friends').append(invitationHtml);
|
||||
$('#friend-input').select();
|
||||
selectedFriendIds[data] = true;
|
||||
}
|
||||
else {
|
||||
|
||||
} else {
|
||||
$('#friend-input').select();
|
||||
context.alert('Invitation already exists for this musician.');
|
||||
}
|
||||
}
|
||||
|
||||
function removeInvitation(evt) {
|
||||
delete selectedFriendIds[$(evt.currentTarget).parent().attr('user-id')];
|
||||
var dd = $(evt.currentTarget).parent().attr('user-id');
|
||||
delete selectedFriendIds[dd];
|
||||
$(evt.currentTarget).closest('.invitation').remove();
|
||||
}
|
||||
|
||||
this.createInvitations = function(sessionId, onComplete) {
|
||||
var callCount = 0;
|
||||
var totalInvitations = 0;
|
||||
var totalInvitations = existingInvites.length;
|
||||
$('.selected-friends .invitation').each(function(index, invitation) {
|
||||
callCount++;
|
||||
totalInvitations++;
|
||||
var invite_id = $(invitation).attr('user-id');
|
||||
var invite = {
|
||||
music_session: sessionId,
|
||||
receiver: invite_id
|
||||
};
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/api/invitations",
|
||||
data: invite
|
||||
}).done(function(response) {
|
||||
callCount--;
|
||||
}).fail(app.ajaxError);
|
||||
if (!_inviteExists(invite_id)) {
|
||||
callCount++;
|
||||
totalInvitations++;
|
||||
var invite = {
|
||||
music_session: sessionId,
|
||||
receiver: invite_id
|
||||
};
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/api/invitations",
|
||||
data: invite
|
||||
}).done(function(response) {
|
||||
callCount--;
|
||||
}).fail(app.ajaxError);
|
||||
}
|
||||
});
|
||||
// TODO - this is the second time I've used this pattern.
|
||||
// refactor to make a common utility for this.
|
||||
|
|
@ -171,13 +181,6 @@
|
|||
});
|
||||
}
|
||||
|
||||
function _events() {
|
||||
$('#selected-friends-'+inviteAction).on("click", ".invitation a", removeInvitation);
|
||||
$('#btn-choose-friends-'+inviteAction).click(function(){
|
||||
friendSelectorDialog.showDialog(selectedFriendIds);
|
||||
});
|
||||
}
|
||||
|
||||
function _friendSelectorHTML() {
|
||||
return context.JK.fillTemplate($('#template-session-invite-musicians').html(),
|
||||
{choose_friends_id: 'btn-choose-friends-'+inviteAction,
|
||||
|
|
@ -186,20 +189,16 @@
|
|||
|
||||
function _appendFriendSelector(elemSelector) {
|
||||
elemSelector.append(_friendSelectorHTML());
|
||||
_events();
|
||||
$('#selected-friends-'+inviteAction).on("click", ".invitation a", removeInvitation);
|
||||
$('#btn-choose-friends-'+inviteAction).click(function(){
|
||||
friendSelectorDialog.showDialog(selectedFriendIds);
|
||||
});
|
||||
};
|
||||
|
||||
this.initialize = function(friendSelectorDialogInstance) {
|
||||
friendSelectorDialog = friendSelectorDialogInstance;
|
||||
};
|
||||
|
||||
this.setCallback = function(callback) {
|
||||
mySaveCallback = callback;
|
||||
}
|
||||
|
||||
this.searchFriends = searchFriends;
|
||||
this.addInvitation = addInvitation;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue