UserProfileEditForm.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Trans, t } from '@lingui/macro';
  2. import React, { FC } from 'react';
  3. import { selectors } from '@grafana/e2e-selectors';
  4. import { Button, Field, FieldSet, Form, Icon, Input, Tooltip } from '@grafana/ui';
  5. import config from 'app/core/config';
  6. import { UserDTO } from 'app/types';
  7. import { ProfileUpdateFields } from './types';
  8. export interface Props {
  9. user: UserDTO | null;
  10. isSavingUser: boolean;
  11. updateProfile: (payload: ProfileUpdateFields) => void;
  12. }
  13. const { disableLoginForm } = config;
  14. export const UserProfileEditForm: FC<Props> = ({ user, isSavingUser, updateProfile }) => {
  15. const onSubmitProfileUpdate = (data: ProfileUpdateFields) => {
  16. updateProfile(data);
  17. };
  18. return (
  19. <Form onSubmit={onSubmitProfileUpdate} validateOn="onBlur">
  20. {({ register, errors }) => {
  21. return (
  22. <FieldSet label={<Trans id="user-profile.title">Edit profile</Trans>}>
  23. <Field
  24. label={t({ id: 'user-profile.fields.name-label', message: 'Name' })}
  25. invalid={!!errors.name}
  26. error={<Trans id="user-profile.fields.name-error">Name is required</Trans>}
  27. disabled={disableLoginForm}
  28. >
  29. <Input
  30. {...register('name', { required: true })}
  31. id="edit-user-profile-name"
  32. placeholder={t({ id: 'user-profile.fields.name-label', message: 'Name' })}
  33. defaultValue={user?.name ?? ''}
  34. suffix={<InputSuffix />}
  35. />
  36. </Field>
  37. <Field
  38. label={t({ id: 'user-profile.fields.email-label', message: 'Email' })}
  39. invalid={!!errors.email}
  40. error={<Trans id="user-profile.fields.email-error">Email is required</Trans>}
  41. disabled={disableLoginForm}
  42. >
  43. <Input
  44. {...register('email', { required: true })}
  45. id="edit-user-profile-email"
  46. placeholder={t({ id: 'user-profile.fields.email-label', message: 'Email' })}
  47. defaultValue={user?.email ?? ''}
  48. suffix={<InputSuffix />}
  49. />
  50. </Field>
  51. <Field
  52. label={t({ id: 'user-profile.fields.username-label', message: 'Username' })}
  53. disabled={disableLoginForm}
  54. >
  55. <Input
  56. {...register('login')}
  57. id="edit-user-profile-username"
  58. defaultValue={user?.login ?? ''}
  59. placeholder={t({ id: 'user-profile.fields.username-label', message: 'Username' })}
  60. suffix={<InputSuffix />}
  61. />
  62. </Field>
  63. <div className="gf-form-button-row">
  64. <Button
  65. variant="primary"
  66. disabled={isSavingUser}
  67. data-testid={selectors.components.UserProfile.profileSaveButton}
  68. type="submit"
  69. >
  70. <Trans id="common.save">Save</Trans>
  71. </Button>
  72. </div>
  73. </FieldSet>
  74. );
  75. }}
  76. </Form>
  77. );
  78. };
  79. export default UserProfileEditForm;
  80. const InputSuffix: FC = () => {
  81. return disableLoginForm ? (
  82. <Tooltip content="Login details locked because they are managed in another system.">
  83. <Icon name="lock" />
  84. </Tooltip>
  85. ) : null;
  86. };