2022-12-27 00:57:59 +00:00
|
|
|
import { SubmitButton, Form, FormInput, FormRow, FormButtons, TextField } from 'react-basics';
|
2023-12-06 03:22:14 +00:00
|
|
|
import { useContext, useRef } from 'react';
|
2024-01-29 02:33:40 +00:00
|
|
|
import { useApi } from 'components/hooks';
|
2020-08-29 04:34:20 +00:00
|
|
|
import { DOMAIN_REGEX } from 'lib/constants';
|
2024-01-29 02:33:40 +00:00
|
|
|
import { useMessages } from 'components/hooks';
|
2023-12-06 03:22:14 +00:00
|
|
|
import SettingsContext from '../../SettingsContext';
|
2020-08-07 09:27:12 +00:00
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
export function WebsiteEditForm({
|
|
|
|
|
websiteId,
|
|
|
|
|
data,
|
|
|
|
|
onSave,
|
|
|
|
|
}: {
|
|
|
|
|
websiteId: string;
|
|
|
|
|
data: any[];
|
|
|
|
|
onSave?: (data: any) => void;
|
|
|
|
|
}) {
|
2023-03-22 21:05:55 +00:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2023-12-06 03:22:14 +00:00
|
|
|
const { websitesUrl } = useContext(SettingsContext);
|
2023-01-10 07:59:26 +00:00
|
|
|
const { post, useMutation } = useApi();
|
2023-12-03 11:07:03 +00:00
|
|
|
const { mutate, error } = useMutation({
|
2023-12-06 03:22:14 +00:00
|
|
|
mutationFn: (data: any) => post(`${websitesUrl}/${websiteId}`, data),
|
2023-12-03 11:07:03 +00:00
|
|
|
});
|
2022-12-27 00:57:59 +00:00
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
2023-12-06 03:22:14 +00:00
|
|
|
const handleSubmit = async (data: any) => {
|
2022-12-27 00:57:59 +00:00
|
|
|
mutate(data, {
|
|
|
|
|
onSuccess: async () => {
|
|
|
|
|
ref.current.reset(data);
|
|
|
|
|
onSave(data);
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-08-07 09:27:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2023-04-13 00:43:08 +00:00
|
|
|
<Form ref={ref} onSubmit={handleSubmit} error={error} values={data}>
|
2023-01-25 15:42:46 +00:00
|
|
|
<FormRow label={formatMessage(labels.websiteId)}>
|
2022-12-27 00:57:59 +00:00
|
|
|
<TextField value={websiteId} readOnly allowCopy />
|
|
|
|
|
</FormRow>
|
2023-01-25 15:42:46 +00:00
|
|
|
<FormRow label={formatMessage(labels.name)}>
|
|
|
|
|
<FormInput name="name" rules={{ required: formatMessage(labels.required) }}>
|
2023-01-06 06:56:36 +00:00
|
|
|
<TextField />
|
|
|
|
|
</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-25 15:42:46 +00:00
|
|
|
required: formatMessage(labels.required),
|
2023-01-06 06:56:36 +00:00
|
|
|
pattern: {
|
|
|
|
|
value: DOMAIN_REGEX,
|
2023-01-25 15:42:46 +00:00
|
|
|
message: formatMessage(messages.invalidDomain),
|
2023-01-06 06:56:36 +00:00
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<TextField />
|
|
|
|
|
</FormInput>
|
|
|
|
|
</FormRow>
|
2022-12-27 00:57:59 +00:00
|
|
|
<FormButtons>
|
2023-01-25 15:42:46 +00:00
|
|
|
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
|
2022-12-27 00:57:59 +00:00
|
|
|
</FormButtons>
|
|
|
|
|
</Form>
|
2020-08-07 09:27:12 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-04-21 15:00:42 +00:00
|
|
|
|
|
|
|
|
export default WebsiteEditForm;
|