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.

ProfileTab.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // @flow
  2. import Button from '@atlaskit/button/standard-button';
  3. import { FieldTextStateless } from '@atlaskit/field-text';
  4. import React from 'react';
  5. import UIEvents from '../../../../../service/UI/UIEvents';
  6. import {
  7. sendAnalytics,
  8. createProfilePanelButtonEvent
  9. } from '../../../analytics';
  10. import { AbstractDialogTab } from '../../../base/dialog';
  11. import type { Props as AbstractDialogTabProps } from '../../../base/dialog';
  12. import { translate } from '../../../base/i18n';
  13. import { openLogoutDialog } from '../../actions';
  14. declare var APP: Object;
  15. /**
  16. * The type of the React {@code Component} props of {@link ProfileTab}.
  17. */
  18. export type Props = {
  19. ...$Exact<AbstractDialogTabProps>,
  20. /**
  21. * Whether or not server-side authentication is available.
  22. */
  23. authEnabled: boolean,
  24. /**
  25. * The name of the currently (server-side) authenticated user.
  26. */
  27. authLogin: string,
  28. /**
  29. * The display name to display for the local participant.
  30. */
  31. displayName: string,
  32. /**
  33. * The email to display for the local participant.
  34. */
  35. email: string,
  36. /**
  37. * Invoked to obtain translated strings.
  38. */
  39. t: Function
  40. }
  41. /**
  42. * React {@code Component} for modifying the local user's profile.
  43. *
  44. * @extends Component
  45. */
  46. class ProfileTab extends AbstractDialogTab<Props> {
  47. static defaultProps = {
  48. displayName: '',
  49. email: ''
  50. };
  51. /**
  52. * Initializes a new {@code ConnectedSettingsDialog} instance.
  53. *
  54. * @param {Props} props - The React {@code Component} props to initialize
  55. * the new {@code ConnectedSettingsDialog} instance with.
  56. */
  57. constructor(props: Props) {
  58. super(props);
  59. // Bind event handlers so they are only bound once for every instance.
  60. this._onAuthToggle = this._onAuthToggle.bind(this);
  61. this._onDisplayNameChange = this._onDisplayNameChange.bind(this);
  62. this._onEmailChange = this._onEmailChange.bind(this);
  63. }
  64. _onDisplayNameChange: (Object) => void;
  65. /**
  66. * Changes display name of the user.
  67. *
  68. * @param {Object} e - The key event to handle.
  69. *
  70. * @returns {void}
  71. */
  72. _onDisplayNameChange({ target: { value } }) {
  73. super._onChange({ displayName: value });
  74. }
  75. _onEmailChange: (Object) => void;
  76. /**
  77. * Changes email of the user.
  78. *
  79. * @param {Object} e - The key event to handle.
  80. *
  81. * @returns {void}
  82. */
  83. _onEmailChange({ target: { value } }) {
  84. super._onChange({ email: value });
  85. }
  86. /**
  87. * Implements React's {@link Component#render()}.
  88. *
  89. * @inheritdoc
  90. * @returns {ReactElement}
  91. */
  92. render() {
  93. const {
  94. authEnabled,
  95. displayName,
  96. email,
  97. t
  98. } = this.props;
  99. return (
  100. <div>
  101. <div className = 'profile-edit'>
  102. <div className = 'profile-edit-field'>
  103. <FieldTextStateless
  104. autoComplete = 'name'
  105. compact = { true }
  106. id = 'setDisplayName'
  107. label = { t('profile.setDisplayNameLabel') }
  108. onChange = { this._onDisplayNameChange }
  109. placeholder = { t('settings.name') }
  110. shouldFitContainer = { true }
  111. type = 'text'
  112. value = { displayName } />
  113. </div>
  114. <div className = 'profile-edit-field'>
  115. <FieldTextStateless
  116. compact = { true }
  117. id = 'setEmail'
  118. label = { t('profile.setEmailLabel') }
  119. onChange = { this._onEmailChange }
  120. placeholder = { t('profile.setEmailInput') }
  121. shouldFitContainer = { true }
  122. type = 'text'
  123. value = { email } />
  124. </div>
  125. </div>
  126. { authEnabled && this._renderAuth() }
  127. </div>
  128. );
  129. }
  130. _onAuthToggle: () => void;
  131. /**
  132. * Shows the dialog for logging in or out of a server and closes this
  133. * dialog.
  134. *
  135. * @private
  136. * @returns {void}
  137. */
  138. _onAuthToggle() {
  139. if (this.props.authLogin) {
  140. sendAnalytics(createProfilePanelButtonEvent('logout.button'));
  141. APP.store.dispatch(openLogoutDialog(
  142. () => APP.UI.emitEvent(UIEvents.LOGOUT)
  143. ));
  144. } else {
  145. sendAnalytics(createProfilePanelButtonEvent('login.button'));
  146. APP.UI.emitEvent(UIEvents.AUTH_CLICKED);
  147. }
  148. }
  149. /**
  150. * Returns a React Element for interacting with server-side authentication.
  151. *
  152. * @private
  153. * @returns {ReactElement}
  154. */
  155. _renderAuth() {
  156. const {
  157. authLogin,
  158. t
  159. } = this.props;
  160. return (
  161. <div>
  162. <h2 className = 'mock-atlaskit-label'>
  163. { t('toolbar.authenticate') }
  164. </h2>
  165. { authLogin
  166. && <div className = 'auth-name'>
  167. { t('settings.loggedIn', { name: authLogin }) }
  168. </div> }
  169. <Button
  170. appearance = 'primary'
  171. id = 'login_button'
  172. onClick = { this._onAuthToggle }
  173. type = 'button'>
  174. { authLogin ? t('toolbar.logout') : t('toolbar.login') }
  175. </Button>
  176. </div>
  177. );
  178. }
  179. }
  180. export default translate(ProfileTab);