accountDetails.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 {User} from 'sentry/types';
  11. import AsyncView from 'sentry/views/asyncView';
  12. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  13. const ENDPOINT = '/users/me/';
  14. class AccountDetails extends AsyncView {
  15. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  16. // local state is NOT updated when the form saves
  17. return [['user', ENDPOINT]];
  18. }
  19. handleSubmitSuccess = (user: User) => {
  20. // the updateUser method updates our Config Store
  21. // No components listen to the ConfigStore, they just access it directly
  22. updateUser(user);
  23. // We need to update the state, because AvatarChooser is using it,
  24. // otherwise it will flick
  25. this.setState({
  26. user,
  27. });
  28. };
  29. renderBody() {
  30. const user = this.state.user as User;
  31. const formCommonProps: Partial<FormProps> = {
  32. apiEndpoint: ENDPOINT,
  33. apiMethod: 'PUT' as APIRequestMethod,
  34. allowUndo: true,
  35. saveOnBlur: true,
  36. onSubmitSuccess: this.handleSubmitSuccess,
  37. };
  38. return (
  39. <div>
  40. <SentryDocumentTitle title={t('Account Details')} />
  41. <SettingsPageHeader title={t('Account Details')} />
  42. <Form initialData={user} {...formCommonProps}>
  43. <JsonForm forms={accountDetailsFields} additionalFieldProps={{user}} />
  44. </Form>
  45. <Form initialData={user.options} {...formCommonProps}>
  46. <JsonForm forms={accountPreferencesFields} additionalFieldProps={{user}} />
  47. </Form>
  48. <AvatarChooser
  49. endpoint="/users/me/avatar/"
  50. model={user}
  51. onSave={resp => {
  52. this.handleSubmitSuccess(resp as User);
  53. }}
  54. isUser
  55. />
  56. </div>
  57. );
  58. }
  59. }
  60. export default AccountDetails;