freeCodeCamp/client/index.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-09-09 02:32:45 +00:00
import unused from './es6-shims'; // eslint-disable-line
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';
2015-09-14 01:12:22 +00:00
import { createLocation, createHistory } from 'history';
2015-07-21 21:06:21 +00:00
import { hydrate } from 'thundercats';
2015-12-23 03:28:07 +00:00
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';
2016-01-07 22:51:41 +00:00
import historySaga from './history-saga';
import errSaga from './err-saga';
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-08-12 18:46:34 +00:00
Rx.config.longStackSupport = !!debug.enabled;
2015-09-14 01:12:22 +00:00
const history = createHistory();
const appLocation = createLocation(
location.pathname + location.search
);
2015-06-18 04:04:28 +00:00
// returns an observable
2015-09-14 01:12:22 +00:00
app$({ history, location: appLocation })
.flatMap(
({ AppCat }) => {
2015-09-14 01:12:22 +00:00
// instantiate the cat with service
2015-12-30 01:35:50 +00:00
const appCat = AppCat(null, services, history);
2015-09-14 01:12:22 +00:00
// hydrate the stores
2015-12-30 01:35:50 +00:00
return hydrate(appCat, catState).map(() => appCat);
},
2015-09-14 01:12:22 +00:00
// not using nextLocation at the moment but will be used for
// redirects in the future
({ nextLocation, props }, appCat) => ({ nextLocation, props, appCat })
)
2015-10-28 03:07:28 +00:00
.doOnNext(({ appCat }) => {
2016-01-01 01:39:29 +00:00
const appStore$ = appCat.getStore('appStore');
2015-12-30 01:35:50 +00:00
2016-01-07 22:51:41 +00:00
const {
toast,
updateLocation,
goTo,
goBack
} = appCat.getActions('appActions');
2016-01-01 01:39:29 +00:00
const routerState$ = appStore$
.map(({ location }) => location)
.filter(location => !!location);
2015-10-28 03:07:28 +00:00
2016-01-04 03:40:49 +00:00
// set page title
appStore$
.pluck('title')
2016-01-07 22:51:41 +00:00
.distinctUntilChanged()
2016-01-04 03:40:49 +00:00
.doOnNext(title => document.title = title)
.subscribe(() => {});
2016-01-07 22:51:41 +00:00
historySaga(
2016-01-01 01:39:29 +00:00
history,
updateLocation,
goTo,
goBack,
routerState$
);
2016-01-07 22:51:41 +00:00
const err$ = appStore$
.pluck('err')
.filter(err => !!err)
.distinctUntilChanged();
errSaga(err$, toast);
2015-10-28 03:07:28 +00:00
})
2016-01-04 03:40:49 +00:00
// allow store subscribe to subscribe to actions
.delay(10)
2015-09-14 01:12:22 +00:00
.flatMap(({ props, appCat }) => {
props.history = history;
2016-01-04 03:40:49 +00:00
2015-12-23 03:28:07 +00:00
return render$(
appCat,
2015-09-14 01:12:22 +00:00
React.createElement(Router, props),
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 => {
throw err;
2015-07-04 18:59:22 +00:00
},
() => {
debug('react closed subscription');
2015-06-18 04:04:28 +00:00
}
);