From 20c02f0f2ab160160e264d3be549808bf85c2dda Mon Sep 17 00:00:00 2001 From: Nuwan Date: Tue, 17 Oct 2023 08:30:23 +0530 Subject: [PATCH] wip on session create in beta site --- .../src/components/page/JKNewMusicSession.js | 16 +++- web/app/assets/javascripts/jam_rest.js | 20 +++-- .../javascripts/scheduled_session.js.erb | 86 +++++++++++++++++-- 3 files changed, 103 insertions(+), 19 deletions(-) diff --git a/jam-ui/src/components/page/JKNewMusicSession.js b/jam-ui/src/components/page/JKNewMusicSession.js index 12655cb06..b158f3d3c 100644 --- a/jam-ui/src/components/page/JKNewMusicSession.js +++ b/jam-ui/src/components/page/JKNewMusicSession.js @@ -8,6 +8,12 @@ import JKFriendsAutoComplete from '../people/JKFriendsAutoComplete'; import JKSessionInviteesChips from '../people/JKSessionInviteesChips'; import { getFriends } from '../../helpers/rest'; +const privacyMap = { + "public": 1, + "private_invite": 2, + "private_approve": 3 +} + const JKNewMusicSession = () => { const { currentUser } = useAuth(); const { t } = useTranslation(); @@ -69,7 +75,9 @@ const JKNewMusicSession = () => { //window.open jamkazam app url using custom URL scheme //an example URL would be: jamkazam://url=https://www.jamkazam.com/client#/createSession/privacy~2|description~hello|inviteeIds~1,2,3,4 const q = `privacy~${payload.privacy}|description~${payload.description}|inviteeIds~${payload.inviteeIds}` - window.open(`jamkazam://url=${process.env.REACT_APP_CLIENT_BASE_URL}/client#/createSession/${q}`) + const url = encodeURI(`${process.env.REACT_APP_CLIENT_BASE_URL}/client#/createSession/${q}`) + const urlScheme = `jamkazam://url=${url}` + window.open(urlScheme) try { //store this payload in localstorage. localStorage.setItem('formData', JSON.stringify(payload)) @@ -113,9 +121,9 @@ const JKNewMusicSession = () => { setPrivacy(e.target.value)} data-testid="session-privacy"> - - - + + + diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 7769e664b..a1d464287 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -1077,13 +1077,19 @@ } function createMusicianInvite(options) { - return $.ajax({ - type: "POST", - dataType: "json", - url: '/api/invitations', - contentType: 'application/json', - processData: false, - data: JSON.stringify(options) + return new Promise(function(resolve, reject){ + $.ajax({ + type: "POST", + dataType: "json", + url: '/api/invitations', + contentType: 'application/json', + processData: false, + data: JSON.stringify(options) + }).done(function(resp){ + resolve(resp) + }).fail(function(xhr){ + reject(xhr) + }) }) } diff --git a/web/app/assets/javascripts/scheduled_session.js.erb b/web/app/assets/javascripts/scheduled_session.js.erb index 26c1d6fd0..9cd833e1e 100644 --- a/web/app/assets/javascripts/scheduled_session.js.erb +++ b/web/app/assets/javascripts/scheduled_session.js.erb @@ -101,6 +101,12 @@ "5": "Adv" }; + var privacyMap = { + "public": "1", + "private_invite": "2", + "private_approve": "3" + } + function afterLoadScheduledSessions(sessionList) { $featureSessions.empty() @@ -570,7 +576,8 @@ createSessionSettings.timezone.label = "(GMT-06:00) Central Time (US & Canada)"; createSessionSettings.timezone.value = "Central Time (US & Canada),America/Chicago"; createSessionSettings.name = "Open Session"; - createSessionSettings.description = "Feel free to join this session, it's open!"; + if(createSessionSettings.description === undefined || createSessionSettings.description.length === 0) + createSessionSettings.description = "Feel free to join this session, it's open!"; createSessionSettings.notations = []; createSessionSettings.language.label = 'English'; createSessionSettings.language.value = 'eng'; @@ -908,7 +915,7 @@ logger.debug("created session on server"); $('#create-session-buttons .btn-next').off('click'); var newSessionId = response.id; - + createSessionSettings.newSessionId = newSessionId if (createSessionSettings.createType == '<%= MusicSession::CREATE_TYPE_QUICK_START %>' || createSessionSettings.createType == "immediately" || createSessionSettings.createType == '<%= MusicSession::CREATE_TYPE_QUICK_PUBLIC %>') { joinSession(newSessionId); } @@ -1518,21 +1525,80 @@ } //handle jamkazam:// custom URL scheme params - function decodeCustomSchemaParams(){ - const hash = context.location.hash; + //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 initCustomUrlScheme(){ + //an example URL would be: https://www.jamkazam.com/client#/createSession/privacy~2|description~hello|inviteeIds~1,2,3,4 + const hash = decodeURIComponent(context.location.hash); const qStr = hash.substring(hash.lastIndexOf('/') + 1); - //an example URL would be: https://www.jamkazam.com/client#/createSession/privacy~2|description~hello|inviteeIds~1,2,3,4 + //decode the query params according to the custom format const qParamsArr = qStr.split('|'); let privacy, description, inviteeIds; - qParamsArr.each(function(q){ + qParamsArr.forEach(function(q){ const qp = q.split('~') if(qp[0] === 'privacy') privacy = qp[1] if(qp[0] === 'description') description = qp[1] - if(qp[0] === 'inviteeIds') inviteeIds = qp[1] + if(qp[0] === 'inviteeIds') inviteeIds = qp[1] }) - + createSessionSettings.description = description; + alert(description) + switch(privacy){ + case privacyMap['private_invite']: + clickQuickStartFriends(); + break; + case privacyMap['private_approve']: + clickQuickStartSolo() + break; + case privacyMap['public']: + clickQuickStartPublic(); + break; + default: + logger.debug('Invalid session privacy value') + return + } + + waitUntilSessionCreated().then(function(){ + //now async send invitations + if(createSessionSettings.newSessionId && inviteeIds !== undefined){ + const inviteUserIds = inviteeIds.split(','); + inviteUserIds.forEach(function(inviteId){ + const invite = { + music_session: createSessionSettings.newSessionId, + receiver: inviteId + }; + rest.createMusicianInvite(invite).then(function(resp){ + console.log("Invitation was sent") + }).catch(function(error){ + logger.debug(error) + }) + }) + } + }).catch(function(error){ + logger.debug(error) + }); + } + + function waitUntilSessionCreated(){ + return new Promise(function(resolve, reject){ + const maxAttempts = 5; + let attempt = 0; + try{ + const sessionCreateInterval = setInterval(function(){ + attempt++; + console.log('_DEBUG_ trying to get the sessionId....', attempt) + if(createSessionSettings.newSessionId){ + clearInterval(sessionCreateInterval) + resolve() + }else if(attempt > maxAttempts){ + reject("Maximum number of attepts for getting a sessionId is exceeded.") + } + }, 1000) + }catch(error){ + reject(error) + } + }) } function initialize(invitationDialogInstance, friendSelectorDialog, instrumentSelectorInstance, instrumentRSVPSelectorInstance) { @@ -1583,6 +1649,10 @@ initializeControls(); events(); + + setTimeout(function(){ + initCustomUrlScheme(); + }, 1000); } this.initialize = initialize;