umami/src/app/(main)/settings/websites/WebsiteAddForm.tsx

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-06 06:56:36 +00:00
import {
Form,
FormRow,
FormInput,
FormButtons,
TextField,
Button,
SubmitButton,
} from 'react-basics';
2024-01-29 02:33:40 +00:00
import { useApi } from 'components/hooks';
import { DOMAIN_REGEX } from 'lib/constants';
2024-01-29 02:33:40 +00:00
import { useMessages } from 'components/hooks';
2024-01-29 02:33:40 +00:00
export function WebsiteAddForm({
teamId,
onSave,
onClose,
}: {
2024-02-03 06:48:03 +00:00
teamId?: string;
2024-01-29 02:33:40 +00:00
onSave?: () => void;
onClose?: () => void;
}) {
2023-03-23 18:46:49 +00:00
const { formatMessage, labels, messages } = useMessages();
2023-01-10 07:59:26 +00:00
const { post, useMutation } = useApi();
2023-12-03 11:07:03 +00:00
const { mutate, error, isPending } = useMutation({
2024-01-29 11:15:22 +00:00
mutationFn: (data: any) => post('/websites', { ...data, teamId }),
2023-12-03 11:07:03 +00:00
});
2023-11-13 22:12:05 +00:00
const handleSubmit = async (data: any) => {
mutate(data, {
onSuccess: async () => {
2023-11-13 22:12:05 +00:00
onSave?.();
onClose?.();
},
});
};
return (
2023-12-03 11:07:03 +00:00
<Form onSubmit={handleSubmit} error={error}>
2023-01-25 15:42:46 +00:00
<FormRow label={formatMessage(labels.name)}>
2023-01-23 23:32:35 +00:00
<FormInput name="name" rules={{ required: formatMessage(labels.required) }}>
2023-01-06 06:56:36 +00:00
<TextField autoComplete="off" />
</FormInput>
</FormRow>
2023-01-25 15:42:46 +00:00
<FormRow label={formatMessage(labels.domain)}>
2023-01-06 06:56:36 +00:00
<FormInput
name="domain"
rules={{
2023-01-23 23:32:35 +00:00
required: formatMessage(labels.required),
2023-03-23 18:46:49 +00:00
pattern: { value: DOMAIN_REGEX, message: formatMessage(messages.invalidDomain) },
2023-01-06 06:56:36 +00:00
}}
>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormButtons flex>
<SubmitButton variant="primary" disabled={false}>
2023-01-23 23:32:35 +00:00
{formatMessage(labels.save)}
</SubmitButton>
2023-11-13 22:12:05 +00:00
{onClose && (
2023-12-03 11:07:03 +00:00
<Button disabled={isPending} onClick={onClose}>
2023-11-13 22:12:05 +00:00
{formatMessage(labels.cancel)}
</Button>
)}
</FormButtons>
</Form>
);
}
2023-04-21 15:00:42 +00:00
export default WebsiteAddForm;