accountDetails.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {updateUser} from 'sentry/actionCreators/account';
  2. import {APIRequestMethod} from 'sentry/api';
  3. import AvatarChooser from 'sentry/components/avatarChooser';
  4. import Form, {FormProps} from 'sentry/components/forms/form';
  5. import JsonForm from 'sentry/components/forms/jsonForm';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import accountDetailsFields from 'sentry/data/forms/accountDetails';
  8. import accountPreferencesFields from 'sentry/data/forms/accountPreferences';
  9. import {t} from 'sentry/locale';
  10. import {Organization, User} from 'sentry/types';
  11. import withOrganization from 'sentry/utils/withOrganization';
  12. import DeprecatedAsyncView, {AsyncViewProps} from 'sentry/views/deprecatedAsyncView';
  13. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  14. const ENDPOINT = '/users/me/';
  15. interface Props extends AsyncViewProps {
  16. organization: Organization;
  17. }
  18. class AccountDetails extends DeprecatedAsyncView<Props> {
  19. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  20. // local state is NOT updated when the form saves
  21. return [['user', ENDPOINT]];
  22. }
  23. handleSubmitSuccess = (user: User) => {
  24. // the updateUser method updates our Config Store
  25. // No components listen to the ConfigStore, they just access it directly
  26. updateUser(user);
  27. // We need to update the state, because AvatarChooser is using it,
  28. // otherwise it will flick
  29. this.setState({
  30. user,
  31. });
  32. };
  33. renderBody() {
  34. const user = this.state.user as User;
  35. const formCommonProps: Partial<FormProps> = {
  36. apiEndpoint: ENDPOINT,
  37. apiMethod: 'PUT' as APIRequestMethod,
  38. allowUndo: true,
  39. saveOnBlur: true,
  40. onSubmitSuccess: this.handleSubmitSuccess,
  41. };
  42. return (
  43. <div>
  44. <SentryDocumentTitle title={t('Account Details')} />
  45. <SettingsPageHeader title={t('Account Details')} />
  46. <Form initialData={user} {...formCommonProps}>
  47. <JsonForm forms={accountDetailsFields} additionalFieldProps={{user}} />
  48. </Form>
  49. <Form initialData={user.options} {...formCommonProps}>
  50. <JsonForm
  51. forms={accountPreferencesFields}
  52. additionalFieldProps={{
  53. user,
  54. organization: this.props.organization,
  55. }}
  56. />
  57. </Form>
  58. <AvatarChooser
  59. endpoint="/users/me/avatar/"
  60. model={user}
  61. onSave={resp => {
  62. this.handleSubmitSuccess(resp as User);
  63. }}
  64. isUser
  65. />
  66. </div>
  67. );
  68. }
  69. }
  70. export default withOrganization(AccountDetails);