accountDetails.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. import {updateUser} from 'app/actionCreators/account';
  3. import {APIRequestMethod} from 'app/api';
  4. import AvatarChooser from 'app/components/avatarChooser';
  5. import accountDetailsFields from 'app/data/forms/accountDetails';
  6. import accountPreferencesFields from 'app/data/forms/accountPreferences';
  7. import {t} from 'app/locale';
  8. import {User} from 'app/types';
  9. import AsyncView from 'app/views/asyncView';
  10. import Form from 'app/views/settings/components/forms/form';
  11. import JsonForm from 'app/views/settings/components/forms/jsonForm';
  12. import SettingsPageHeader from 'app/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 {location} = this.props;
  32. const formCommonProps: Partial<Form['props']> = {
  33. apiEndpoint: ENDPOINT,
  34. apiMethod: 'PUT' as APIRequestMethod,
  35. allowUndo: true,
  36. saveOnBlur: true,
  37. onSubmitSuccess: this.handleSubmitSuccess,
  38. };
  39. return (
  40. <div>
  41. <SettingsPageHeader title={t('Account Details')} />
  42. <Form initialData={user} {...formCommonProps}>
  43. <JsonForm
  44. location={location}
  45. forms={accountDetailsFields}
  46. additionalFieldProps={{user}}
  47. />
  48. </Form>
  49. <Form initialData={user.options} {...formCommonProps}>
  50. <JsonForm
  51. location={location}
  52. forms={accountPreferencesFields}
  53. additionalFieldProps={{user}}
  54. />
  55. </Form>
  56. <AvatarChooser
  57. endpoint="/users/me/avatar/"
  58. model={user}
  59. onSave={resp => {
  60. this.handleSubmitSuccess(resp as User);
  61. }}
  62. isUser
  63. />
  64. </div>
  65. );
  66. }
  67. }
  68. export default AccountDetails;