forked from higlass/higlass-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (80 loc) · 2.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import createPubSub from 'pub-sub-es';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import * as serviceWorker from './service-worker';
// HOCs
import { Provider as PubSubProvider } from './hocs/with-pub-sub';
// Components
import App from './components/App';
import AppFake from './components/AppFake';
// Services
import auth from './services/auth';
// Factories
import { createState } from './factories/state';
// Utils
import Logger from './utils/logger';
import pathify from './utils/pathify';
// Styles
import './index.scss';
const logger = Logger('Index');
// Initialize store
const state = createState();
let rehydratedStore;
const storeRehydrated = state.configure();
// Init pub-sub service
const pubSub = createPubSub();
const basename = pathify(
typeof window.HGAC_BASEPATH === 'string'
? window.HGAC_BASEPATH // from compiled `config.js`
: HGAC_BASEPATH // from webpack's DefinePlugin
);
const render = (Component, store, error) => {
if (!store) {
ReactDOM.render(
<PubSubProvider value={pubSub}>
<AppFake error={error} />
</PubSubProvider>,
document.getElementById('root')
);
} else {
ReactDOM.render(
<Provider store={store}>
<BrowserRouter basename={basename}>
<PubSubProvider value={pubSub}>
<Component />
</PubSubProvider>
</BrowserRouter>
</Provider>,
document.getElementById('root')
);
}
};
render(AppFake);
auth
.checkAuthentication()
.then(() => storeRehydrated)
.then(store => {
rehydratedStore = store;
render(App, store);
})
.catch(error => {
logger.error('Failed to rehydrate the store! This is fatal!', error);
render(
undefined,
undefined,
'Failed to initialize! This is bad, please contact an admin.'
);
});
if (module.hot) {
module.hot.accept('./components/App', () => {
const NextApp = require('./components/App').default; // eslint-disable-line global-require
render(NextApp, rehydratedStore);
});
storeRehydrated.then(store => {
window.store = store;
});
}
// Chnage to `serviceWorker.register();` to use the service worker
serviceWorker.unregisterAll();