2022-12-27 01:57:59 +01:00
|
|
|
import { SubmitButton, Form, FormInput, FormRow, FormButtons, TextField } from 'react-basics';
|
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
import { useRef } from 'react';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2020-08-29 06:34:20 +02:00
|
|
|
import { DOMAIN_REGEX } from 'lib/constants';
|
2020-08-07 11:27:12 +02:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
export default function WebsiteEditForm({ websiteId, data, onSave }) {
|
2022-12-28 06:38:23 +01:00
|
|
|
const { post } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
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 (
|
2022-12-27 01:57:59 +01:00
|
|
|
<Form ref={ref} onSubmit={handleSubmit} error={error} values={data}>
|
|
|
|
<FormRow label="Website ID">
|
|
|
|
<TextField value={websiteId} readOnly allowCopy />
|
|
|
|
</FormRow>
|
|
|
|
<FormInput name="name" label="Name" rules={{ required: 'Required' }}>
|
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
<FormInput
|
|
|
|
name="domain"
|
|
|
|
label="Domain"
|
|
|
|
rules={{
|
|
|
|
required: 'Required',
|
|
|
|
pattern: {
|
|
|
|
value: DOMAIN_REGEX,
|
|
|
|
message: 'Invalid domain',
|
|
|
|
},
|
|
|
|
}}
|
2020-08-09 08:48:43 +02:00
|
|
|
>
|
2022-12-27 01:57:59 +01:00
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
<FormButtons>
|
|
|
|
<SubmitButton variant="primary">Save</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
2020-08-07 11:27:12 +02:00
|
|
|
);
|
|
|
|
}
|