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.

PasswordRequiredPrompt.native.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { IReduxState, IStore } from '../../app/types';
  4. import { setPassword } from '../../base/conference/actions';
  5. import { IJitsiConference } from '../../base/conference/reducer';
  6. import InputDialog from '../../base/dialog/components/native/InputDialog';
  7. import { _cancelPasswordRequiredPrompt } from '../actions';
  8. /**
  9. * {@code PasswordRequiredPrompt}'s React {@code Component} prop types.
  10. */
  11. interface IProps {
  12. /**
  13. * The previously entered password, if any.
  14. */
  15. _password?: string;
  16. /**
  17. * Number of digits used in the room-lock password.
  18. */
  19. _passwordNumberOfDigits?: number;
  20. /**
  21. * The {@code JitsiConference} which requires a password.
  22. *
  23. * @type {JitsiConference}
  24. */
  25. conference: IJitsiConference;
  26. /**
  27. * The redux dispatch function.
  28. */
  29. dispatch: IStore['dispatch'];
  30. }
  31. interface IState {
  32. /**
  33. * The previously entered password, if any.
  34. */
  35. password?: string;
  36. }
  37. /**
  38. * Implements a React {@code Component} which prompts the user when a password
  39. * is required to join a conference.
  40. */
  41. class PasswordRequiredPrompt extends Component<IProps, IState> {
  42. /**
  43. * Initializes a new {@code PasswordRequiredPrompt} instance.
  44. *
  45. * @param {IProps} props - The read-only React {@code Component} props with
  46. * which the new instance is to be initialized.
  47. */
  48. constructor(props: IProps) {
  49. super(props);
  50. this.state = {
  51. password: props._password
  52. };
  53. // Bind event handlers so they are only bound once per instance.
  54. this._onCancel = this._onCancel.bind(this);
  55. this._onSubmit = this._onSubmit.bind(this);
  56. }
  57. /**
  58. * Implements {@code Component#componentDidUpdate}.
  59. *
  60. * @inheritdoc
  61. */
  62. componentDidUpdate() {
  63. const { _password } = this.props;
  64. // The previous password in Redux gets cleared after the dialog appears and it ends up breaking the dialog
  65. // logic. We move the prop into state and only update it if it has an actual value, avoiding losing the
  66. // previously received value when Redux updates.
  67. if (_password && _password !== this.state.password) {
  68. // eslint-disable-next-line react/no-did-update-set-state
  69. this.setState({
  70. password: _password
  71. });
  72. }
  73. }
  74. /**
  75. * Implements React's {@link Component#render()}.
  76. *
  77. * @inheritdoc
  78. * @returns {ReactElement}
  79. */
  80. render() {
  81. const { password } = this.state;
  82. const { _passwordNumberOfDigits } = this.props;
  83. const textInputProps: any = {
  84. secureTextEntry: true
  85. };
  86. if (_passwordNumberOfDigits) {
  87. textInputProps.keyboardType = 'numeric';
  88. textInputProps.maxLength = _passwordNumberOfDigits;
  89. }
  90. return (
  91. <InputDialog
  92. descriptionKey = 'dialog.passwordLabel'
  93. initialValue = { password }
  94. messageKey = { password ? 'dialog.incorrectRoomLockPassword' : undefined }
  95. onCancel = { this._onCancel }
  96. onSubmit = { this._onSubmit }
  97. textInputProps = { textInputProps }
  98. titleKey = 'dialog.password' />
  99. );
  100. }
  101. /**
  102. * Notifies this prompt that it has been dismissed by cancel.
  103. *
  104. * @private
  105. * @returns {boolean} If this prompt is to be closed/hidden, {@code true};
  106. * otherwise, {@code false}.
  107. */
  108. _onCancel() {
  109. this.props.dispatch(
  110. _cancelPasswordRequiredPrompt(this.props.conference));
  111. return true;
  112. }
  113. /**
  114. * Notifies this prompt that it has been dismissed by submitting a specific
  115. * value.
  116. *
  117. * @param {string|undefined} value - The submitted value.
  118. * @private
  119. * @returns {boolean} If this prompt is to be closed/hidden, {@code true};
  120. * otherwise, {@code false}.
  121. */
  122. _onSubmit(value?: string) {
  123. const { conference } = this.props;
  124. this.props.dispatch(setPassword(conference, conference.join, value));
  125. return true;
  126. }
  127. }
  128. /**
  129. * Maps part of the Redux state to the props of this component.
  130. *
  131. * @param {Object} state - The Redux state.
  132. * @returns {IProps}
  133. */
  134. function _mapStateToProps(state: IReduxState) {
  135. const { roomPasswordNumberOfDigits } = state['features/base/config'];
  136. return {
  137. _password: state['features/base/conference'].password,
  138. _passwordNumberOfDigits: roomPasswordNumberOfDigits
  139. };
  140. }
  141. export default connect(_mapStateToProps)(PasswordRequiredPrompt);