freeCodeCamp/client/index.js

81 lines
2.2 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-01-27 19:34:44 +00:00
import { routeReducer as routing, syncHistory } 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';
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;
2016-01-27 19:34:44 +00:00
const log = debug('fcc:client');
const hotReloadTimeout = 5000;
2016-05-09 17:13:02 +00:00
const csrfToken = window.__fcc__.csrf.token;
const DOMContainer = document.getElementById('fcc');
const initialState = isColdStored() ?
getColdStorage() :
window.__fcc__.data;
2016-05-09 17:13:02 +00:00
initialState.app.csrfToken = csrfToken;
2016-01-27 19:34:44 +00:00
2016-05-09 17:13:02 +00:00
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
2015-06-18 04:04:28 +00:00
2015-09-14 01:12:22 +00:00
const history = createHistory();
2016-01-27 19:34:44 +00:00
const routingMiddleware = syncHistory(history);
2016-01-27 19:34:44 +00:00
const devTools = window.devToolsExtension ? window.devToolsExtension() : f => f;
const shouldRouterListenForReplays = !!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
};
2016-01-04 03:40:49 +00:00
createApp({
history,
serviceOptions,
initialState,
2016-04-25 04:54:48 +00:00
middlewares: [ routingMiddleware ],
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 }) => {
2016-01-27 19:34:44 +00:00
if (shouldRouterListenForReplays && store) {
log('routing middleware listening for replays');
routingMiddleware.listenForReplays(store);
}
if (module.hot && typeof module.hot.accept === 'function') {
module.hot.accept('../common/app', function() {
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
))
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
);