You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { setRoom } from '../base/conference';
  2. import {
  3. getDomain,
  4. setDomain
  5. } from '../base/connection';
  6. import {
  7. loadConfig,
  8. setConfig
  9. } from '../base/lib-jitsi-meet';
  10. import {
  11. APP_WILL_MOUNT,
  12. APP_WILL_UNMOUNT
  13. } from './actionTypes';
  14. import {
  15. _getRoomAndDomainFromUrlString,
  16. _getRouteToRender,
  17. init
  18. } from './functions';
  19. import './reducer';
  20. /**
  21. * Temporary solution. Should dispatch actions related to
  22. * initial settings of the app like setting log levels,
  23. * reading the config parameters from query string etc.
  24. *
  25. * @returns {Function}
  26. */
  27. export function appInit() {
  28. return () => init();
  29. }
  30. /**
  31. * Triggers an in-app navigation to a different route. Allows navigation to be
  32. * abstracted between the mobile and web versions.
  33. *
  34. * @param {(string|undefined)} urlOrRoom - The URL or room name to which to
  35. * navigate.
  36. * @returns {Function}
  37. */
  38. export function appNavigate(urlOrRoom) {
  39. return (dispatch, getState) => {
  40. const oldDomain = getDomain(getState());
  41. const { domain, room } = _getRoomAndDomainFromUrlString(urlOrRoom);
  42. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  43. // currently in a conference and ask her if she wants to close the
  44. // current conference and start a new one with the new room name or
  45. // domain.
  46. if (typeof domain === 'undefined' || oldDomain === domain) {
  47. // If both domain and room vars became undefined, that means we're
  48. // actually dealing with just room name and not with URL.
  49. dispatch(
  50. _setRoomAndNavigate(
  51. typeof room === 'undefined' && typeof domain === 'undefined'
  52. ? urlOrRoom
  53. : room));
  54. } else if (oldDomain !== domain) {
  55. // Update domain without waiting for config to be loaded to prevent
  56. // race conditions when we will start to load config multiple times.
  57. dispatch(setDomain(domain));
  58. // If domain has changed, we need to load the config of the new
  59. // domain and set it, and only after that we can navigate to
  60. // different route.
  61. loadConfig(`https://${domain}`)
  62. .then(
  63. config => configLoaded(/* err */ undefined, config),
  64. err => configLoaded(err, /* config */ undefined));
  65. }
  66. /**
  67. * Notifies that an attempt to load the config(uration) of domain has
  68. * completed.
  69. *
  70. * @param {string|undefined} err - If the loading has failed, the error
  71. * detailing the cause of the failure.
  72. * @param {Object|undefined} config - If the loading has succeeded, the
  73. * loaded config(uration).
  74. * @returns {void}
  75. */
  76. function configLoaded(err, config) {
  77. if (err) {
  78. // XXX The failure could be, for example, because of a
  79. // certificate-related error. In which case the connection will
  80. // fail later in Strophe anyway even if we use the default
  81. // config here.
  82. // The function loadConfig will log the err.
  83. return;
  84. }
  85. // We set room name only here to prevent race conditions on app
  86. // start to not make app re-render conference page for two times.
  87. dispatch(setRoom(room));
  88. dispatch(setConfig(config));
  89. _navigate(getState());
  90. }
  91. };
  92. }
  93. /**
  94. * Signals that a specific App will mount (in the terms of React).
  95. *
  96. * @param {App} app - The App which will mount.
  97. * @returns {{
  98. * type: APP_WILL_MOUNT,
  99. * app: App
  100. * }}
  101. */
  102. export function appWillMount(app) {
  103. return {
  104. type: APP_WILL_MOUNT,
  105. app
  106. };
  107. }
  108. /**
  109. * Signals that a specific App will unmount (in the terms of React).
  110. *
  111. * @param {App} app - The App which will unmount.
  112. * @returns {{
  113. * type: APP_WILL_UNMOUNT,
  114. * app: App
  115. * }}
  116. */
  117. export function appWillUnmount(app) {
  118. return {
  119. type: APP_WILL_UNMOUNT,
  120. app
  121. };
  122. }
  123. /**
  124. * Navigates to route corresponding to current room name.
  125. *
  126. * @param {Object} state - Redux state.
  127. * @private
  128. * @returns {void}
  129. */
  130. function _navigate(state) {
  131. const app = state['features/app'].app;
  132. const routeToRender = _getRouteToRender(state);
  133. app._navigate(routeToRender);
  134. }
  135. /**
  136. * Sets room and navigates to new route if needed.
  137. *
  138. * @param {string} newRoom - New room name.
  139. * @private
  140. * @returns {Function}
  141. */
  142. function _setRoomAndNavigate(newRoom) {
  143. return (dispatch, getState) => {
  144. const oldRoom = getState()['features/base/conference'].room;
  145. dispatch(setRoom(newRoom));
  146. const state = getState();
  147. const room = state['features/base/conference'].room;
  148. if (room !== oldRoom) {
  149. _navigate(state);
  150. }
  151. };
  152. }