From d50d0faabe655799e09699a0e3519e34ede118c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 29 Sep 2015 14:58:56 +0200 Subject: [PATCH 1/2] Write generic function for extracting subdomain and implement troughout whole app --- js/app.js | 10 ++++++---- js/components/whitelabel/prize/prize_app.js | 5 ++++- js/components/whitelabel/wallet/wallet_app.js | 4 +++- js/fetchers/license_fetcher.js | 5 ++++- js/fetchers/whitelabel_fetcher.js | 5 ++++- js/third_party/intercom.js | 4 +++- js/third_party/notifications.js | 6 ++++-- js/utils/general_utils.js | 11 +++++++++++ 8 files changed, 39 insertions(+), 11 deletions(-) diff --git a/js/app.js b/js/app.js index 30a57d2b..bd1b839a 100644 --- a/js/app.js +++ b/js/app.js @@ -10,13 +10,15 @@ import fetch from 'isomorphic-fetch'; /* eslint-enable */ import ApiUrls from './constants/api_urls'; -import { updateApiUrls } from './constants/api_urls'; -import appConstants from './constants/application_constants'; + +import AppConstants from './constants/application_constants'; import getRoutes from './routes'; import requests from './utils/requests'; +import { updateApiUrls } from './constants/api_urls'; import { getSubdomainSettings } from './utils/constants_utils'; import { initLogging } from './utils/error_utils'; +import { getSubdomain } from './utils/general_utils'; import EventActions from './actions/event_actions'; @@ -48,11 +50,11 @@ requests.defaults({ class AppGateway { start() { let settings; - let subdomain = window.location.host.split('.')[0]; + let subdomain = getSubdomain(); try { settings = getSubdomainSettings(subdomain); - appConstants.whitelabel = settings; + AppConstants.whitelabel = settings; updateApiUrls(settings.type, subdomain); this.load(settings); } catch(err) { diff --git a/js/components/whitelabel/prize/prize_app.js b/js/components/whitelabel/prize/prize_app.js index f187dd1c..e0be7707 100644 --- a/js/components/whitelabel/prize/prize_app.js +++ b/js/components/whitelabel/prize/prize_app.js @@ -9,6 +9,9 @@ import GlobalNotification from '../../global_notification'; import getRoutes from './prize_routes'; +import { getSubdomain } from '../../../utils/general_utils'; + + let RouteHandler = Router.RouteHandler; let PrizeApp = React.createClass({ @@ -16,7 +19,7 @@ let PrizeApp = React.createClass({ render() { let header = null; - let subdomain = window.location.host.split('.')[0]; + let subdomain = getSubdomain(); let ROUTES = getRoutes(null, subdomain); diff --git a/js/components/whitelabel/wallet/wallet_app.js b/js/components/whitelabel/wallet/wallet_app.js index 2a235cef..bbc58689 100644 --- a/js/components/whitelabel/wallet/wallet_app.js +++ b/js/components/whitelabel/wallet/wallet_app.js @@ -10,6 +10,8 @@ import GlobalNotification from '../../global_notification'; import getRoutes from './wallet_routes'; import classNames from 'classnames'; +import { getSubdomain } from '../../../utils/general_utils'; + let RouteHandler = Router.RouteHandler; @@ -18,7 +20,7 @@ let WalletApp = React.createClass({ mixins: [Router.State], render() { - let subdomain = window.location.host.split('.')[0]; + let subdomain = getSubdomain(); let ROUTES = getRoutes(null, subdomain); let activeRoutes = this.getRoutes().map(elem => 'route--' + elem.name); diff --git a/js/fetchers/license_fetcher.js b/js/fetchers/license_fetcher.js index 6b0cada0..d5b296be 100644 --- a/js/fetchers/license_fetcher.js +++ b/js/fetchers/license_fetcher.js @@ -2,12 +2,15 @@ import requests from '../utils/requests'; +import { getSubdomain } from '../utils/general_utils'; + + let LicenseFetcher = { /** * Fetch the available licenses from the API (might be bound to the subdomain e.g. cc.ascribe.io). */ fetch() { - return requests.get('licenses', {'subdomain': window.location.host.split('.')[0]}); + return requests.get('licenses', {'subdomain': getSubdomain()}); } }; diff --git a/js/fetchers/whitelabel_fetcher.js b/js/fetchers/whitelabel_fetcher.js index 51f4ed0f..7a39f676 100644 --- a/js/fetchers/whitelabel_fetcher.js +++ b/js/fetchers/whitelabel_fetcher.js @@ -2,12 +2,15 @@ import requests from '../utils/requests'; +import { getSubdomain } from '../utils/general_utils'; + + let WhitelabelFetcher = { /** * Fetch the custom whitelabel data from the API. */ fetch() { - return requests.get('whitelabel_settings', {'subdomain': window.location.host.split('.')[0]}); + return requests.get('whitelabel_settings', {'subdomain': getSubdomain()}); } }; diff --git a/js/third_party/intercom.js b/js/third_party/intercom.js index ef284d7b..42ecccf7 100644 --- a/js/third_party/intercom.js +++ b/js/third_party/intercom.js @@ -3,6 +3,8 @@ import alt from '../alt'; import EventActions from '../actions/event_actions'; +import { getSubdomain } from '../utils/general_utils'; + class IntercomHandler { constructor() { @@ -20,7 +22,7 @@ class IntercomHandler { /* eslint-enable */ app_id: 'oboxh5w1', email: profile.email, - subdomain: window.location.host.split('.')[0], + subdomain: getSubdomain(), widget: { activator: '#IntercomDefaultWidget' } diff --git a/js/third_party/notifications.js b/js/third_party/notifications.js index 8887bbe2..eeac1bff 100644 --- a/js/third_party/notifications.js +++ b/js/third_party/notifications.js @@ -5,6 +5,8 @@ import EventActions from '../actions/event_actions'; import NotificationActions from '../actions/notification_actions'; +import { getSubdomain } from '../utils/general_utils'; + class NotificationsHandler { @@ -13,11 +15,11 @@ class NotificationsHandler { this.loaded = false; } - onProfileDidLoad(profile) { + onProfileDidLoad() { if (this.loaded) { return; } - let subdomain = window.location.host.split('.')[0]; + let subdomain = getSubdomain(); if (subdomain === 'ikonotv') { NotificationActions.fetchContractAgreementListNotifications().then( (res) => { diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index ba2b7752..8f4aebbc 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -221,4 +221,15 @@ export function truncateTextAtCharIndex(text, charIndex, replacement = '...') { truncatedText += text.length > charIndex ? replacement : ''; return truncatedText; +} + +/** + * Extracts the user's subdomain from the browser's window. + * If no subdomain is found (for example on a naked domain), the default "www" is just assumed. + * @return {string} subdomain as a string + */ +export function getSubdomain() { + let { host } = window.location; + let matchedHost = host.match(/(([^.]+)\.)?(.*)\.(.*)/); + return matchedHost[2] || 'www'; } \ No newline at end of file From aa908a1c57444f8c52c7f729fb7a515e57335089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Wed, 30 Sep 2015 11:09:04 +0200 Subject: [PATCH 2/2] PR Feedback: Simplify subdomain extraction --- js/utils/general_utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index 8f4aebbc..2cba98b9 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -230,6 +230,6 @@ export function truncateTextAtCharIndex(text, charIndex, replacement = '...') { */ export function getSubdomain() { let { host } = window.location; - let matchedHost = host.match(/(([^.]+)\.)?(.*)\.(.*)/); - return matchedHost[2] || 'www'; + let tokens = host.split('.'); + return tokens.length > 2 ? tokens[0] : 'www'; } \ No newline at end of file