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.

DialInNumbersForm.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import { StatelessDropdownMenu } from '@atlaskit/dropdown-menu';
  2. import ExpandIcon from '@atlaskit/icon/glyph/expand';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../base/i18n';
  6. import { getLocalParticipant } from '../../base/participants';
  7. import { updateDialInNumbers } from '../actions';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * React {@code Component} responsible for fetching and displaying telephone
  11. * numbers for dialing into a conference. Also supports copying a selected
  12. * dial-in number to the clipboard.
  13. *
  14. * @extends Component
  15. */
  16. class DialInNumbersForm extends Component {
  17. /**
  18. * {@code DialInNumbersForm}'s property types.
  19. *
  20. * @static
  21. */
  22. static propTypes = {
  23. /**
  24. * The redux state representing the dial-in numbers feature.
  25. */
  26. _dialIn: React.PropTypes.object,
  27. /**
  28. * The display name of the local user.
  29. */
  30. _localUserDisplayName: React.PropTypes.string,
  31. /**
  32. * Invoked to send an ajax request for dial-in numbers.
  33. */
  34. dispatch: React.PropTypes.func,
  35. /**
  36. * The URL of the conference into which this {@code DialInNumbersForm}
  37. * is inviting the local participant.
  38. */
  39. inviteURL: React.PropTypes.string,
  40. /**
  41. * Invoked to obtain translated strings.
  42. */
  43. t: React.PropTypes.func
  44. };
  45. /**
  46. * Initializes a new {@code DialInNumbersForm} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props) {
  52. super(props);
  53. this.state = {
  54. /**
  55. * Whether or not the dropdown should be open.
  56. *
  57. * @type {boolean}
  58. */
  59. isDropdownOpen: false,
  60. /**
  61. * The dial-in number to display as currently selected in the
  62. * dropdown. The value should be an object which has two key/value
  63. * pairs, content and number. The value of "content" will display in
  64. * the dropdown while the value of "number" is a substring of
  65. * "content" which will be copied to clipboard.
  66. *
  67. * @type {object}
  68. */
  69. selectedNumber: null
  70. };
  71. /**
  72. * The internal reference to the DOM/HTML element backing the React
  73. * {@code Component} text area. It is necessary for the implementation
  74. * of copying to the clipboard.
  75. *
  76. * @private
  77. * @type {HTMLTextAreaElement}
  78. */
  79. this._copyElement = null;
  80. // Bind event handlers so they are only bound once for every instance.
  81. this._onCopyClick = this._onCopyClick.bind(this);
  82. this._onOpenChange = this._onOpenChange.bind(this);
  83. this._onSelect = this._onSelect.bind(this);
  84. this._setCopyElement = this._setCopyElement.bind(this);
  85. }
  86. /**
  87. * Sets a default number to display in the dropdown trigger.
  88. *
  89. * @inheritdoc
  90. * returns {void}
  91. */
  92. componentWillMount() {
  93. const { numbers } = this.props._dialIn;
  94. if (numbers) {
  95. this._setDefaultNumber(numbers);
  96. } else {
  97. this.props.dispatch(updateDialInNumbers());
  98. }
  99. }
  100. /**
  101. * Monitors for number updates and sets a default number to display in the
  102. * dropdown trigger if not already set.
  103. *
  104. * @inheritdoc
  105. * returns {void}
  106. */
  107. componentWillReceiveProps(nextProps) {
  108. if (!this.state.selectedNumber && nextProps._dialIn.numbers) {
  109. this._setDefaultNumber(nextProps._dialIn.numbers);
  110. }
  111. }
  112. /**
  113. * Implements React's {@link Component#render()}. Returns null if the
  114. * component is not ready for display.
  115. *
  116. * @inheritdoc
  117. * @returns {ReactElement|null}
  118. */
  119. render() {
  120. const { _dialIn, t } = this.props;
  121. const { conferenceID, numbers, numbersEnabled } = _dialIn;
  122. const { selectedNumber } = this.state;
  123. if (!conferenceID || !numbers || !numbersEnabled || !selectedNumber) {
  124. return null;
  125. }
  126. const items = this._formatNumbers(numbers);
  127. return (
  128. <div className = 'form-control dial-in-numbers'>
  129. <label className = 'form-control__label'>
  130. { t('invite.howToDialIn') }
  131. <span className = 'dial-in-numbers-conference-id'>
  132. { conferenceID }
  133. </span>
  134. </label>
  135. <div className = 'form-control__container'>
  136. { this._createDropdownMenu(items, selectedNumber.content) }
  137. <button
  138. className = 'button-control button-control_light'
  139. onClick = { this._onCopyClick }
  140. type = 'button'>
  141. Copy
  142. </button>
  143. </div>
  144. <textarea
  145. className = 'dial-in-numbers-copy'
  146. readOnly = { true }
  147. ref = { this._setCopyElement }
  148. tabIndex = '-1'
  149. value = { this._generateCopyText() } />
  150. </div>
  151. );
  152. }
  153. /**
  154. * Creates a {@code StatelessDropdownMenu} instance.
  155. *
  156. * @param {Array} items - The content to display within the dropdown.
  157. * @param {string} triggerText - The text to display within the
  158. * trigger element.
  159. * @returns {ReactElement}
  160. */
  161. _createDropdownMenu(items, triggerText) {
  162. return (
  163. <StatelessDropdownMenu
  164. isOpen = { this.state.isDropdownOpen }
  165. items = { [ { items } ] }
  166. onItemActivated = { this._onSelect }
  167. onOpenChange = { this._onOpenChange }
  168. shouldFitContainer = { true }>
  169. { this._createDropdownTrigger(triggerText) }
  170. </StatelessDropdownMenu>
  171. );
  172. }
  173. /**
  174. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  175. * trigger for displaying the dropdown menu. The {@code Component} will also
  176. * display the currently selected number.
  177. *
  178. * @param {string} triggerText - Text to display in the HTMLInputElement.
  179. * @private
  180. * @returns {ReactElement}
  181. */
  182. _createDropdownTrigger(triggerText) {
  183. return (
  184. <div className = 'dial-in-numbers-trigger'>
  185. <input
  186. className = 'input-control'
  187. readOnly = { true }
  188. type = 'text'
  189. value = { triggerText || '' } />
  190. <span className = 'dial-in-numbers-trigger-icon'>
  191. <ExpandIcon label = 'expand' />
  192. </span>
  193. </div>
  194. );
  195. }
  196. /**
  197. * Detects whether the response from dialInNumbersUrl returned an array or
  198. * an object with dial-in numbers and calls the appropriate method to
  199. * transform the numbers into the format expected by
  200. * {@code StatelessDropdownMenu}.
  201. *
  202. * @param {Array<string>|Object} dialInNumbers - The numbers returned from
  203. * requesting dialInNumbersUrl.
  204. * @private
  205. * @returns {Array<Object>}
  206. */
  207. _formatNumbers(dialInNumbers) {
  208. if (Array.isArray(dialInNumbers)) {
  209. return this._formatNumbersArray(dialInNumbers);
  210. }
  211. return this._formatNumbersObject(dialInNumbers);
  212. }
  213. /**
  214. * Transforms the passed in numbers array into an array of objects that can
  215. * be parsed by {@code StatelessDropdownMenu}.
  216. *
  217. * @param {Array<string>} dialInNumbers - An array with dial-in numbers to
  218. * display and copy.
  219. * @private
  220. * @returns {Array<Object>}
  221. */
  222. _formatNumbersArray(dialInNumbers) {
  223. return dialInNumbers.map(number => {
  224. return {
  225. content: number,
  226. number
  227. };
  228. });
  229. }
  230. /**
  231. * Transforms the passed in numbers object into an array of objects that can
  232. * be parsed by {@code StatelessDropdownMenu}.
  233. *
  234. * @param {Object} dialInNumbers - The numbers object to parse. The
  235. * expected format is an object with keys being the name of the country
  236. * and the values being an array of numbers as strings.
  237. * @private
  238. * @returns {Array<Object>}
  239. */
  240. _formatNumbersObject(dialInNumbers) {
  241. const phoneRegions = Object.keys(dialInNumbers);
  242. if (!phoneRegions.length) {
  243. return [];
  244. }
  245. const formattedNumbers = phoneRegions.map(region => {
  246. const numbers = dialInNumbers[region];
  247. return numbers.map(number => {
  248. return {
  249. content: `${region}: ${number}`,
  250. number
  251. };
  252. });
  253. });
  254. return Array.prototype.concat(...formattedNumbers);
  255. }
  256. /**
  257. * Creates a message describing how to dial in to the conference.
  258. *
  259. * @private
  260. * @returns {string}
  261. */
  262. _generateCopyText() {
  263. const { t } = this.props;
  264. const welcome = t('invite.invitedYouTo', {
  265. inviteURL: this.props.inviteURL,
  266. userName: this.props._localUserDisplayName
  267. });
  268. const callNumber = t('invite.callNumber', {
  269. number: this.state.selectedNumber.number
  270. });
  271. const stepOne = `1) ${callNumber}`;
  272. const enterID = t('invite.enterID', {
  273. conferenceID: this.props._dialIn.conferenceID
  274. });
  275. const stepTwo = `2) ${enterID}`;
  276. return `${welcome}\n${stepOne}\n${stepTwo}`;
  277. }
  278. /**
  279. * Copies part of the number displayed in the dropdown trigger into the
  280. * clipboard. Only the value specified in selectedNumber.number, which
  281. * should be a substring of the displayed value, will be copied.
  282. *
  283. * @private
  284. * @returns {void}
  285. */
  286. _onCopyClick() {
  287. try {
  288. this._copyElement.select();
  289. document.execCommand('copy');
  290. this._copyElement.blur();
  291. } catch (err) {
  292. logger.error('error when copying the text', err);
  293. }
  294. }
  295. /**
  296. * Sets the internal state to either open or close the dropdown. If the
  297. * dropdown is disabled, the state will always be set to false.
  298. *
  299. * @param {Object} dropdownEvent - The even returned from clicking on the
  300. * dropdown trigger.
  301. * @private
  302. * @returns {void}
  303. */
  304. _onOpenChange(dropdownEvent) {
  305. this.setState({
  306. isDropdownOpen: dropdownEvent.isOpen
  307. });
  308. }
  309. /**
  310. * Updates the internal state of the currently selected number.
  311. *
  312. * @param {Object} selection - Event from choosing an dropdown option.
  313. * @private
  314. * @returns {void}
  315. */
  316. _onSelect(selection) {
  317. this.setState({
  318. isDropdownOpen: false,
  319. selectedNumber: selection.item
  320. });
  321. }
  322. /**
  323. * Sets the internal reference to the DOM/HTML element backing the React
  324. * {@code Component} text area.
  325. *
  326. * @param {HTMLTextAreaElement} element - The DOM/HTML element for this
  327. * {@code Component}'s text area.
  328. * @private
  329. * @returns {void}
  330. */
  331. _setCopyElement(element) {
  332. this._copyElement = element;
  333. }
  334. /**
  335. * Updates the internal state of the currently selected number by defaulting
  336. * to the first available number.
  337. *
  338. * @param {Object} dialInNumbers - The array or object of numbers to parse.
  339. * @private
  340. * @returns {void}
  341. */
  342. _setDefaultNumber(dialInNumbers) {
  343. const numbers = this._formatNumbers(dialInNumbers);
  344. this.setState({
  345. selectedNumber: numbers[0]
  346. });
  347. }
  348. }
  349. /**
  350. * Maps (parts of) the Redux state to the associated
  351. * {@code DialInNumbersForm}'s props.
  352. *
  353. * @param {Object} state - The Redux state.
  354. * @private
  355. * @returns {{
  356. * _localUserDisplayName: React.PropTypes.string,
  357. * _dialIn: React.PropTypes.object
  358. * }}
  359. */
  360. function _mapStateToProps(state) {
  361. return {
  362. _localUserDisplayName: getLocalParticipant(state).name,
  363. _dialIn: state['features/invite']
  364. };
  365. }
  366. export default translate(connect(_mapStateToProps)(DialInNumbersForm));