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.native.js 4.6KB

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