您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DialOutDialog.web.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { translate } from '../../base/i18n';
  4. import { Dialog } from '../../base/dialog';
  5. import { cancel, checkDialNumber, dial } from '../actions';
  6. import DialOutNumbersForm from './DialOutNumbersForm';
  7. /**
  8. * Implements a React {@link Component} which allows the user to dial out from
  9. * the conference.
  10. */
  11. class DialOutDialog extends Component {
  12. /**
  13. * {@code DialOutDialog} component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * Property indicating if a dial number is allowed.
  20. */
  21. _isDialNumberAllowed: React.PropTypes.bool,
  22. /**
  23. * The function performing the cancel action.
  24. */
  25. cancel: React.PropTypes.func,
  26. /**
  27. * The function performing the phone number validity check.
  28. */
  29. checkDialNumber: React.PropTypes.func,
  30. /**
  31. * The function performing the dial action.
  32. */
  33. dial: React.PropTypes.func,
  34. /**
  35. * Invoked to obtain translated strings.
  36. */
  37. t: React.PropTypes.func
  38. };
  39. /**
  40. * Initializes a new {@code DialOutNumbersForm} instance.
  41. *
  42. * @param {Object} props - The read-only properties with which the new
  43. * instance is to be initialized.
  44. */
  45. constructor(props) {
  46. super(props);
  47. this.state = {
  48. /**
  49. * The number to dial.
  50. */
  51. dialNumber: '',
  52. /**
  53. * Indicates if the dial input is currently empty.
  54. */
  55. isDialInputEmpty: true
  56. };
  57. // Bind event handlers so they are only bound once for every instance.
  58. this._onDialNumberChange = this._onDialNumberChange.bind(this);
  59. this._onCancel = this._onCancel.bind(this);
  60. this._onSubmit = this._onSubmit.bind(this);
  61. }
  62. /**
  63. * Implements React's {@link Component#render()}.
  64. *
  65. * @inheritdoc
  66. * @returns {ReactElement}
  67. */
  68. render() {
  69. const { _isDialNumberAllowed } = this.props;
  70. return (
  71. <Dialog
  72. okDisabled = { this.state.isDialInputEmpty
  73. || !_isDialNumberAllowed }
  74. okTitleKey = 'dialOut.dial'
  75. onCancel = { this._onCancel }
  76. onSubmit = { this._onSubmit }
  77. titleKey = 'dialOut.dialOut'
  78. width = 'small'>
  79. { this._renderContent() }
  80. </Dialog>
  81. );
  82. }
  83. /**
  84. * Formats the dial number in a way to remove all non digital characters
  85. * from it (including spaces, brackets, dash, dot, etc.).
  86. *
  87. * @param {string} dialNumber - The phone number to format.
  88. * @private
  89. * @returns {string} - The formatted phone number.
  90. */
  91. _formatDialNumber(dialNumber) {
  92. return dialNumber.replace(/\D/g, '');
  93. }
  94. /**
  95. * Renders the dialog content.
  96. *
  97. * @returns {ReactElement}
  98. * @private
  99. */
  100. _renderContent() {
  101. const { _isDialNumberAllowed } = this.props;
  102. return (
  103. <div className = 'dial-out-content'>
  104. { _isDialNumberAllowed ? '' : this._renderErrorMessage() }
  105. <DialOutNumbersForm
  106. onChange = { this._onDialNumberChange } />
  107. </div>);
  108. }
  109. /**
  110. * Renders the error message to display if the dial phone number is not
  111. * allowed.
  112. *
  113. * @returns {ReactElement}
  114. * @private
  115. */
  116. _renderErrorMessage() {
  117. const { t } = this.props;
  118. return (
  119. <div className = 'dial-out-error'>
  120. { t('dialOut.phoneNotAllowed') }
  121. </div>);
  122. }
  123. /**
  124. * Cancel the dial out.
  125. *
  126. * @private
  127. * @returns {boolean} - Returns true to indicate that the dialog should be
  128. * closed.
  129. */
  130. _onCancel() {
  131. this.props.cancel();
  132. return true;
  133. }
  134. /**
  135. * Dials the number.
  136. *
  137. * @private
  138. * @returns {boolean} - Returns true to indicate that the dialog should be
  139. * closed.
  140. */
  141. _onSubmit() {
  142. if (this.props._isDialNumberAllowed) {
  143. this.props.dial(this.state.dialNumber);
  144. }
  145. return true;
  146. }
  147. /**
  148. * Updates the dialNumber and check for validity.
  149. *
  150. * @param {string} dialCode - The dial code value.
  151. * @param {string} dialInput - The dial input value.
  152. * @private
  153. * @returns {void}
  154. */
  155. _onDialNumberChange(dialCode, dialInput) {
  156. // We remove all starting zeros from the dial input before attaching it
  157. // to the country code.
  158. const formattedDialInput = dialInput.replace(/^(0+)/, '');
  159. const dialNumber = `${dialCode}${formattedDialInput}`;
  160. const formattedNumber = this._formatDialNumber(dialNumber);
  161. this.props.checkDialNumber(formattedNumber);
  162. this.setState({
  163. dialNumber: formattedNumber,
  164. isDialInputEmpty: !formattedDialInput
  165. || formattedDialInput.length === 0
  166. });
  167. }
  168. }
  169. /**
  170. * Maps (parts of) the Redux state to the associated
  171. * {@code DialOutDialog}'s props.
  172. *
  173. * @param {Object} state - The Redux state.
  174. * @private
  175. * @returns {{
  176. * _isDialNumberAllowed: React.PropTypes.bool
  177. * }}
  178. */
  179. function _mapStateToProps(state) {
  180. const { isDialNumberAllowed } = state['features/dial-out'];
  181. return {
  182. /**
  183. * Property indicating if a dial number is allowed.
  184. *
  185. * @private
  186. * @type {boolean}
  187. */
  188. _isDialNumberAllowed: isDialNumberAllowed
  189. };
  190. }
  191. export default translate(
  192. connect(_mapStateToProps, {
  193. cancel,
  194. checkDialNumber,
  195. dial
  196. })(DialOutDialog));