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

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