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.

DialInSummary.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { translate } from '../../../../base/i18n';
  4. import ConferenceID from './ConferenceID';
  5. import NumbersList from './NumbersList';
  6. declare var config: Object;
  7. /**
  8. * The type of the React {@code Component} props of {@link DialInSummary}.
  9. */
  10. type Props = {
  11. /**
  12. * Additional CSS classnames to append to the root of the component.
  13. */
  14. className: string,
  15. /**
  16. * Whether or not numbers should include links with the telephone protocol.
  17. */
  18. clickableNumbers: boolean,
  19. /**
  20. * The name of the conference to show a conferenceID for.
  21. */
  22. room: string,
  23. /**
  24. * Invoked to obtain translated strings.
  25. */
  26. t: Function
  27. };
  28. /**
  29. * The type of the React {@code Component} state of {@link DialInSummary}.
  30. */
  31. type State = {
  32. /**
  33. * The numeric ID of the conference, used as a pin when dialing in.
  34. */
  35. conferenceID: ?string,
  36. /**
  37. * An error message to display.
  38. */
  39. error: string,
  40. /**
  41. * Whether or not the app is fetching data.
  42. */
  43. loading: boolean,
  44. /**
  45. * The dial-in numbers to be displayed.
  46. */
  47. numbers: ?Array<Object> | ?Object,
  48. /**
  49. * Whether or not dial-in is allowed.
  50. */
  51. numbersEnabled: ?boolean
  52. }
  53. /**
  54. * Displays a page listing numbers for dialing into a conference and pin to
  55. * the a specific conference.
  56. *
  57. * @extends Component
  58. */
  59. class DialInSummary extends Component<Props, State> {
  60. state = {
  61. conferenceID: null,
  62. error: '',
  63. loading: true,
  64. numbers: null,
  65. numbersEnabled: null
  66. };
  67. /**
  68. * Initializes a new {@code DialInSummary} instance.
  69. *
  70. * @param {Object} props - The read-only properties with which the new
  71. * instance is to be initialized.
  72. */
  73. constructor(props: Props) {
  74. super(props);
  75. // Bind event handlers so they are only bound once for every instance.
  76. this._onGetNumbersSuccess = this._onGetNumbersSuccess.bind(this);
  77. this._onGetConferenceIDSuccess
  78. = this._onGetConferenceIDSuccess.bind(this);
  79. this._setErrorMessage = this._setErrorMessage.bind(this);
  80. }
  81. /**
  82. * Implements {@link Component#componentDidMount()}. Invoked immediately
  83. * after this component is mounted.
  84. *
  85. * @inheritdoc
  86. * @returns {void}
  87. */
  88. componentDidMount() {
  89. const getNumbers = this._getNumbers()
  90. .then(this._onGetNumbersSuccess)
  91. .catch(this._setErrorMessage);
  92. const getID = this._getConferenceID()
  93. .then(this._onGetConferenceIDSuccess)
  94. .catch(this._setErrorMessage);
  95. Promise.all([ getNumbers, getID ])
  96. .then(() => {
  97. this.setState({ loading: false });
  98. });
  99. }
  100. /**
  101. * Implements React's {@link Component#render()}.
  102. *
  103. * @inheritdoc
  104. * @returns {ReactElement}
  105. */
  106. render() {
  107. let className = '';
  108. let contents;
  109. const { conferenceID, error, loading, numbersEnabled } = this.state;
  110. if (loading) {
  111. contents = '';
  112. } else if (numbersEnabled === false) {
  113. contents = this.props.t('info.dialInNotSupported');
  114. } else if (error) {
  115. contents = error;
  116. } else {
  117. className = 'has-numbers';
  118. contents = [
  119. conferenceID
  120. ? <ConferenceID
  121. conferenceID = { conferenceID }
  122. conferenceName = { this.props.room }
  123. key = 'conferenceID' />
  124. : null,
  125. <NumbersList
  126. clickableNumbers = { this.props.clickableNumbers }
  127. conferenceID = { conferenceID }
  128. key = 'numbers'
  129. numbers = { this.state.numbers } />
  130. ];
  131. }
  132. return (
  133. <div className = { `${this.props.className} ${className}` }>
  134. { contents }
  135. </div>
  136. );
  137. }
  138. /**
  139. * Creates an AJAX request for the conference ID.
  140. *
  141. * @private
  142. * @returns {Promise}
  143. */
  144. _getConferenceID() {
  145. const { room } = this.props;
  146. const { dialInConfCodeUrl, hosts } = config;
  147. const mucURL = hosts && hosts.muc;
  148. if (!dialInConfCodeUrl || !mucURL || !room) {
  149. return Promise.resolve();
  150. }
  151. const conferenceIDURL
  152. = `${dialInConfCodeUrl}?conference=${room}@${mucURL}`;
  153. return fetch(conferenceIDURL)
  154. .then(response => response.json())
  155. .catch(() => Promise.reject(this.props.t('info.genericError')));
  156. }
  157. /**
  158. * Creates an AJAX request for dial-in numbers.
  159. *
  160. * @private
  161. * @returns {Promise}
  162. */
  163. _getNumbers() {
  164. const { room } = this.props;
  165. const { dialInNumbersUrl, hosts } = config;
  166. const mucURL = hosts && hosts.muc;
  167. let URLSuffix = '';
  168. if (!dialInNumbersUrl) {
  169. return Promise.reject(this.props.t('info.dialInNotSupported'));
  170. }
  171. // when room and mucURL are available
  172. // provide conference when looking up dial in numbers
  173. if (room && mucURL) {
  174. URLSuffix = `?conference=${room}@${mucURL}`;
  175. }
  176. const conferenceIDURL
  177. = `${dialInNumbersUrl}${URLSuffix}`;
  178. return fetch(conferenceIDURL)
  179. .then(response => response.json())
  180. .catch(() => Promise.reject(this.props.t('info.genericError')));
  181. }
  182. _onGetConferenceIDSuccess: (Object) => void;
  183. /**
  184. * Callback invoked when fetching the conference ID succeeds.
  185. *
  186. * @param {Object} response - The response from fetching the conference ID.
  187. * @private
  188. * @returns {void}
  189. */
  190. _onGetConferenceIDSuccess(response = {}) {
  191. const { conference, id } = response;
  192. if (!conference || !id) {
  193. return;
  194. }
  195. this.setState({ conferenceID: id });
  196. }
  197. _onGetNumbersSuccess: (Object) => void;
  198. /**
  199. * Callback invoked when fetching dial-in numbers succeeds. Sets the
  200. * internal to show the numbers.
  201. *
  202. * @param {Array|Object} response - The response from fetching
  203. * dial-in numbers.
  204. * @param {Array|Object} response.numbers - The dial-in numbers.
  205. * @param {boolean} response.numbersEnabled - Whether or not dial-in is
  206. * enabled, old syntax that is deprecated.
  207. * @private
  208. * @returns {void}
  209. */
  210. _onGetNumbersSuccess(
  211. response: Array<Object> | { numbersEnabled?: boolean }) {
  212. this.setState({
  213. numbersEnabled:
  214. Array.isArray(response)
  215. ? response.length > 0 : response.numbersEnabled,
  216. numbers: response
  217. });
  218. }
  219. _setErrorMessage: (string) => void;
  220. /**
  221. * Sets an error message to display on the page instead of content.
  222. *
  223. * @param {string} error - The error message to display.
  224. * @private
  225. * @returns {void}
  226. */
  227. _setErrorMessage(error) {
  228. this.setState({
  229. error
  230. });
  231. }
  232. }
  233. export default translate(DialInSummary);