2018-01-05 13:47:53 -08:00
|
|
|
import _ from 'lodash';
|
2015-07-04 11:59:22 -07:00
|
|
|
import Rx from 'rx';
|
2016-01-27 11:34:44 -08:00
|
|
|
import debug from 'debug';
|
2016-05-04 16:46:19 -07:00
|
|
|
import { render } from 'redux-epic';
|
2017-11-09 17:10:30 -08:00
|
|
|
import createHistory from 'history/createBrowserHistory';
|
2016-07-21 16:35:37 -07:00
|
|
|
import useLangRoutes from './utils/use-lang-routes';
|
2016-09-01 18:46:09 -07:00
|
|
|
import sendPageAnalytics from './utils/send-page-analytics';
|
|
|
|
import flashToToast from './utils/flash-to-toast';
|
2015-06-17 21:04:28 -07:00
|
|
|
|
2017-11-09 17:10:30 -08:00
|
|
|
import { App, createApp, provideStore } from '../common/app';
|
2016-11-25 00:59:51 +00:00
|
|
|
import { getLangFromPath } from '../common/app/utils/lang';
|
2015-06-17 21:04:28 -07:00
|
|
|
|
2017-07-31 20:04:01 -07:00
|
|
|
// client specific epics
|
|
|
|
import epics from './epics';
|
2016-01-27 11:34:44 -08:00
|
|
|
|
2016-03-14 17:22:56 -07:00
|
|
|
import {
|
|
|
|
isColdStored,
|
|
|
|
getColdStorage,
|
|
|
|
saveToColdStorage
|
|
|
|
} from './cold-reload';
|
|
|
|
|
2016-06-29 13:00:22 -07:00
|
|
|
const isDev = Rx.config.longStackSupport = debug.enabled('fcc:*');
|
2016-01-27 11:34:44 -08:00
|
|
|
const log = debug('fcc:client');
|
2016-09-27 10:57:56 -07:00
|
|
|
const hotReloadTimeout = 2000;
|
2018-01-05 13:47:53 -08:00
|
|
|
const {
|
|
|
|
devToolsExtension = _.noop,
|
|
|
|
location,
|
|
|
|
history: _history,
|
|
|
|
document,
|
|
|
|
ga,
|
|
|
|
__fcc__: {
|
|
|
|
flash = [],
|
|
|
|
data: ssrState = {},
|
|
|
|
csrf: {
|
|
|
|
token: csrfToken
|
|
|
|
} = {}
|
|
|
|
}
|
|
|
|
} = window;
|
|
|
|
const epicOptions = {
|
|
|
|
isDev,
|
|
|
|
window,
|
|
|
|
document,
|
|
|
|
location,
|
|
|
|
history: _history
|
|
|
|
};
|
|
|
|
|
2016-03-20 21:43:36 -07:00
|
|
|
const DOMContainer = document.getElementById('fcc');
|
2017-11-09 17:10:30 -08:00
|
|
|
const defaultState = isColdStored() ?
|
2016-03-14 17:22:56 -07:00
|
|
|
getColdStorage() :
|
2018-01-05 13:47:53 -08:00
|
|
|
ssrState;
|
|
|
|
const primaryLang = getLangFromPath(location.pathname);
|
2016-11-25 00:59:51 +00:00
|
|
|
|
2017-11-09 17:10:30 -08:00
|
|
|
defaultState.app.csrfToken = csrfToken;
|
2018-01-05 13:47:53 -08:00
|
|
|
defaultState.toasts = flashToToast(flash);
|
2016-09-01 17:37:30 -07:00
|
|
|
|
2016-05-09 10:13:02 -07:00
|
|
|
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
|
2015-06-17 21:04:28 -07:00
|
|
|
|
2016-11-25 00:59:51 +00:00
|
|
|
const history = useLangRoutes(createHistory, primaryLang)();
|
2018-01-05 13:47:53 -08:00
|
|
|
sendPageAnalytics(history, ga);
|
2016-01-03 19:40:49 -08:00
|
|
|
|
2016-03-14 17:22:56 -07:00
|
|
|
createApp({
|
|
|
|
history,
|
|
|
|
serviceOptions,
|
2017-11-09 17:10:30 -08:00
|
|
|
defaultState,
|
2017-07-31 20:04:01 -07:00
|
|
|
epics,
|
|
|
|
epicOptions,
|
2018-01-05 13:47:53 -08:00
|
|
|
enhancers: [ devToolsExtension() ]
|
2016-03-14 17:22:56 -07:00
|
|
|
})
|
|
|
|
.doOnNext(({ store }) => {
|
|
|
|
if (module.hot && typeof module.hot.accept === 'function') {
|
2016-09-27 10:57:56 -07:00
|
|
|
module.hot.accept(err => {
|
|
|
|
if (err) { console.error(err); }
|
|
|
|
log('saving state and refreshing.');
|
|
|
|
log('ignore react ssr warning.');
|
2016-03-14 17:22:56 -07:00
|
|
|
saveToColdStorage(store.getState());
|
2018-01-05 13:47:53 -08:00
|
|
|
setTimeout(() => location.reload(), hotReloadTimeout);
|
2016-03-14 17:22:56 -07:00
|
|
|
});
|
|
|
|
}
|
2015-06-29 09:50:25 -07:00
|
|
|
})
|
2016-03-14 17:22:56 -07:00
|
|
|
.doOnNext(() => log('rendering'))
|
2017-11-09 17:10:30 -08:00
|
|
|
.flatMap(({ store }) => render(provideStore(App, store), DOMContainer))
|
2015-06-17 21:04:28 -07:00
|
|
|
.subscribe(
|
2016-01-27 11:34:44 -08:00
|
|
|
() => debug('react rendered'),
|
|
|
|
err => { throw err; },
|
|
|
|
() => debug('react closed subscription')
|
2015-06-17 21:04:28 -07:00
|
|
|
);
|