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.4KB

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