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

73 lines
2.1 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';
import useApi from 'hooks/useApi';
import { ROLES } from 'lib/constants';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-01-21 02:12:53 +01:00
2023-04-21 17:00:42 +02:00
export 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-03-22 22:05:55 +01:00
const { formatMessage, labels } = useMessages();
2023-01-21 02:12:53 +01:00
const handleSubmit = async data => {
mutate(data, {
onSuccess: async () => {
onSave(data);
onClose();
2023-01-21 02:12:53 +01:00
},
});
};
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>
);
}
2023-04-21 17:00:42 +02:00
export default UserAddForm;