accountDetails.tsx 2.1 KB

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