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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. joining: false,
  47. room: '',
  48. roomPlaceholder: '',
  49. updateTimeoutId: null
  50. };
  51. // Bind event handlers so they are only bound once per instance.
  52. this._animateRoomnameChanging
  53. = this._animateRoomnameChanging.bind(this);
  54. this._onJoin = this._onJoin.bind(this);
  55. this._onRoomChange = this._onRoomChange.bind(this);
  56. this._updateRoomname = this._updateRoomname.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#componentWillMount()}. Invoked
  60. * immediately before mounting occurs.
  61. *
  62. * @inheritdoc
  63. */
  64. componentWillMount() {
  65. this._mounted = true;
  66. }
  67. /**
  68. * Implements React's {@link Component#componentWillReceiveProps()}. Invoked
  69. * before this mounted component receives new props.
  70. *
  71. * @inheritdoc
  72. * @param {Object} nextProps - New props component will receive.
  73. */
  74. componentWillReceiveProps(nextProps) {
  75. this.setState({ room: nextProps._room });
  76. }
  77. /**
  78. * Implements React's {@link Component#componentWillUnmount()}. Invoked
  79. * immediately before this component is unmounted and destroyed.
  80. *
  81. * @inheritdoc
  82. */
  83. componentWillUnmount() {
  84. this._clearTimeouts();
  85. this._mounted = false;
  86. }
  87. /**
  88. * Animates the changing of the room name.
  89. *
  90. * @param {string} word - The part of room name that should be added to
  91. * placeholder.
  92. * @private
  93. * @returns {void}
  94. */
  95. _animateRoomnameChanging(word) {
  96. let animateTimeoutId = null;
  97. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  98. if (word.length > 1) {
  99. animateTimeoutId
  100. = setTimeout(
  101. () => {
  102. this._animateRoomnameChanging(
  103. word.substring(1, word.length));
  104. },
  105. 70);
  106. }
  107. this.setState({
  108. animateTimeoutId,
  109. roomPlaceholder
  110. });
  111. }
  112. /**
  113. * Method that clears timeouts for animations and updates of room name.
  114. *
  115. * @private
  116. * @returns {void}
  117. */
  118. _clearTimeouts() {
  119. clearTimeout(this.state.animateTimeoutId);
  120. clearTimeout(this.state.updateTimeoutId);
  121. }
  122. /**
  123. * Determines whether the 'Join' button is (to be) disabled i.e. there's no
  124. * valid room name typed into the respective text input field.
  125. *
  126. * @protected
  127. * @returns {boolean} If the 'Join' button is (to be) disabled, true;
  128. * otherwise, false.
  129. */
  130. _isJoinDisabled() {
  131. return this.state.joining || !isRoomValid(this.state.room);
  132. }
  133. /**
  134. * Handles joining. Either by clicking on 'Join' button
  135. * or by pressing 'Enter' in room name input field.
  136. *
  137. * @protected
  138. * @returns {void}
  139. */
  140. _onJoin() {
  141. const room = this.state.room || this.state.generatedRoomname;
  142. if (room) {
  143. this.setState({ joining: true });
  144. // By the time the Promise of appNavigate settles, this component
  145. // may have already been unmounted.
  146. const onAppNavigateSettled = () => {
  147. this._mounted && this.setState({ joining: false });
  148. };
  149. this.props.dispatch(appNavigate(room))
  150. .then(onAppNavigateSettled, onAppNavigateSettled);
  151. }
  152. }
  153. /**
  154. * Handles 'change' event for the room name text input field.
  155. *
  156. * @param {string} value - The text typed into the respective text input
  157. * field.
  158. * @protected
  159. * @returns {void}
  160. */
  161. _onRoomChange(value) {
  162. this.setState({ room: value });
  163. }
  164. /**
  165. * Triggers the generation of a new room name and initiates an animation of
  166. * its changing.
  167. *
  168. * @protected
  169. * @returns {void}
  170. */
  171. _updateRoomname() {
  172. const generatedRoomname = generateRoomWithoutSeparator();
  173. const roomPlaceholder = '';
  174. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  175. this._clearTimeouts();
  176. this.setState(
  177. {
  178. generatedRoomname,
  179. roomPlaceholder,
  180. updateTimeoutId
  181. },
  182. () => this._animateRoomnameChanging(generatedRoomname));
  183. }
  184. }
  185. /**
  186. * Maps (parts of) the redux state to the React {@code Component} props of
  187. * {@code AbstractWelcomePage}.
  188. *
  189. * @param {Object} state - The redux state.
  190. * @protected
  191. * @returns {{
  192. * _room: string
  193. * }}
  194. */
  195. export function _mapStateToProps(state) {
  196. return {
  197. _room: state['features/base/conference'].room
  198. };
  199. }