1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 16:48:04 +02:00
onion/js/app.js

98 lines
3.1 KiB
JavaScript
Raw Normal View History

import 'core-js/es6';
import 'core-js/stage/4';
2016-01-05 13:38:58 +01:00
import 'classlist-polyfill';
import 'isomorphic-fetch';
2015-07-03 10:40:04 +02:00
2015-05-13 16:26:12 +02:00
import React from 'react';
2016-01-25 13:33:20 +01:00
import ReactDOM from 'react-dom';
import Router from 'react-router/es6/Router';
2015-06-01 14:22:04 +02:00
2016-06-03 17:52:38 +02:00
import AppResolver from './app_resolver';
import history from './history';
2015-06-01 14:22:04 +02:00
import AppConstants from './constants/application_constants';
import { getDefaultSubdomainSettings, getSubdomainSettings } from './utils/constants';
import { initLogging } from './utils/error';
2016-06-13 17:33:47 +02:00
import { getCurrentSubdomain } from './utils/url';
import UrlResolver from './utils/url_resolver';
2015-07-17 15:41:09 +02:00
2016-06-03 17:52:38 +02:00
// FIXME: rename these event actions
import EventActions from './actions/event_actions';
2015-07-27 18:21:20 +02:00
// You can comment out the modules you don't need
// import DebugHandler from './third_party/debug_handler';
import './third_party/facebook_handler';
import './third_party/ga_handler';
import './third_party/intercom_handler';
import './third_party/notifications_handler';
import './third_party/raven_handler';
// Import global stylesheet
import '../sass/main.scss';
2015-05-20 11:23:50 +02:00
const AppGateway = {
start() {
2016-06-03 17:52:38 +02:00
let subdomainSettings;
try {
2016-06-13 17:33:47 +02:00
subdomainSettings = getSubdomainSettings(getCurrentSubdomain());
2016-06-03 17:52:38 +02:00
} catch (err) {
// if there are no matching subdomains, we''ll route to the default frontend
console.logGlobal(err);
2016-06-03 17:52:38 +02:00
subdomainSettings = getDefaultSubdomainSettings();
}
2016-06-03 17:52:38 +02:00
this.load(subdomainSettings);
},
load(settings) {
// Send the applicationWillBoot event to the third-party stores
EventActions.applicationWillBoot(settings);
2016-06-03 17:52:38 +02:00
// `history.listen` is called on every route change, which is perfect for routeDidChange
// events.
// For history <= 3.0, history.listen will synchronously invoke the callback once
// immediately after registration.
history.listen((location) => {
const { locationQueue } = history;
locationQueue.unshift(location);
// Limit the number of locations to keep in memory to avoid too much memory usage
if (locationQueue.length > AppConstants.locationThreshold) {
locationQueue.length = AppConstants.locationThreshold;
}
EventActions.routeDidChange(location);
});
2016-06-03 17:52:38 +02:00
// Adds a client specific class to the body for whitelabel styling
window.document.body.classList.add(`client--${settings.subdomain || 'ascribe'}`);
2016-06-03 17:52:38 +02:00
AppResolver
.resolve(settings)
.then(({ apiUrls, redirectRoute, routes }) => {
// Set url mapping for outgoing api requests
UrlResolver.setUrlMapping(apiUrls);
2016-06-03 17:52:38 +02:00
2016-01-25 13:33:20 +01:00
ReactDOM.render((
2016-06-03 17:52:38 +02:00
<Router history={history}>
{redirectRoute}
{routes}
</Router>
), document.getElementById('main'));
// Send the applicationDidBoot event to the third-party stores
EventActions.applicationDidBoot(settings);
});
}
};
// Initialize pre-start components
initLogging();
// And bootstrap app
AppGateway.start();