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.

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 Component which allows the user to dial out from the
  9. * 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. * Formats the dial number in a way to remove all non digital characters
  84. * from it (including spaces, brackets, dash, dot, etc.).
  85. *
  86. * @param {string} dialNumber - The phone number to format.
  87. * @private
  88. * @returns {string} - The formatted phone number.
  89. */
  90. _formatDialNumber(dialNumber) {
  91. return dialNumber.replace(/\D/g, '');
  92. }
  93. /**
  94. * Renders the dialog content.
  95. *
  96. * @returns {XML}
  97. * @private
  98. */
  99. _renderContent() {
  100. const { _isDialNumberAllowed } = this.props;
  101. return (
  102. <div className = 'dial-out-content'>
  103. { _isDialNumberAllowed ? '' : this._renderErrorMessage() }
  104. <DialOutNumbersForm
  105. onChange = { this._onDialNumberChange } />
  106. </div>);
  107. }
  108. /**
  109. * Renders the error message to display if the dial phone number is not
  110. * allowed.
  111. *
  112. * @returns {XML}
  113. * @private
  114. */
  115. _renderErrorMessage() {
  116. const { t } = this.props;
  117. return (
  118. <div className = 'dial-out-error'>
  119. { t('dialOut.phoneNotAllowed') }
  120. </div>);
  121. }
  122. /**
  123. * Cancel the dial out.
  124. *
  125. * @private
  126. * @returns {boolean} - Returns true to indicate that the dialog should be
  127. * closed.
  128. */
  129. _onCancel() {
  130. this.props.cancel();
  131. return true;
  132. }
  133. /**
  134. * Dials the number.
  135. *
  136. * @private
  137. * @returns {boolean} - Returns true to indicate that the dialog should be
  138. * closed.
  139. */
  140. _onSubmit() {
  141. if (this.props._isDialNumberAllowed) {
  142. this.props.dial(this.state.dialNumber);
  143. }
  144. return true;
  145. }
  146. /**
  147. * Updates the dialNumber and check for validity.
  148. *
  149. * @param {string} dialCode - The dial code value.
  150. * @param {string} dialInput - The dial input value.
  151. * @private
  152. * @returns {void}
  153. */
  154. _onDialNumberChange(dialCode, dialInput) {
  155. // We remove all starting zeros from the dial input before attaching it
  156. // to the country code.
  157. const formattedDialInput = dialInput.replace(/^(0+)/, '');
  158. const dialNumber = `${dialCode}${formattedDialInput}`;
  159. const formattedNumber = this._formatDialNumber(dialNumber);
  160. this.props.checkDialNumber(formattedNumber);
  161. this.setState({
  162. dialNumber: formattedNumber,
  163. isDialInputEmpty: !formattedDialInput
  164. || formattedDialInput.length === 0
  165. });
  166. }
  167. }
  168. /**
  169. * Maps (parts of) the Redux state to the associated
  170. * {@code DialOutDialog}'s props.
  171. *
  172. * @param {Object} state - The Redux state.
  173. * @private
  174. * @returns {{
  175. * _isDialNumberAllowed: React.PropTypes.bool
  176. * }}
  177. */
  178. function _mapStateToProps(state) {
  179. const { isDialNumberAllowed } = state['features/dial-out'];
  180. return {
  181. /**
  182. * Property indicating if a dial number is allowed.
  183. *
  184. * @private
  185. * @type {boolean}
  186. */
  187. _isDialNumberAllowed: isDialNumberAllowed
  188. };
  189. }
  190. export default translate(
  191. connect(_mapStateToProps, {
  192. cancel,
  193. dial,
  194. checkDialNumber
  195. })(DialOutDialog));