freeCodeCamp/client/index.js

95 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-01-27 19:34:44 +00:00
import './es6-shims';
2015-07-04 18:59:22 +00:00
import Rx from 'rx';
import React from 'react';
2016-01-27 19:34:44 +00:00
import debug from 'debug';
2015-07-04 18:59:22 +00:00
import { Router } from 'react-router';
2016-06-14 16:31:41 +00:00
import {
routerMiddleware,
routerReducer as routing,
syncHistoryWithStore
} from 'react-router-redux';
2016-05-04 23:46:19 +00:00
import { render } from 'redux-epic';
2016-03-03 03:45:54 +00:00
import { createHistory } from 'history';
import useLangRoutes from './utils/use-lang-routes';
import sendPageAnalytics from './utils/send-page-analytics';
import flashToToast from './utils/flash-to-toast';
2015-06-18 04:04:28 +00:00
import createApp from '../common/app';
2016-01-27 19:34:44 +00:00
import provideStore from '../common/app/provide-store';
2015-06-18 04:04:28 +00:00
2016-01-27 19:34:44 +00:00
// client specific sagas
import sagas from './sagas';
import {
isColdStored,
getColdStorage,
saveToColdStorage
} from './cold-reload';
const isDev = Rx.config.longStackSupport = debug.enabled('fcc:*');
2016-01-27 19:34:44 +00:00
const log = debug('fcc:client');
const hotReloadTimeout = 2000;
const { csrf: { csrfToken } = {} } = window.__fcc__;
const DOMContainer = document.getElementById('fcc');
const initialState = isColdStored() ?
getColdStorage() :
window.__fcc__.data;
2016-05-09 17:13:02 +00:00
initialState.app.csrfToken = csrfToken;
initialState.toasts = flashToToast(window.__fcc__.flash);
// make empty object so hot reload works
window.__fcc__ = {};
2016-05-09 17:13:02 +00:00
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
2015-06-18 04:04:28 +00:00
const history = useLangRoutes(createHistory)();
sendPageAnalytics(history, window.ga);
2016-01-27 19:34:44 +00:00
const devTools = window.devToolsExtension ? window.devToolsExtension() : f => f;
2016-06-14 16:31:41 +00:00
const adjustUrlOnReplay = !!window.devToolsExtension;
2015-10-28 03:07:28 +00:00
2016-04-25 04:54:48 +00:00
const sagaOptions = {
isDev,
2016-04-25 04:54:48 +00:00
window,
document: window.document,
location: window.location,
history: window.history
2016-04-25 04:54:48 +00:00
};
2016-01-04 03:40:49 +00:00
createApp({
history,
2016-06-14 16:31:41 +00:00
syncHistoryWithStore,
syncOptions: { adjustUrlOnReplay },
serviceOptions,
initialState,
2016-06-14 16:31:41 +00:00
middlewares: [ routerMiddleware(history) ],
2016-05-20 19:42:26 +00:00
sagas: [...sagas ],
2016-04-25 04:54:48 +00:00
sagaOptions,
reducers: { routing },
enhancers: [ devTools ]
})
.doOnNext(({ store }) => {
if (module.hot && typeof module.hot.accept === 'function') {
module.hot.accept(err => {
if (err) { console.error(err); }
log('saving state and refreshing.');
log('ignore react ssr warning.');
saveToColdStorage(store.getState());
setTimeout(() => window.location.reload(), hotReloadTimeout);
});
}
2015-06-29 16:50:25 +00:00
})
.doOnNext(() => log('rendering'))
.flatMap(
({ props, store }) => render(
provideStore(React.createElement(Router, props), store),
DOMContainer
),
({ store }) => store
)
2015-06-18 04:04:28 +00:00
.subscribe(
2016-01-27 19:34:44 +00:00
() => debug('react rendered'),
err => { throw err; },
() => debug('react closed subscription')
2015-06-18 04:04:28 +00:00
);