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

52 lines
1.6 KiB
JavaScript
Raw Normal View History

import { SubmitButton, Form, FormInput, FormRow, FormButtons, TextField } from 'react-basics';
import { useRef } from 'react';
2022-12-28 05:20:44 +01:00
import useApi from 'hooks/useApi';
import { DOMAIN_REGEX } from 'lib/constants';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2020-08-07 11:27:12 +02:00
export default function WebsiteEditForm({ websiteId, data, onSave }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
2023-01-10 08:59:26 +01:00
const { post, useMutation } = useApi();
const { mutate, error } = useMutation(data => post(`/websites/${websiteId}`, data));
const ref = useRef(null);
const handleSubmit = async data => {
mutate(data, {
onSuccess: async () => {
ref.current.reset(data);
onSave(data);
},
});
2020-08-07 11:27:12 +02:00
};
return (
2023-01-21 02:12:53 +01:00
<Form ref={ref} onSubmit={handleSubmit} error={error} values={data} style={{ width: 600 }}>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.websiteId)}>
<TextField value={websiteId} readOnly allowCopy />
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.name)}>
<FormInput name="name" rules={{ required: formatMessage(labels.required) }}>
2023-01-06 07:56:36 +01:00
<TextField />
</FormInput>
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.domain)}>
2023-01-06 07:56:36 +01:00
<FormInput
name="domain"
rules={{
2023-01-25 16:42:46 +01:00
required: formatMessage(labels.required),
2023-01-06 07:56:36 +01:00
pattern: {
value: DOMAIN_REGEX,
2023-01-25 16:42:46 +01:00
message: formatMessage(messages.invalidDomain),
2023-01-06 07:56:36 +01:00
},
}}
>
<TextField />
</FormInput>
</FormRow>
<FormButtons>
2023-01-25 16:42:46 +01:00
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
</FormButtons>
</Form>
2020-08-07 11:27:12 +02:00
);
}