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

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