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.

AbstractWelcomePage.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import PropTypes from 'prop-types';
  2. import { Component } from 'react';
  3. import { appNavigate } from '../../app';
  4. import { isRoomValid } from '../../base/conference';
  5. import { generateRoomWithoutSeparator } from '../functions';
  6. /**
  7. * Base (abstract) class for container component rendering the welcome page.
  8. *
  9. * @abstract
  10. */
  11. export class AbstractWelcomePage extends Component {
  12. /**
  13. * {@code AbstractWelcomePage}'s React {@code Component} prop types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. _room: PropTypes.string,
  19. dispatch: PropTypes.func
  20. };
  21. /**
  22. * Initializes a new {@code AbstractWelcomePage} instance.
  23. *
  24. * @param {Object} props - The React {@code Component} props to initialize
  25. * the new {@code AbstractWelcomePage} instance with.
  26. */
  27. constructor(props) {
  28. super(props);
  29. /**
  30. * Save room name into component's local state.
  31. *
  32. * @type {Object}
  33. * @property {number|null} animateTimeoutId - Identifier of the letter
  34. * animation timeout.
  35. * @property {string} generatedRoomname - Automatically generated
  36. * room name.
  37. * @property {string} room - Room name.
  38. * @property {string} roomPlaceholder - Room placeholder
  39. * that's used as a placeholder for input.
  40. * @property {nubmer|null} updateTimeoutId - Identifier of the timeout
  41. * updating the generated room name.
  42. */
  43. this.state = {
  44. animateTimeoutId: null,
  45. generatedRoomname: '',
  46. room: '',
  47. roomPlaceholder: '',
  48. updateTimeoutId: null
  49. };
  50. // Bind event handlers so they are only bound once per instance.
  51. this._animateRoomnameChanging
  52. = this._animateRoomnameChanging.bind(this);
  53. this._onJoin = this._onJoin.bind(this);
  54. this._onRoomChange = this._onRoomChange.bind(this);
  55. this._updateRoomname = this._updateRoomname.bind(this);
  56. }
  57. /**
  58. * This method is executed when component receives new properties.
  59. *
  60. * @inheritdoc
  61. * @param {Object} nextProps - New props component will receive.
  62. */
  63. componentWillReceiveProps(nextProps) {
  64. this.setState({ room: nextProps._room });
  65. }
  66. /**
  67. * This method is executed when method will be unmounted from DOM.
  68. *
  69. * @inheritdoc
  70. */
  71. componentWillUnmount() {
  72. this._clearTimeouts();
  73. }
  74. /**
  75. * Animates the changing of the room name.
  76. *
  77. * @param {string} word - The part of room name that should be added to
  78. * placeholder.
  79. * @private
  80. * @returns {void}
  81. */
  82. _animateRoomnameChanging(word) {
  83. let animateTimeoutId = null;
  84. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  85. if (word.length > 1) {
  86. animateTimeoutId
  87. = setTimeout(
  88. () => {
  89. this._animateRoomnameChanging(
  90. word.substring(1, word.length));
  91. },
  92. 70);
  93. }
  94. this.setState({
  95. animateTimeoutId,
  96. roomPlaceholder
  97. });
  98. }
  99. /**
  100. * Method that clears timeouts for animations and updates of room name.
  101. *
  102. * @private
  103. * @returns {void}
  104. */
  105. _clearTimeouts() {
  106. clearTimeout(this.state.animateTimeoutId);
  107. clearTimeout(this.state.updateTimeoutId);
  108. }
  109. /**
  110. * Determines whether the 'Join' button is (to be) disabled i.e. there's no
  111. * valid room name typed into the respective text input field.
  112. *
  113. * @protected
  114. * @returns {boolean} If the 'Join' button is (to be) disabled, true;
  115. * otherwise, false.
  116. */
  117. _isJoinDisabled() {
  118. return !isRoomValid(this.state.room);
  119. }
  120. /**
  121. * Handles joining. Either by clicking on 'Join' button
  122. * or by pressing 'Enter' in room name input field.
  123. *
  124. * @protected
  125. * @returns {void}
  126. */
  127. _onJoin() {
  128. const room = this.state.room || this.state.generatedRoomname;
  129. room && this.props.dispatch(appNavigate(room));
  130. }
  131. /**
  132. * Handles 'change' event for the room name text input field.
  133. *
  134. * @param {string} value - The text typed into the respective text input
  135. * field.
  136. * @protected
  137. * @returns {void}
  138. */
  139. _onRoomChange(value) {
  140. this.setState({ room: value });
  141. }
  142. /**
  143. * Triggers the generation of a new room name and initiates an animation of
  144. * its changing.
  145. *
  146. * @protected
  147. * @returns {void}
  148. */
  149. _updateRoomname() {
  150. const generatedRoomname = generateRoomWithoutSeparator();
  151. const roomPlaceholder = '';
  152. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  153. this._clearTimeouts();
  154. this.setState(
  155. {
  156. generatedRoomname,
  157. roomPlaceholder,
  158. updateTimeoutId
  159. },
  160. () => this._animateRoomnameChanging(generatedRoomname));
  161. }
  162. }
  163. /**
  164. * Maps (parts of) the redux state to the React {@code Component} props of
  165. * {@code AbstractWelcomePage}.
  166. *
  167. * @param {Object} state - The redux state.
  168. * @protected
  169. * @returns {{
  170. * _room: string
  171. * }}
  172. */
  173. export function _mapStateToProps(state) {
  174. return {
  175. _room: state['features/base/conference'].room
  176. };
  177. }