dispatch/client/src/js/index.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-12-28 23:34:32 +00:00
import React from 'react';
import { render } from 'react-dom';
import { AppContainer } from 'react-hot-loader';
2017-02-16 02:55:50 +00:00
import 'react-virtualized/styles.css';
2015-12-28 23:34:32 +00:00
import configureStore from './store';
import initRouter, { replace } from './util/router';
import routes from './routes';
2015-12-28 23:34:32 +00:00
import Socket from './util/Socket';
import handleSocket from './socket';
import Root from './containers/Root';
2017-05-15 04:51:24 +00:00
import { setEnvironment } from './actions/environment';
import { addMessages } from './actions/message';
import { initWidthUpdates } from './util/messageHeight';
2015-12-28 23:34:32 +00:00
2017-02-16 02:55:50 +00:00
const host = DEV ? `${window.location.hostname}:1337` : window.location.host;
const socket = new Socket(host);
2015-12-28 23:34:32 +00:00
const store = configureStore(socket);
initRouter(routes, store);
handleSocket(socket, store);
2016-02-03 18:42:07 +00:00
const env = JSON.parse(document.getElementById('env').innerHTML);
2017-05-15 04:51:24 +00:00
store.dispatch(setEnvironment('connect_defaults', env.defaults));
2016-02-03 18:42:07 +00:00
if (env.servers) {
store.dispatch({
type: 'SOCKET_SERVERS',
2016-02-03 18:42:07 +00:00
data: env.servers
});
} else {
2016-03-14 22:22:24 +00:00
store.dispatch(replace('/connect'));
}
2016-02-03 18:42:07 +00:00
if (env.channels) {
store.dispatch({
type: 'SOCKET_CHANNELS',
2016-02-03 18:42:07 +00:00
data: env.channels
});
}
2016-02-03 18:42:07 +00:00
if (env.users) {
store.dispatch({
type: 'SOCKET_USERS',
2016-02-03 18:42:07 +00:00
...env.users
});
}
2017-05-12 08:51:37 +00:00
initWidthUpdates(store, () => {
if (env.messages) {
const { messages, server, to, next } = env.messages;
store.dispatch(addMessages(messages, server, to, false, next));
}
});
const renderRoot = () => {
render(
<AppContainer>
<Root store={store} />
</AppContainer>,
document.getElementById('root')
);
};
renderRoot();
if (module.hot) {
module.hot.accept('./containers/Root', () => renderRoot());
}