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.

DisplayNamePrompt.web.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { FieldTextStateless as TextField } from '@atlaskit/field-text';
  5. import { Dialog } from '../../base/dialog';
  6. import { translate } from '../../base/i18n';
  7. import {
  8. getLocalParticipant,
  9. participantDisplayNameChanged
  10. } from '../../base/participants';
  11. /**
  12. * The type of the React {@code Component} props of {@link DisplayNamePrompt}.
  13. */
  14. type Props = {
  15. /**
  16. * The current ID for the local participant. Used for setting the display
  17. * name on the associated participant.
  18. */
  19. _localParticipantID: string,
  20. /**
  21. * Invoked to update the local participant's display name.
  22. */
  23. dispatch: Dispatch<*>,
  24. /**
  25. * Invoked to obtain translated strings.
  26. */
  27. t: Function
  28. };
  29. /**
  30. * The type of the React {@code Component} props of {@link DisplayNamePrompt}.
  31. */
  32. type State = {
  33. /**
  34. * The name to show in the display name text field.
  35. */
  36. displayName: string
  37. };
  38. /**
  39. * Implements a React {@code Component} for displaying a dialog with an field
  40. * for setting the local participant's display name.
  41. *
  42. * @extends Component
  43. */
  44. class DisplayNamePrompt extends Component<Props, State> {
  45. /**
  46. * Initializes a new {@code DisplayNamePrompt} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props: Props) {
  52. super(props);
  53. this.state = {
  54. displayName: ''
  55. };
  56. // Bind event handlers so they are only bound once for every instance.
  57. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  58. this._onSubmit = this._onSubmit.bind(this);
  59. }
  60. /**
  61. * Implements React's {@link Component#render()}.
  62. *
  63. * @inheritdoc
  64. * @returns {ReactElement}
  65. */
  66. render() {
  67. return (
  68. <Dialog
  69. isModal = { true }
  70. onSubmit = { this._onSubmit }
  71. titleKey = 'dialog.displayNameRequired'
  72. width = 'small'>
  73. <TextField
  74. autoFocus = { true }
  75. compact = { true }
  76. label = { this.props.t('dialog.enterDisplayName') }
  77. name = 'displayName'
  78. onChange = { this._onDisplayNameChange }
  79. shouldFitContainer = { true }
  80. type = 'text'
  81. value = { this.state.displayName } />
  82. </Dialog>);
  83. }
  84. _onDisplayNameChange: (Object) => void;
  85. /**
  86. * Updates the entered display name.
  87. *
  88. * @param {Object} event - The DOM event triggered from the entered display
  89. * name value having changed.
  90. * @private
  91. * @returns {void}
  92. */
  93. _onDisplayNameChange(event) {
  94. this.setState({
  95. displayName: event.target.value
  96. });
  97. }
  98. _onSubmit: () => boolean;
  99. /**
  100. * Dispatches an action to update the local participant's display name. A
  101. * name must be entered for the action to dispatch.
  102. *
  103. * @private
  104. * @returns {boolean}
  105. */
  106. _onSubmit() {
  107. const { displayName } = this.state;
  108. if (!displayName.trim()) {
  109. return false;
  110. }
  111. const { dispatch, _localParticipantID } = this.props;
  112. dispatch(
  113. participantDisplayNameChanged(_localParticipantID, displayName));
  114. return true;
  115. }
  116. }
  117. /**
  118. * Maps (parts of) the Redux state to the associated {@code DisplayNamePrompt}'s
  119. * props.
  120. *
  121. * @param {Object} state - The Redux state.
  122. * @private
  123. * @returns {{
  124. * _localParticipantID: string
  125. * }}
  126. */
  127. function _mapStateToProps(state) {
  128. const { id } = getLocalParticipant(state);
  129. return {
  130. /**
  131. * The current ID for the local participant.
  132. *
  133. * @type {string}
  134. */
  135. _localParticipantID: id
  136. };
  137. }
  138. export default translate(connect(_mapStateToProps)(DisplayNamePrompt));