import * as React from 'react'; import { ModuleInfo } from '../../models/module'; import { SECTION_LABELS } from '../../../content/ordering'; import SlideoverForm from './SlideoverForm'; import { useState } from 'react'; import useStickyState from '../../hooks/useStickyState'; // Warning: this file is insanely messy. This should be rewritten soon :) const Field = ({ label, id, value, onChange, errorMsg = null }) => { return (
{errorMsg && (
)}
{errorMsg &&

{errorMsg}

}
); }; function validateEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase()); } export default function ContactUsSlideover({ isOpen, onClose, activeModule, }: { isOpen: boolean; onClose: any; activeModule: ModuleInfo; }) { const [name, setName] = useStickyState('', 'contact_form_name'); const [email, setEmail] = useStickyState('', 'contact_form_email'); const [location, setLocation] = useState(''); const [topic, setTopic] = useStickyState('', 'contact_form_topic'); const topics = [ 'Unclear Explanation', 'Problem Editorial Request', 'Typo / Broken Link', 'Suggestion', 'Website Bug', 'Other', ]; const [message, setMessage] = useStickyState('', 'contact_form_message'); const [showSuccess, setShowSuccess] = useState(false); const [submitEnabled, setSubmitEnabled] = useState(true); const [showErrors, setShowErrors] = useState(false); React.useEffect(() => { if (activeModule) setLocation( `${activeModule.title} - ${SECTION_LABELS[activeModule.section]}` ); else setLocation(''); }, [activeModule]); React.useEffect(() => { if (isOpen) { setShowSuccess(false); setShowErrors(false); setSubmitEnabled(true); } }, [isOpen]); const handleSubmit = async e => { e.preventDefault(); setShowErrors(true); if ( name === '' || email === '' || !validateEmail(email) || message === '' ) { return; } let data = new FormData(); data.append('name', name); data.append('email', email); data.append('location', location); data.append('topic', topic); data.append('message', message); setSubmitEnabled(false); try { await fetch( 'https://formsubmit.co/ajax/da14a047686367521c3c8c5a79b03c97', { method: 'POST', mode: 'no-cors', body: data, } ); setTopic(''); setMessage(''); setShowSuccess(true); } catch (e) { setSubmitEnabled(true); alert('Form submission failed: ' + e.message); } finally { setShowErrors(false); } }; return ( } onSubmit={handleSubmit} >
{showSuccess && (

Message received!

We will try our best to respond (if one is needed) within a week.

For urgent requests, please feel free to email{' '} nathan.r.wang@gmail.com .

)} {!showSuccess && (
setName(e.target.value)} errorMsg={ showErrors && name === '' ? 'This field is required.' : null } /> setEmail(e.target.value)} errorMsg={ showErrors ? email === '' ? 'This field is required.' : !validateEmail(email) ? 'Please enter a valid email address.' : null : null } /> setLocation(e.target.value)} />
Topic
{topics.map((t, idx) => (
setTopic(t)} />
))}