umami/components/pages/settings/websites/WebsiteAddForm.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-01-06 07:56:36 +01:00
import {
Form,
FormRow,
FormInput,
FormButtons,
TextField,
Button,
SubmitButton,
} from 'react-basics';
2023-01-24 00:32:35 +01:00
import { defineMessages, useIntl } from 'react-intl';
2022-12-28 05:20:44 +01:00
import useApi from 'hooks/useApi';
import { DOMAIN_REGEX } from 'lib/constants';
2023-01-24 00:32:35 +01:00
import { labels } from 'components/messages';
const messages = defineMessages({
invalidDomain: { id: 'label.invalid-domain', defaultMessage: 'Invalid domain' },
});
export default function WebsiteAddForm({ onSave, onClose }) {
2023-01-24 00:32:35 +01:00
const { formatMessage } = useIntl();
2023-01-10 08:59:26 +01:00
const { post, useMutation } = useApi();
const { mutate, error, isLoading } = useMutation(data => post('/websites', data));
const handleSubmit = async data => {
mutate(data, {
onSuccess: async () => {
onSave();
},
});
};
return (
2023-01-24 00:32:35 +01:00
<Form onSubmit={handleSubmit} error={error}>
2023-01-06 07:56:36 +01:00
<FormRow label="Name">
2023-01-24 00:32:35 +01:00
<FormInput name="name" rules={{ required: formatMessage(labels.required) }}>
2023-01-06 07:56:36 +01:00
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormRow label="Domain">
<FormInput
name="domain"
rules={{
2023-01-24 00:32:35 +01:00
required: formatMessage(labels.required),
pattern: { value: DOMAIN_REGEX, message: formatMessage(messages.invalidDomain) },
2023-01-06 07:56:36 +01:00
}}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormButtons flex>
<SubmitButton variant="primary" disabled={false}>
2023-01-24 00:32:35 +01:00
{formatMessage(labels.save)}
</SubmitButton>
<Button disabled={isLoading} onClick={onClose}>
2023-01-24 00:32:35 +01:00
{formatMessage(labels.cancel)}
</Button>
</FormButtons>
</Form>
);
}