umami/components/forms/WebsiteEditForm.js

111 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-08-07 11:27:12 +02:00
import React, { useState } from 'react';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
2020-08-07 11:27:12 +02:00
import { Formik, Form, Field } from 'formik';
import { post } from 'lib/web';
2020-08-08 05:36:57 +02:00
import Button from 'components/common/Button';
2020-08-07 11:27:12 +02:00
import FormLayout, {
FormButtons,
FormError,
FormMessage,
FormRow,
} from 'components/layout/FormLayout';
import Checkbox from 'components/common/Checkbox';
import { DOMAIN_REGEX } from 'lib/constants';
2020-10-01 07:34:16 +02:00
import { useRouter } from 'next/router';
2020-08-07 11:27:12 +02:00
2020-08-09 08:48:43 +02:00
const initialValues = {
name: '',
domain: '',
2020-08-15 10:17:15 +02:00
public: false,
2020-08-09 08:48:43 +02:00
};
2020-08-07 11:27:12 +02:00
const validate = ({ name, domain }) => {
const errors = {};
if (!name) {
2020-09-06 02:27:01 +02:00
errors.name = <FormattedMessage id="label.required" defaultMessage="Required" />;
2020-08-07 11:27:12 +02:00
}
if (!domain) {
2020-09-06 02:27:01 +02:00
errors.domain = <FormattedMessage id="label.required" defaultMessage="Required" />;
} else if (!DOMAIN_REGEX.test(domain)) {
2020-09-06 02:27:01 +02:00
errors.domain = <FormattedMessage id="label.invalid-domain" defaultMessage="Invalid domain" />;
2020-08-07 11:27:12 +02:00
}
return errors;
};
2020-08-08 05:36:57 +02:00
export default function WebsiteEditForm({ values, onSave, onClose }) {
2020-10-01 07:34:16 +02:00
const { basePath } = useRouter();
2020-08-07 11:27:12 +02:00
const [message, setMessage] = useState();
const handleSubmit = async values => {
2020-10-01 07:34:16 +02:00
const { ok, data } = await post(`${basePath}/api/website`, values);
2020-08-07 11:27:12 +02:00
2020-10-01 00:14:44 +02:00
if (ok) {
2020-08-07 11:27:12 +02:00
onSave();
} else {
2020-10-01 00:14:44 +02:00
setMessage(
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
2020-08-07 11:27:12 +02:00
}
};
return (
<FormLayout>
2020-08-09 08:48:43 +02:00
<Formik
2020-08-17 06:28:54 +02:00
initialValues={{ ...initialValues, ...values, enable_share_url: !!values?.share_id }}
2020-08-09 08:48:43 +02:00
validate={validate}
onSubmit={handleSubmit}
>
2020-08-07 11:27:12 +02:00
{() => (
<Form>
<FormRow>
2020-09-06 02:27:01 +02:00
<label htmlFor="name">
<FormattedMessage id="label.name" defaultMessage="Name" />
</label>
<div>
<Field name="name" type="text" />
<FormError name="name" />
</div>
2020-08-07 11:27:12 +02:00
</FormRow>
<FormRow>
2020-09-06 02:27:01 +02:00
<label htmlFor="domain">
<FormattedMessage id="label.domain" defaultMessage="Domain" />
</label>
<div>
<Field name="domain" type="text" />
<FormError name="domain" />
</div>
2020-08-07 11:27:12 +02:00
</FormRow>
2020-08-15 10:17:15 +02:00
<FormRow>
<label></label>
2020-08-17 06:28:54 +02:00
<Field name="enable_share_url">
2020-09-06 02:27:01 +02:00
{({ field }) => (
<Checkbox
{...field}
label={
<FormattedMessage
id="label.enable-share-url"
defaultMessage="Enable share URL"
/>
}
/>
)}
2020-08-15 10:17:15 +02:00
</Field>
</FormRow>
2020-08-07 11:27:12 +02:00
<FormButtons>
<Button type="submit" variant="action">
2020-10-13 07:53:59 +02:00
<FormattedMessage id="label.save" defaultMessage="Save" />
2020-09-06 02:27:01 +02:00
</Button>
<Button onClick={onClose}>
2020-10-13 07:53:59 +02:00
<FormattedMessage id="label.cancel" defaultMessage="Cancel" />
</Button>
2020-08-07 11:27:12 +02:00
</FormButtons>
<FormMessage>{message}</FormMessage>
</Form>
)}
</Formik>
</FormLayout>
);
}