freeCodeCamp/client/index.js

74 lines
2.1 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-03-03 03:45:54 +00:00
import { createHistory } from 'history';
2015-06-18 04:04:28 +00:00
2016-01-06 17:33:55 +00:00
import app$ 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';
// render to observable
import render from '../common/app/utils/render';
const log = debug('fcc:client');
2016-03-11 18:25:59 +00:00
const DOMContainer = document.getElementById('fcc');
2016-01-27 19:34:44 +00:00
const initialState = window.__fcc__.data;
2016-05-09 17:13:02 +00:00
const csrfToken = window.__fcc__.csrf.token;
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-08-12 18:46:34 +00:00
Rx.config.longStackSupport = !!debug.enabled;
2015-09-14 01:12:22 +00:00
const history = createHistory();
2016-03-03 03:45:54 +00:00
const appLocation = history.createLocation(
2015-09-14 01:12:22 +00:00
location.pathname + location.search
);
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-01-27 19:34:44 +00:00
const clientSagaOptions = { doc: document };
2016-01-04 03:40:49 +00:00
2016-01-27 19:34:44 +00:00
// returns an observable
app$({
location: appLocation,
history,
serviceOptions,
initialState,
middlewares: [
routingMiddleware,
...sagas.map(saga => saga(clientSagaOptions))
],
reducers: { routing },
enhancers: [ devTools ]
})
.flatMap(({ props, store }) => {
2016-01-07 22:51:41 +00:00
2016-01-27 19:34:44 +00:00
// because of weirdness in react-routers match function
// we replace the wrapped returned in props with the first one
// we passed in. This might be fixed in react-router 2.0
props.history = history;
2016-01-04 03:40:49 +00:00
2016-01-27 19:34:44 +00:00
if (shouldRouterListenForReplays && store) {
log('routing middleware listening for replays');
routingMiddleware.listenForReplays(store);
}
log('rendering');
return render(
provideStore(React.createElement(Router, props), store),
2016-03-11 18:25:59 +00:00
DOMContainer
);
2015-06-29 16:50:25 +00:00
})
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
);