2022-01-17 16:58:49 +00:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2024-03-16 08:48:05 +01:00
|
|
|
import { Button, Field, FieldSet, Icon, Input, Tooltip } from '@grafana/ui';
|
|
|
|
|
import { Form } from 'app/core/components/Form/Form';
|
2019-08-19 13:40:14 -04:00
|
|
|
import config from 'app/core/config';
|
2022-10-06 16:34:04 +01:00
|
|
|
import { t, Trans } from 'app/core/internationalization';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { UserDTO } from 'app/types';
|
|
|
|
|
|
2021-05-25 14:48:16 +02:00
|
|
|
import { ProfileUpdateFields } from './types';
|
2019-08-19 13:40:14 -04:00
|
|
|
|
|
|
|
|
export interface Props {
|
2021-05-25 14:48:16 +02:00
|
|
|
user: UserDTO | null;
|
2019-08-19 13:40:14 -04:00
|
|
|
isSavingUser: boolean;
|
|
|
|
|
updateProfile: (payload: ProfileUpdateFields) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 11:19:00 -04:00
|
|
|
const { disableLoginForm } = config;
|
2019-08-19 13:40:14 -04:00
|
|
|
|
2023-03-14 07:38:21 -07:00
|
|
|
export const UserProfileEditForm = ({ user, isSavingUser, updateProfile }: Props) => {
|
2020-06-08 11:19:00 -04:00
|
|
|
const onSubmitProfileUpdate = (data: ProfileUpdateFields) => {
|
|
|
|
|
updateProfile(data);
|
2019-08-19 13:40:14 -04:00
|
|
|
};
|
|
|
|
|
|
2022-07-15 07:10:20 +00:00
|
|
|
// check if authLabels is longer than 0 otherwise false
|
|
|
|
|
const isExternalUser: boolean = (user && user.isExternal) ?? false;
|
|
|
|
|
const authSource = isExternalUser && user && user.authLabels ? user.authLabels[0] : '';
|
|
|
|
|
const lockMessage = authSource ? ` (Synced via ${authSource})` : '';
|
|
|
|
|
const disabledEdit = disableLoginForm || isExternalUser;
|
|
|
|
|
|
2020-06-08 11:19:00 -04:00
|
|
|
return (
|
|
|
|
|
<Form onSubmit={onSubmitProfileUpdate} validateOn="onBlur">
|
|
|
|
|
{({ register, errors }) => {
|
|
|
|
|
return (
|
2023-09-20 15:34:56 +05:30
|
|
|
<>
|
|
|
|
|
<FieldSet>
|
|
|
|
|
<Field
|
|
|
|
|
label={t('user-profile.fields.name-label', 'Name') + lockMessage}
|
|
|
|
|
invalid={!!errors.name}
|
|
|
|
|
error={<Trans i18nKey="user-profile.fields.name-error">Name is required</Trans>}
|
|
|
|
|
disabled={disabledEdit}
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
{...register('name', { required: true })}
|
|
|
|
|
id="edit-user-profile-name"
|
|
|
|
|
placeholder={t('user-profile.fields.name-label', 'Name')}
|
|
|
|
|
defaultValue={user?.name ?? ''}
|
|
|
|
|
suffix={<InputSuffix />}
|
|
|
|
|
/>
|
|
|
|
|
</Field>
|
2022-01-17 16:58:49 +00:00
|
|
|
|
2023-09-20 15:34:56 +05:30
|
|
|
<Field
|
|
|
|
|
label={t('user-profile.fields.email-label', 'Email') + lockMessage}
|
|
|
|
|
invalid={!!errors.email}
|
|
|
|
|
error={<Trans i18nKey="user-profile.fields.email-error">Email is required</Trans>}
|
|
|
|
|
disabled={disabledEdit}
|
2022-01-17 16:58:49 +00:00
|
|
|
>
|
2023-09-20 15:34:56 +05:30
|
|
|
<Input
|
|
|
|
|
{...register('email', { required: true })}
|
|
|
|
|
id="edit-user-profile-email"
|
|
|
|
|
placeholder={t('user-profile.fields.email-label', 'Email')}
|
|
|
|
|
defaultValue={user?.email ?? ''}
|
|
|
|
|
suffix={<InputSuffix />}
|
|
|
|
|
/>
|
|
|
|
|
</Field>
|
|
|
|
|
|
|
|
|
|
<Field label={t('user-profile.fields.username-label', 'Username') + lockMessage} disabled={disabledEdit}>
|
|
|
|
|
<Input
|
|
|
|
|
{...register('login')}
|
|
|
|
|
id="edit-user-profile-username"
|
|
|
|
|
defaultValue={user?.login ?? ''}
|
|
|
|
|
placeholder={t('user-profile.fields.username-label', 'Username') + lockMessage}
|
|
|
|
|
suffix={<InputSuffix />}
|
|
|
|
|
/>
|
|
|
|
|
</Field>
|
|
|
|
|
</FieldSet>
|
|
|
|
|
<Button
|
|
|
|
|
variant="primary"
|
|
|
|
|
disabled={isSavingUser || disabledEdit}
|
|
|
|
|
data-testid={selectors.components.UserProfile.profileSaveButton}
|
|
|
|
|
type="submit"
|
|
|
|
|
>
|
|
|
|
|
<Trans i18nKey="common.save">Save</Trans>
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
2020-06-08 11:19:00 -04:00
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
};
|
2019-08-19 13:40:14 -04:00
|
|
|
|
|
|
|
|
export default UserProfileEditForm;
|
2020-06-08 11:19:00 -04:00
|
|
|
|
2022-10-14 10:24:32 +01:00
|
|
|
const InputSuffix = () => {
|
2020-06-08 11:19:00 -04:00
|
|
|
return disableLoginForm ? (
|
2021-03-31 16:07:37 -07:00
|
|
|
<Tooltip content="Login details locked because they are managed in another system.">
|
2020-06-08 11:19:00 -04:00
|
|
|
<Icon name="lock" />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
) : null;
|
|
|
|
|
};
|