diff --git a/web/app/assets/javascripts/asyncJamClient.js b/web/app/assets/javascripts/asyncJamClient.js index b22fc6e19..01cc75cb9 100644 --- a/web/app/assets/javascripts/asyncJamClient.js +++ b/web/app/assets/javascripts/asyncJamClient.js @@ -548,19 +548,22 @@ context.RecordingActions.audioRecordingFormatChanged(`.${audioFormat}`) break; case '3012': //customUrl - logger.log(`[asyncJamClient] 3012 CustomUrl: ${response['CustomUrl']}`); const customUrl = response['CustomUrl'] - //window.location.href = "http://www.jamkazam.local:3000/client#/createSession/custom~yes|privacy~2|description~hello|inviteeIds~27bd4a30-d1b8-4eea-8454-01a104d59381" - //window.location.href = "http://www.jamkazam.local:3000/client#/joinSession/custom~yes|joinSessionId~2" - window.location.href = customUrl + logger.log(`_DEBUG_ [asyncJamClient] 3012 CustomUrl: ${customUrl}`); + //if localStorage is available and has customUrl key, remove it + if(localStorage){ + if(localStorage.getItem('customUrl')){ + localStorage.removeItem('customUrl') + } + //now set the customUrl which received from backend + if(customUrl){ + const httpUrl = window.location.protocol + '//' + customUrl; + localStorage.setItem('customUrl', httpUrl); + window.location.href = httpUrl; + } + } + break; - // case '3013': //JKJoinSessionFromCustomUrlEvent - // logger.log(`[asyncJamClient] 3013 JKJoinSessionFromCustomUrlEvent: ${response['JKJoinSessionFromCustomUrlEvent']['CustomUrl']}`); - // if(context.JK.FindSessionScreen){ - // const customUrl = response['JKJoinSessionFromCustomUrlEvent']['CustomUrl'] - // context.JK.SessionUtils.joinSessionFromCustomUrlScheme(customUrl) - // } - // break; default: break; } diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index 6a95f50be..ceac53f95 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -153,7 +153,7 @@ // refreshes the currently active tab function renderActive() { - console.log("_DEBUG_ renderActive isAdmin: " + isAdmin + " isMember: " + isMember); + //console.log("_DEBUG_ renderActive isAdmin: " + isAdmin + " isMember: " + isMember); if (isMember) { $("#btn-follow-band").hide(); $("#btn-edit-band-profile").show(); @@ -549,7 +549,7 @@ error: app.ajaxError }) .done(function(response) { - console.log("_DEBUG_ determineMembership response: " + JSON.stringify(response)); + //console.log("_DEBUG_ determineMembership response: " + JSON.stringify(response)); isAdmin = isMember = false; $.each(response, function(index, val) { if (val.id === context.JK.currentUserId) { diff --git a/web/app/assets/javascripts/customUrlHandler.js b/web/app/assets/javascripts/customUrlHandler.js new file mode 100644 index 000000000..b90c539d8 --- /dev/null +++ b/web/app/assets/javascripts/customUrlHandler.js @@ -0,0 +1,114 @@ +(function (context, $) { + "use strict"; + + context.JK = context.JK || {}; + + context.JK.CustomUrlHandler = function () { + + var hashchangeEvent; + var loadEvent; + + function handleCustomUrlScheme(customUrl) { + + var maxAttempts = 20; + + //TODO: show a notification while waiting for the screen to load + + //extract the hash from the custom URL passed in + + var url = new URL(customUrl); + var hash = url.hash; + + if (hash) { // if the hash contains the custom yes flag + + var screenLoadInterval = setInterval(() => { + + //handle create session. #/createSession is a valid hash. switch to the createSession screen and handle creating a session from the custom URL + if (hash.includes("#/createSession")) { // if the hash contains the createSession flag + if (JK.createScheduledSessionScreen && JK.CreateScheduledSessionDataIsLoaded) { // if the createScheduledSessionScreen is loaded + JK.createScheduledSessionScreen.createSessionByCustomUrlScheme(hash); // this will create a session + clearInterval(screenLoadInterval); + } else { + console.log("attempting to create session. ", maxAttempts); + maxAttempts--; + if (maxAttempts <= 0) { + clearInterval(screenLoadInterval); + } + } + } + + //handle join session. there is no joinSession screen. JK.SessionUtils is a helper class that handles joining a session + if (hash.includes("#/joinSession")) { // if the hash contains the joinSession flag + if (JK.SessionUtils) { // if the SessionUtils is loaded + JK.SessionUtils.joinSessionFromCustomUrlScheme(hash); + clearInterval(screenLoadInterval); + } else { + console.log("attempting to join session", maxAttempts); + maxAttempts--; + if (maxAttempts <= 0) { + clearInterval(screenLoadInterval); + } + } + } + + }, 1000); // check every second + } + + } + + function getCustomUrl() { + //get customUrl from local storage + if (localStorage.getItem("customUrl")) { + try{ + return decodeURIComponent(localStorage.getItem("customUrl")); + } + catch(e){ + console.error("error decoding customUrl", e); + return null; + } + } + return null; + } + + function handle() { + //clear the event listeners + if (hashchangeEvent) { + window.removeEventListener('hashchange', hashchangeEvent); + } + if (loadEvent) { + window.removeEventListener('load', loadEvent); + } + // check periodically if localStorage has customUrl available for 10 seconds + //if available call handleCustomUrlScheme and clear the interval and localStorage customUrl + var attempts = 5; + var customUrlInterval = setInterval(() => { + const customUrl = getCustomUrl(); + if (customUrl) { + handleCustomUrlScheme(customUrl); + localStorage.removeItem("customUrl"); + clearInterval(customUrlInterval); + } + if (attempts < 0) { + localStorage.removeItem("customUrl"); + clearInterval(customUrlInterval); + } + attempts--; + }, 1000); + } + + function initialize() { + hashchangeEvent = window.addEventListener('hashchange', function () { + handle(); + }); + + loadEvent = window.addEventListener('load', (event) => { + handle(); + }); + } + + return { + initialize: initialize, + }; + + }; +})(window, jQuery); diff --git a/web/app/assets/javascripts/scheduled_session.js.erb b/web/app/assets/javascripts/scheduled_session.js.erb index 0f0214754..ad8ac551e 100644 --- a/web/app/assets/javascripts/scheduled_session.js.erb +++ b/web/app/assets/javascripts/scheduled_session.js.erb @@ -315,8 +315,13 @@ $fetchingSpinner.show(); $fetchingSpinnerLabel.show(); - Promise.all([fetchScheduledSessions(), fetchUserDetail()]).then(function() { + var p1 = fetchScheduledSessions(); + var p2 = fetchUserDetail(); + + Promise.all([p1, p2]).then(function() { context.JK.CreateScheduledSessionDataIsLoaded = true; + }).catch(function() { + context.JK.CreateScheduledSessionDataIsLoaded = false; }); } @@ -1554,6 +1559,7 @@ //when a user submits create new session form (in the new react website) //this custom url scheme is loaded and as a result the JamKazam app loads create session window. function createSessionByCustomUrlScheme(hash){ + var qStr = hash.substring(hash.lastIndexOf('/') + 1); @@ -1567,6 +1573,7 @@ if(qp[0] === 'description') description = qp[1] if(qp[0] === 'inviteeIds') inviteeIds = qp[1] }) + if(isCustom !== 'yes'){ return; } @@ -1593,7 +1600,6 @@ waitUntilSessionCreated().then(function(){ //now async send invitations - console.log('_DEBUG_ waitUntilSessionCreated createSessionSettings.newSessionId', createSessionSettings.newSessionId) if(createSessionSettings.newSessionId && inviteeIds !== undefined){ var inviteUserIds = inviteeIds.split(',').filter(Boolean); //filter to remove empty strings inviteUserIds.forEach(function(inviteId){ @@ -1613,7 +1619,6 @@ }) } }).catch(function(error){ - console.log('Error while creating session', error); logger.debug(error) }); } diff --git a/web/app/views/clients/index.html.erb b/web/app/views/clients/index.html.erb index 52d2d6fd6..983091e44 100644 --- a/web/app/views/clients/index.html.erb +++ b/web/app/views/clients/index.html.erb @@ -389,6 +389,10 @@ JK.app.initialRouting(); + var customUrlHandler = new JK.CustomUrlHandler(); + customUrlHandler.initialize(); + + JK.hideCurtain(300); } @@ -418,6 +422,8 @@ JK.RecordingUtils.init(); + + // Initialize the app JK.app.initialize(); // Let's get things rolling... @@ -449,62 +455,8 @@ }); - window.addEventListener('hashchange', function() { - console.log("_DEBUG_ hashchange event", window.location.hash); - handleCustomUrlScheme(); - }); - - window.addEventListener('load', (event) => { - console.log("_DEBUG_ load event", window.location.hash); - handleCustomUrlScheme(); - }); - - - function handleCustomUrlScheme(){ - // this is a hack to create a session from a custom URL scheme - // an example custom URL would be: https://www.jamkazam.com/client#/createSession/custom~yes|privacy~2|description~hello|inviteeIds~1,2,3,4 - - var maxAttempts = 5; - - //TODO: show a notification while waiting for the screen to load - - var hash = decodeURIComponent(window.location.hash) - - if(hash.includes("custom~yes")){ // if the hash contains the custom yes flag - - var screenLoadInterval = setInterval(() => { - - //handle create session. #/createSession is a valid hash. switch to the createSession screen and handle creating a session from the custom URL - if(hash.includes("#/createSession")){ // if the hash contains the createSession flag - if (JK.createScheduledSessionScreen && JK.CreateScheduledSessionDataIsLoaded) { // if the createScheduledSessionScreen is loaded - JK.createScheduledSessionScreen.createSessionByCustomUrlScheme(hash); // this will create a session - clearInterval(screenLoadInterval); - }else{ - console.log("attempting to create session. ", maxAttempts); - maxAttempts--; - if(maxAttempts <= 0){ - clearInterval(screenLoadInterval); - } - } - } - - //handle join session. there is no joinSession screen. JK.SessionUtils is a helper class that handles joining a session - if(hash.includes("#/joinSession")){ // if the hash contains the joinSession flag - if (JK.SessionUtils) { // if the SessionUtils is loaded - JK.SessionUtils.joinSessionFromCustomUrlScheme(hash); - clearInterval(screenLoadInterval); - }else{ - console.log("attempting to join session", maxAttempts); - maxAttempts--; - if(maxAttempts <= 0){ - clearInterval(screenLoadInterval); - } - } - } - - }, 1000); // check every second - } - + window.onUnloaded = function() { + JK.JamServer.disconnect(); }