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 6.3KB

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