import React, {useState} from 'react'; import {postJson} from '../api'; import {AuthFooter, CheckField, FormErrors, TextField, fieldErrors} from './AuthFields'; import Modal from './Modal'; import useRecaptcha from './useRecaptcha'; const FIELDS = ['login_name', 'email', 'password', 'password_confirmation', 'tos_agreement']; const FAILED = {byField: {}, other: ['Something went wrong joining. Please try again.']}; // Joining, as a dialog over the page the link was on. The same fields and // checks as the sign up page: it posts to Devise's registrations#create as // JSON, shows the server's errors against their fields, and on success goes to // wherever the server would have sent the new member. export default function SignUpModal({urls, recaptchaSiteKey, siteName, onClose, onSwitch}) { const [values, setValues] = useState({login_name: '', email: '', password: '', password_confirmation: '', tos_agreement: false, newsletter: true}); const [saving, setSaving] = useState(false); const [errors, setErrors] = useState({byField: {}, other: []}); const captcha = useRecaptcha(recaptchaSiteKey); const set = (name) => (event) => setValues({...values, [name]: event.target.type === 'checkbox' ? event.target.checked : event.target.value}); async function submit(event) { event.preventDefault(); setSaving(true); setErrors({byField: {}, other: []}); try { const {ok, status, data} = await postJson(urls.sign_up, {member: values, 'g-recaptcha-response': captcha.response()}); if (ok && data && data.location) { setTimeout(() => { window.location.href = data.location; }, 0); return; } setErrors(status === 422 ? fieldErrors(data, FIELDS) : FAILED); } catch (error) { setErrors(FAILED); } captcha.reset(); setSaving(false); } const on = errors.byField; return (

Sign up to track your vegetable garden and connect with other local growers.

I agree to the Terms of Service} checked={values.tos_agreement} onChange={set('tos_agreement')} errors={on.tos_agreement} />
{recaptchaSiteKey ?
: null}
{recaptchaSiteKey ?

If you have accessibility issues with the captcha, please contact us via the links in the footer and we will help.

: null}
); }