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.

AddPasswordForm.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { setPassword } from '../../base/conference';
  4. import { translate } from '../../base/i18n';
  5. /**
  6. * A React {@code Component} for locking a JitsiConference with a password.
  7. */
  8. class AddPasswordForm extends Component {
  9. /**
  10. * {@code AddPasswordForm}'s property types.
  11. *
  12. * @static
  13. */
  14. static propTypes = {
  15. /**
  16. * The JitsiConference on which to lock and set a password.
  17. *
  18. * @type {JitsiConference}
  19. */
  20. conference: React.PropTypes.object,
  21. /**
  22. * Invoked to set a password on the conference.
  23. */
  24. dispatch: React.PropTypes.func,
  25. /**
  26. * Invoked to obtain translated strings.
  27. */
  28. t: React.PropTypes.func
  29. };
  30. /**
  31. * Initializes a new {@code AddPasswordForm} instance.
  32. *
  33. * @param {Object} props - The read-only properties with which the new
  34. * instance is to be initialized.
  35. */
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. /**
  40. * The current value to display in {@code AddPasswordForm}
  41. * component's input field. The value is also used as the desired
  42. * new password when creating a {@code setPassword} action.
  43. *
  44. * @type {string}
  45. */
  46. password: ''
  47. };
  48. // Bind event handlers so they are only bound once for every instance.
  49. this._onKeyDown = this._onKeyDown.bind(this);
  50. this._onPasswordChange = this._onPasswordChange.bind(this);
  51. this._onSubmit = this._onSubmit.bind(this);
  52. }
  53. /**
  54. * Implements React's {@link Component#render()}.
  55. *
  56. * @inheritdoc
  57. * @returns {ReactElement}
  58. */
  59. render() {
  60. const { t } = this.props;
  61. return (
  62. <div
  63. className = 'form-control'
  64. onSubmit = { this._onSubmit } >
  65. <div className = 'form-control__container'>
  66. <input
  67. autoFocus = { true }
  68. className = 'input-control'
  69. id = 'newPasswordInput'
  70. onChange = { this._onPasswordChange }
  71. onKeyDown = { this._onKeyDown }
  72. placeholder = { t('dialog.createPassword') }
  73. type = 'text' />
  74. <button
  75. className = 'button-control button-control_light'
  76. disabled = { !this.state.password }
  77. id = 'addPasswordBtn'
  78. onClick = { this._onSubmit }
  79. type = 'button'>
  80. { t('dialog.add') }
  81. </button>
  82. </div>
  83. </div>
  84. );
  85. }
  86. /**
  87. * Mimics form behavior by listening for enter key press and submitting the
  88. * entered password.
  89. *
  90. * @param {Object} event - DOM Event for keydown.
  91. * @private
  92. * @returns {void}
  93. */
  94. _onKeyDown(event) {
  95. event.stopPropagation();
  96. if (event.keyCode === /* Enter */ 13) {
  97. this._onSubmit();
  98. }
  99. }
  100. /**
  101. * Updates the internal state of the entered password.
  102. *
  103. * @param {Object} event - DOM Event for value change.
  104. * @private
  105. * @returns {void}
  106. */
  107. _onPasswordChange(event) {
  108. this.setState({ password: event.target.value });
  109. }
  110. /**
  111. * Dispatches a request to lock the conference with a password.
  112. *
  113. * @private
  114. * @returns {void}
  115. */
  116. _onSubmit() {
  117. if (!this.state.password) {
  118. return;
  119. }
  120. const { conference } = this.props;
  121. this.props.dispatch(setPassword(
  122. conference,
  123. conference.lock,
  124. this.state.password
  125. ));
  126. this.setState({ password: '' });
  127. }
  128. }
  129. export default translate(connect()(AddPasswordForm));