diff --git a/web/app/assets/javascripts/alert.js b/web/app/assets/javascripts/alert.js index 077240d4e..7ba13363c 100644 --- a/web/app/assets/javascripts/alert.js +++ b/web/app/assets/javascripts/alert.js @@ -10,7 +10,9 @@ function events() { $('#btn-alert-ok').unbind("click"); $('#btn-alert-ok').click(function(evt) { - callback(sessionId); + if (callback) { + callback(sessionId); + } }); $('#btn-alert-cancel').unbind("click"); diff --git a/web/app/assets/javascripts/sessionList.js b/web/app/assets/javascripts/sessionList.js index 131c987aa..49af23e00 100644 --- a/web/app/assets/javascripts/sessionList.js +++ b/web/app/assets/javascripts/sessionList.js @@ -137,19 +137,69 @@ // wire up the Join Link to the T&Cs dialog var $parentRow = $('tr[id=' + session.id + ']', tbGroup); - if (session.approval_required) { - $('#join-link', $parentRow).click(function(evt) { - openAlert(session.id); - }); - } - else { - $('#join-link', $parentRow).click(function(evt) { - openTerms(session.id); - }); - } + $('#join-link', $parentRow).click(function(evt) { + joinClick(session.id); + }); } } + function joinClick(sessionId) { + var hasInvitation = false; + var session = null; + // we need to do a real-time check of the session in case the settings have + // changed while the user was sitting on the Find Session screen + $.ajax({ + type: "GET", + url: "/api/sessions/" + sessionId, + async: false, + success: function(response) { + session = response; + if ("invitations" in session) { + var invitation; + // user has invitations for this session + for (var i=0; i < session.invitations.length; i++) { + invitation = session.invitations[i]; + // session contains an invitation for this user + if (invitation.receiver_id === context.JK.currentUserId) { + hasInvitation = true; + } + } + } + + if (session) { + // if user has an invitation, always open terms and allow joining regardless of settings + if (hasInvitation) { + logger.debug("Found invitation for user " + context.JK.currentUserId + ", session " + sessionId); + openTerms(sessionId); + } + else { + if (session.musician_access) { + if (session.approval_required) { + openAlert(sessionId); + } + else { + openTerms(sessionId); + } + } + } + } + }, + error: function(xhr, textStatus, errorMessage) { + logger.debug("xhr.status = " + xhr.status); + if (xhr.status === 404) { + sessionNotJoinableAlert(); + } + else { + app.notify( + { title: "Unable to Join Session", + text: "There was an unexpected error while attempting to join the session." + }, + { no_cancel: true }); + } + } + }); + } + function openAlert(sessionId) { var alertDialog = new context.JK.AlertDialog(app, "YES", "You must be approved to join this session. Would you like to send a request to join?", @@ -159,6 +209,19 @@ app.layout.showDialog('alert'); } + function sessionNotJoinableAlert() { + var alertDialog = new context.JK.AlertDialog(app, "OK", + "This session is over or is no longer public and cannot be joined. Please click Refresh to update the session list.", + null, + function(evt) { + app.layout.closeDialog('alert'); + } + ); + + alertDialog.initialize(); + app.layout.showDialog('alert'); + } + function onCreateJoinRequest(sessionId) { var joinRequest = {}; joinRequest.music_session = sessionId;