dispatch/client/src/js/modules/initialState.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

import Cookie from 'js-cookie';
import { socket as socketActions } from '../state/actions';
import { getWrapWidth, setEnvironment } from '../state/environment';
import { addMessages } from '../state/messages';
import { select, updateSelection } from '../state/tab';
import { find } from '../util';
import { when } from '../util/observe';
2017-05-19 05:29:44 +00:00
import { replace } from '../util/router';
export default function initialState({ store }) {
const env = JSON.parse(document.getElementById('env').innerHTML);
store.dispatch(setEnvironment('connect_defaults', env.defaults));
if (env.servers) {
store.dispatch({
type: socketActions.SERVERS,
2017-05-19 05:29:44 +00:00
data: env.servers
});
if (!store.getState().router.route) {
const tab = Cookie.get('tab');
if (tab) {
const [server, name = null] = tab.split(':');
if (find(env.servers, srv => srv.host === server)) {
store.dispatch(select(server, name, true));
} else {
store.dispatch(updateSelection());
}
} else {
store.dispatch(updateSelection());
}
}
2017-05-19 05:29:44 +00:00
} else {
store.dispatch(replace('/connect'));
}
if (env.channels) {
store.dispatch({
type: socketActions.CHANNELS,
2017-05-19 05:29:44 +00:00
data: env.channels
});
}
if (env.users) {
store.dispatch({
type: socketActions.USERS,
2017-05-19 05:29:44 +00:00
...env.users
});
}
// Wait until wrapWidth gets initialized so that height calculations
// only happen once for these messages
when(store, getWrapWidth, () => {
2017-05-19 05:29:44 +00:00
if (env.messages) {
const { messages, server, to, next } = env.messages;
store.dispatch(addMessages(messages, server, to, false, next));
}
});
}