From 272e4747ab03f2e94b4f50a85825cd33ed2d318c Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 30 Nov 2017 18:23:27 -0800 Subject: [PATCH] minimized cookie usage as state --- public/assets/js/createChannelFunctions.js | 12 +++++------ public/assets/js/loginFunctions.js | 23 +++++++--------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/public/assets/js/createChannelFunctions.js b/public/assets/js/createChannelFunctions.js index dbcf4e49..fb0129d8 100644 --- a/public/assets/js/createChannelFunctions.js +++ b/public/assets/js/createChannelFunctions.js @@ -33,15 +33,13 @@ function publishNewChannel (event) { return sendAuthRequest(userName, password, '/signup') // post the request }) .then(result => { + setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); showChannelCreateDoneDisplay(); - // refresh window logged in as the channel - setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); // set cookies - }) - .then(() => { + // if user is on the home page, update the needed elements without reloading if (window.location.pathname === '/') { - // remove old channel and replace with new one & select it - replaceChannelOptionInPublishChannelSelect(); - replaceChannelOptionInNavBarChannelSelect(); + replaceChannelOptionInPublishChannelSelect(result.channelName); + replaceChannelOptionInNavBarChannelSelect(result.channelName); + // if user is not on home page, redirect to home page } else { window.location = '/'; } diff --git a/public/assets/js/loginFunctions.js b/public/assets/js/loginFunctions.js index 5f3a13b0..569cbb53 100644 --- a/public/assets/js/loginFunctions.js +++ b/public/assets/js/loginFunctions.js @@ -1,11 +1,9 @@ -function replaceChannelOptionInPublishChannelSelect() { +function replaceChannelOptionInPublishChannelSelect(loggedInChannel) { // remove the old channel option const oldChannel = document.getElementById('publish-channel-select-channel-option') if (oldChannel){ oldChannel.parentNode.removeChild(oldChannel); } - // get channel details from cookies - const loggedInChannel = getCookie('channel_name'); // create new channel option const newChannelOption = document.createElement('option'); newChannelOption.setAttribute('value', loggedInChannel); @@ -19,14 +17,12 @@ function replaceChannelOptionInPublishChannelSelect() { toggleSelectedChannel(loggedInChannel); } -function replaceChannelOptionInNavBarChannelSelect () { +function replaceChannelOptionInNavBarChannelSelect (loggedInChannel) { // remove the old channel option const oldChannel = document.getElementById('nav-bar-channel-select-channel-option'); if (oldChannel){ oldChannel.parentNode.removeChild(oldChannel); } - // get channel details from cookies - const loggedInChannel = getCookie('channel_name'); // create new channel option & select it const newChannelOption = document.createElement('option'); newChannelOption.setAttribute('value', loggedInChannel); @@ -49,20 +45,15 @@ function loginToChannel (event) { event.preventDefault() validationFunctions.validateNewChannelLogin(userName, password) .then(() => { - // send request return sendAuthRequest(userName, password, '/login') }) .then(result => { - // update session cookie with new channel name and id's - setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); // replace the current cookies - }) - .then(() => { - // update channel selection + setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); + // if user is on the home page, update the needed elements without reloading if (window.location.pathname === '/') { - // remove old channel and replace with new one & select it - replaceChannelOptionInPublishChannelSelect(); - // remove old channel and replace with new one & select it - replaceChannelOptionInNavBarChannelSelect(); + replaceChannelOptionInPublishChannelSelect(result.channelName); + replaceChannelOptionInNavBarChannelSelect(result.channelName); + // if user is not on home page, redirect to home page } else { window.location = '/'; }