mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
import {
|
|
Dropdown,
|
|
Item,
|
|
Form,
|
|
FormRow,
|
|
FormButtons,
|
|
FormInput,
|
|
TextField,
|
|
SubmitButton,
|
|
PasswordField,
|
|
} from 'react-basics';
|
|
import { useIntl } from 'react-intl';
|
|
import useApi from 'hooks/useApi';
|
|
import { ROLES } from 'lib/constants';
|
|
import { labels, messages } from 'components/messages';
|
|
|
|
export default function UserEditForm({ userId, data, onSave }) {
|
|
const { formatMessage } = useIntl();
|
|
const { post, useMutation } = useApi();
|
|
const { mutate, error } = useMutation(({ username }) => post(`/users/${userId}`, { username }));
|
|
|
|
const handleSubmit = async data => {
|
|
mutate(data, {
|
|
onSuccess: async () => {
|
|
onSave(data);
|
|
},
|
|
});
|
|
};
|
|
|
|
const renderValue = value => {
|
|
if (value === ROLES.user) {
|
|
return formatMessage(labels.user);
|
|
}
|
|
if (value === ROLES.admin) {
|
|
return formatMessage(labels.admin);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Form onSubmit={handleSubmit} error={error} values={data} style={{ width: 300 }}>
|
|
<FormRow label={formatMessage(labels.username)}>
|
|
<FormInput name="username">
|
|
<TextField />
|
|
</FormInput>
|
|
</FormRow>
|
|
<FormRow label={formatMessage(labels.password)}>
|
|
<FormInput
|
|
name="newPassword"
|
|
rules={{
|
|
minLength: { value: 8, message: formatMessage(messages.minPasswordLength) },
|
|
}}
|
|
>
|
|
<PasswordField autoComplete="new-password" />
|
|
</FormInput>
|
|
</FormRow>
|
|
<FormRow label={formatMessage(labels.role)}>
|
|
<FormInput name="role" rules={{ required: formatMessage(labels.required) }}>
|
|
<Dropdown renderValue={renderValue}>
|
|
<Item key={ROLES.user}>{formatMessage(labels.user)}</Item>
|
|
<Item key={ROLES.admin}>{formatMessage(labels.admin)}</Item>
|
|
</Dropdown>
|
|
</FormInput>
|
|
</FormRow>
|
|
<FormButtons>
|
|
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
|
|
</FormButtons>
|
|
</Form>
|
|
);
|
|
}
|