Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractWelcomePage.js 6.9KB

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