Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DialOutDialog.web.js 6.3KB

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