umami/components/pages/settings/users/UserAddForm.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-01-21 02:12:53 +01:00
import {
Dropdown,
Item,
Form,
FormRow,
FormButtons,
FormInput,
TextField,
PasswordField,
SubmitButton,
2023-01-24 00:32:35 +01:00
Button,
2023-01-21 02:12:53 +01:00
} from 'react-basics';
2023-01-25 16:42:46 +01:00
import { useIntl } from 'react-intl';
2023-01-21 02:12:53 +01:00
import useApi from 'hooks/useApi';
import { ROLES } from 'lib/constants';
2023-01-24 00:32:35 +01:00
import { labels } from 'components/messages';
2023-01-21 02:12:53 +01:00
2023-01-24 00:32:35 +01:00
export default function UserAddForm({ onSave, onClose }) {
2023-01-21 02:12:53 +01:00
const { post, useMutation } = useApi();
2023-01-24 00:32:35 +01:00
const { mutate, error, isLoading } = useMutation(data => post(`/users`, data));
2023-01-21 02:12:53 +01:00
const { formatMessage } = useIntl();
const handleSubmit = async data => {
mutate(data, {
onSuccess: async () => {
onSave(data);
},
});
};
2023-01-24 00:32:35 +01:00
const renderValue = value => {
if (value === ROLES.user) {
2023-01-25 16:42:46 +01:00
return formatMessage(labels.user);
2023-01-24 00:32:35 +01:00
}
if (value === ROLES.admin) {
2023-01-25 16:42:46 +01:00
return formatMessage(labels.admin);
2023-01-24 00:32:35 +01:00
}
};
2023-01-21 02:12:53 +01:00
return (
<Form onSubmit={handleSubmit} error={error}>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.username)}>
2023-01-24 00:32:35 +01:00
<FormInput name="username" rules={{ required: formatMessage(labels.required) }}>
<TextField autoComplete="new-username" />
2023-01-21 02:12:53 +01:00
</FormInput>
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.password)}>
2023-01-24 00:32:35 +01:00
<FormInput name="password" rules={{ required: formatMessage(labels.required) }}>
<PasswordField autoComplete="new-password" />
2023-01-21 02:12:53 +01:00
</FormInput>
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.role)}>
2023-01-24 00:32:35 +01:00
<FormInput name="role" rules={{ required: formatMessage(labels.required) }}>
2023-01-25 16:42:46 +01:00
<Dropdown renderValue={renderValue}>
<Item key={ROLES.user}>{formatMessage(labels.user)}</Item>
<Item key={ROLES.admin}>{formatMessage(labels.admin)}</Item>
2023-01-21 02:12:53 +01:00
</Dropdown>
</FormInput>
</FormRow>
2023-01-24 00:32:35 +01:00
<FormButtons flex>
<SubmitButton variant="primary" disabled={false}>
{formatMessage(labels.save)}
</SubmitButton>
<Button disabled={isLoading} onClick={onClose}>
{formatMessage(labels.cancel)}
</Button>
2023-01-21 02:12:53 +01:00
</FormButtons>
</Form>
);
}