import React, { useEffect, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { Formik, Form, Field, useFormikContext } from 'formik'; import Button from 'components/common/Button'; import FormLayout, { FormButtons, FormError, FormMessage, FormRow, } from 'components/layout/FormLayout'; import Checkbox from 'components/common/Checkbox'; import { DOMAIN_REGEX } from 'lib/constants'; import useApi from 'hooks/useApi'; import useFetch from 'hooks/useFetch'; import useUser from 'hooks/useUser'; import styles from './WebsiteEditForm.module.css'; const initialValues = { name: '', domain: '', owner: '', public: false, }; const validate = ({ name, domain }) => { const errors = {}; if (!name) { errors.name = ; } if (!domain) { errors.domain = ; } else if (!DOMAIN_REGEX.test(domain)) { errors.domain = ; } return errors; }; const OwnerDropDown = ({ user, accounts }) => { const { setFieldValue, values } = useFormikContext(); useEffect(() => { if (values.userId != null && values.owner === '') { setFieldValue('owner', values.userId.toString()); } else if (user?.id && values.owner === '') { setFieldValue('owner', user.id.toString()); } }, [accounts, setFieldValue, user, values]); if (user?.isAdmin) { return (
{accounts?.map(acc => ( ))}
); } else { return null; } }; export default function WebsiteEditForm({ values, onSave, onClose }) { const { post } = useApi(); const { data: accounts } = useFetch(`/accounts`); const { user } = useUser(); const [message, setMessage] = useState(); const handleSubmit = async values => { const { id: websiteId } = values; const { ok, data } = await post(websiteId ? `/websites/${websiteId}` : '/websites', values); if (ok) { onSave(); } else { setMessage( data || , ); } }; return ( {() => (
{message} )}
); }