freeCodeCamp/client/index.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-07-04 18:59:22 +00:00
import Rx from 'rx';
import React from 'react';
import Fetchr from 'fetchr';
import debugFactory from 'debug';
2015-07-04 18:59:22 +00:00
import { Router } from 'react-router';
import { history } from 'react-router/lib/BrowserHistory';
2015-07-21 21:06:21 +00:00
import { hydrate } from 'thundercats';
import { Render } from 'thundercats-react';
2015-06-18 04:04:28 +00:00
2015-07-01 22:14:10 +00:00
import { app$ } from '../common/app';
2015-06-18 04:04:28 +00:00
const debug = debugFactory('fcc:client');
2015-07-04 18:59:22 +00:00
const DOMContianer = document.getElementById('fcc');
const catState = window.__fcc__.data || {};
const services = new Fetchr({
xhrPath: '/services'
});
2015-06-18 04:04:28 +00:00
2015-07-04 18:59:22 +00:00
Rx.longStackSupport = !!debug.enabled;
2015-06-18 04:04:28 +00:00
// returns an observable
2015-07-04 18:59:22 +00:00
app$(history)
.flatMap(
({ AppCat }) => {
const appCat = AppCat(null, services);
2015-07-21 21:06:21 +00:00
return hydrate(appCat, catState)
.map(() => appCat);
},
({ initialState }, appCat) => ({ initialState, appCat })
)
.flatMap(({ initialState, appCat }) => {
return Render(
appCat,
React.createElement(Router, initialState),
DOMContianer
);
2015-06-29 16:50:25 +00:00
})
2015-06-18 04:04:28 +00:00
.subscribe(
2015-07-04 18:59:22 +00:00
() => {
2015-06-18 04:04:28 +00:00
debug('react rendered');
},
2015-07-04 18:59:22 +00:00
err => {
2015-06-18 04:04:28 +00:00
debug('an error has occured', err.stack);
2015-07-04 18:59:22 +00:00
},
() => {
debug('react closed subscription');
2015-06-18 04:04:28 +00:00
}
);