umami/src/app/(main)/settings/users/UserAddForm.tsx

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-01-21 01:12:53 +00:00
import {
Dropdown,
Item,
Form,
FormRow,
FormButtons,
FormInput,
TextField,
PasswordField,
SubmitButton,
2023-01-23 23:32:35 +00:00
Button,
2023-01-21 01:12:53 +00:00
} from 'react-basics';
2024-01-29 02:33:40 +00:00
import { useApi } from 'components/hooks';
2023-01-21 01:12:53 +00:00
import { ROLES } from 'lib/constants';
2024-01-29 02:33:40 +00:00
import { useMessages } from 'components/hooks';
2023-01-21 01:12:53 +00:00
2023-04-21 15:00:42 +00:00
export function UserAddForm({ onSave, onClose }) {
2023-01-21 01:12:53 +00:00
const { post, useMutation } = useApi();
2023-12-03 11:07:03 +00:00
const { mutate, error, isPending } = useMutation({
mutationFn: (data: any) => post(`/users`, data),
});
2023-03-22 21:05:55 +00:00
const { formatMessage, labels } = useMessages();
2023-01-21 01:12:53 +00:00
2023-12-06 03:22:14 +00:00
const handleSubmit = async (data: any) => {
2023-01-21 01:12:53 +00:00
mutate(data, {
onSuccess: async () => {
onSave(data);
onClose();
2023-01-21 01:12:53 +00:00
},
});
};
2023-01-23 23:32:35 +00:00
const renderValue = value => {
if (value === ROLES.user) {
2023-01-25 15:42:46 +00:00
return formatMessage(labels.user);
2023-01-23 23:32:35 +00:00
}
if (value === ROLES.admin) {
2023-01-25 15:42:46 +00:00
return formatMessage(labels.admin);
2023-01-23 23:32:35 +00:00
}
2023-07-07 06:02:16 +00:00
if (value === ROLES.viewOnly) {
return formatMessage(labels.viewOnly);
}
2023-01-23 23:32:35 +00:00
};
2023-01-21 01:12:53 +00:00
return (
<Form onSubmit={handleSubmit} error={error}>
2023-01-25 15:42:46 +00:00
<FormRow label={formatMessage(labels.username)}>
2023-01-23 23:32:35 +00:00
<FormInput name="username" rules={{ required: formatMessage(labels.required) }}>
<TextField autoComplete="new-username" />
2023-01-21 01:12:53 +00:00
</FormInput>
</FormRow>
2023-01-25 15:42:46 +00:00
<FormRow label={formatMessage(labels.password)}>
2023-01-23 23:32:35 +00:00
<FormInput name="password" rules={{ required: formatMessage(labels.required) }}>
<PasswordField autoComplete="new-password" />
2023-01-21 01:12:53 +00:00
</FormInput>
</FormRow>
2023-01-25 15:42:46 +00:00
<FormRow label={formatMessage(labels.role)}>
2023-01-23 23:32:35 +00:00
<FormInput name="role" rules={{ required: formatMessage(labels.required) }}>
2023-01-25 15:42:46 +00:00
<Dropdown renderValue={renderValue}>
2023-07-07 06:02:16 +00:00
<Item key={ROLES.viewOnly}>{formatMessage(labels.viewOnly)}</Item>
2023-01-25 15:42:46 +00:00
<Item key={ROLES.user}>{formatMessage(labels.user)}</Item>
<Item key={ROLES.admin}>{formatMessage(labels.admin)}</Item>
2023-01-21 01:12:53 +00:00
</Dropdown>
</FormInput>
</FormRow>
2023-01-23 23:32:35 +00:00
<FormButtons flex>
<SubmitButton variant="primary" disabled={false}>
{formatMessage(labels.save)}
</SubmitButton>
2023-12-03 11:07:03 +00:00
<Button disabled={isPending} onClick={onClose}>
2023-01-23 23:32:35 +00:00
{formatMessage(labels.cancel)}
</Button>
2023-01-21 01:12:53 +00:00
</FormButtons>
</Form>
);
}
2023-04-21 15:00:42 +00:00
export default UserAddForm;