Home Reference Source

application/main.js

  1. // require('babel-polyfill');
  2. import React from 'react';
  3. import ReactDom from 'react-dom';
  4. import Immutable from 'immutable';
  5.  
  6. import {Router, Route, Link, browserHistory} from "react-router";
  7. import AutobahnReact from 'autobahn-react';
  8.  
  9. import App from './components/app';
  10.  
  11. import {buildRoutes} from './routes';
  12. import alt from './alt';
  13.  
  14. import {createStore, applyMiddleware} from 'redux';
  15. import {Provider} from 'react-redux';
  16.  
  17. import appState from './state';
  18. import thunkMiddleware from 'redux-thunk';
  19.  
  20. import Config from './config';
  21. import CurrentUser from './current-user';
  22.  
  23. import {
  24. contentReceiveWebSocket,
  25. contentDeleteFromWebSocket
  26. } from './redux/actions/content-actions';
  27.  
  28. import {
  29. assignmentsReceiveWebSocket
  30. } from './redux/actions/assignment-actions';
  31.  
  32. import {
  33. assignmentCommentsReceiveWebSocket
  34. } from './redux/actions/assignment-comment-actions';
  35.  
  36. import {
  37. snackbarShowMessage,
  38. snackbarClose
  39. } from './redux/actions/snackbar-actions';
  40.  
  41. import {
  42. applicationSetVersion,
  43. applicationNotifyUpdates,
  44. applicationIsConnected,
  45. applicationFetchSettings
  46. } from './redux/actions/application-actions';
  47.  
  48.  
  49. let store = createStore(appState, applyMiddleware(thunkMiddleware));
  50.  
  51. window.React = React;
  52. window.Store = store;
  53.  
  54. window._ceoScrollTop = function() {
  55. window.scrollTo(0,0);
  56. }
  57.  
  58. // start up autobhan
  59. let abStart = false;
  60. store.subscribe(() => {
  61. if (abStart === false) {
  62. abStart = true;
  63.  
  64. // build the URL
  65. const url = Config.get('_abep');
  66. const clientCode = Config.get('client_code');
  67.  
  68. // SRN is the username, token is the password
  69. const srn = CurrentUser.getSrn();
  70. const token = Config.get('_abtok');
  71.  
  72. let warnTimeout = null;
  73.  
  74. // Initialize the connection, then set up the subs and start the auth process
  75. AutobahnReact.initialize(url + '/' + token, 'realm');
  76.  
  77. // Library remembers the on ready state, so this will automatically
  78. // reconnect on a dropped connection
  79. AutobahnReact.Connection.onReady((details) => {
  80. store.dispatch(snackbarClose());
  81. store.dispatch(applicationIsConnected(true));
  82. store.dispatch(applicationFetchSettings());
  83. if (warnTimeout) {
  84. clearTimeout(warnTimeout);
  85. warnTimeout = null;
  86. }
  87.  
  88. // just for testing
  89. AutobahnReact.subscribe('com.getsnworks.ceo.test', function(payload) {
  90. console.log(payload);
  91. });
  92.  
  93. // broadcast channel allows us to broadcast a message to all users
  94. AutobahnReact.subscribe('com.getsnworks.ceo.broadcast', function(payload) {
  95. console.log(payload);
  96. });
  97.  
  98. // client specific broadcast and events
  99. AutobahnReact.subscribe('com.getsnworks.ceo.' + clientCode + '.broadcast', function(payload) {
  100. console.log(payload);
  101. });
  102. AutobahnReact.subscribe('com.getsnworks.ceo.' + clientCode + '.contentupdate', function(payload) {
  103. store.dispatch(contentReceiveWebSocket(Immutable.fromJS(payload[0].content)));
  104. });
  105. AutobahnReact.subscribe('com.getsnworks.ceo.' + clientCode + '.contentupdate.deleted', function(payload) {
  106. store.dispatch(contentDeleteFromWebSocket(Immutable.fromJS(payload[0].content)));
  107. });
  108. AutobahnReact.subscribe('com.getsnworks.ceo.' + clientCode + '.assignmentupdate', function(payload) {
  109. store.dispatch(assignmentsReceiveWebSocket(Immutable.fromJS(payload[0].content)));
  110. });
  111. AutobahnReact.subscribe('com.getsnworks.ceo.' + clientCode + '.assignmentcommentupdate', function(payload) {
  112. store.dispatch(assignmentCommentsReceiveWebSocket(Immutable.fromJS(payload[0].content)));
  113. });
  114. });
  115. AutobahnReact.Connection.onLost((details) => {
  116. warnTimeout = setTimeout(() => {
  117. store.dispatch(snackbarShowMessage('Trying to reconnect...', 0));
  118. }, 2000);
  119. store.dispatch(applicationIsConnected(false));
  120. });
  121.  
  122. // start the WampCra login process
  123. AutobahnReact.Auth.logIn({username: srn, password: token})
  124. .then(() => {
  125. console.log('Connected');
  126. store.dispatch(applicationIsConnected(true));
  127. })
  128. .catch(() => {
  129. store.dispatch(snackbarShowMessage('Unable to connect to server.', 0));
  130. console.log('Unable to connect to server.', arguments);
  131. store.dispatch(applicationIsConnected(false));
  132. });
  133.  
  134. // check the version and notify
  135. const current = localStorage.getItem('_ceover');
  136. if (!current || current != Config.get('system_version')) {
  137. localStorage.setItem('_ceover', Config.get('system_version'));
  138. store.dispatch(applicationNotifyUpdates());
  139. }
  140.  
  141. // check rebound for failed messages
  142. setTimeout(() => {
  143. if (window.Rebound && CurrentUser.getEmail().length) {
  144. window.Rebound.check({
  145. publicToken: "5qtz5s5smnfdo2wdunocmp",
  146. email: CurrentUser.getEmail(),
  147. appearance: "alert",
  148. position: "topRight",
  149. title: "Please verify your email"
  150. });
  151. }
  152. }, 2000);
  153.  
  154. // finally setup the heartbeat
  155. setInterval(() => {
  156. console.info('Dub');
  157. fetch('/sys/ping', {
  158. 'credentials': 'same-origin',
  159. })
  160. .then((r) => {
  161. if (r.status !== 200) {
  162. store.dispatch(snackbarShowMessage('Unable to connect to server.', 0));
  163. store.dispatch(applicationIsConnected(false));
  164. return;
  165. }
  166. console.info('Lub');
  167. })
  168. .catch(() => {
  169. store.dispatch(snackbarShowMessage('Unable to communicate with server.', 0));
  170. store.dispatch(applicationIsConnected(false));
  171. });
  172.  
  173. }, 600000); // every 10 minutes
  174. }
  175. });
  176.  
  177. /**
  178. * Main entry point for the app. Load the router and inject the routes.
  179. * @type {Object}
  180. */
  181. ReactDom.render((
  182. <Provider store={store}>
  183. <Router history={browserHistory}>
  184. {buildRoutes(store)}
  185. </Router>
  186. </Provider>
  187. ), document.querySelector("#app-container"));