From afe0397a9b5b4a5c7a0714641d20dbdf4d9d6760 Mon Sep 17 00:00:00 2001 From: Nathan Wang Date: Mon, 13 Jul 2020 22:49:38 -0700 Subject: [PATCH] wrap up contact slideover; closes #41 --- src/components/ContactUsSlideover.tsx | 222 ++++++++++++++++ src/components/ModuleLayout/ModuleLayout.tsx | 64 +---- src/components/ReportIssueSlideover.tsx | 241 ------------------ .../{Slideover.tsx => SlideoverForm.tsx} | 11 +- src/hooks/useStickyState.tsx | 14 + 5 files changed, 254 insertions(+), 298 deletions(-) create mode 100644 src/components/ContactUsSlideover.tsx delete mode 100644 src/components/ReportIssueSlideover.tsx rename src/components/Slideover/{Slideover.tsx => SlideoverForm.tsx} (90%) create mode 100644 src/hooks/useStickyState.tsx diff --git a/src/components/ContactUsSlideover.tsx b/src/components/ContactUsSlideover.tsx new file mode 100644 index 0000000..e36c03d --- /dev/null +++ b/src/components/ContactUsSlideover.tsx @@ -0,0 +1,222 @@ +import * as React from 'react'; +import { ModuleInfo } from '../module'; +import { divisionLabels } from '../../content/ordering'; +import SlideoverForm from './Slideover/SlideoverForm'; +import { useState } from 'react'; +import useStickyState from '../hooks/useStickyState'; + +const Field = ({ label, id, value, onChange }) => { + return ( +
+ +
+ +
+
+ ); +}; + +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 = [ + 'Typo', + 'Broken Link', + 'Unclear Explanation', + 'Suggestion', + 'Website Bug', + 'Other', + ]; + const [message, setMessage] = useStickyState('', 'contact_form_message'); + const [showSuccess, setShowSuccess] = useState(false); + const [submitEnabled, setSubmitEnabled] = useState(true); + + React.useEffect(() => { + if (activeModule) + setLocation( + `${activeModule.title} - ${divisionLabels[activeModule.division]}` + ); + else setLocation(''); + }, [activeModule]); + + React.useEffect(() => { + if (isOpen) { + setShowSuccess(false); + setSubmitEnabled(true); + } + }, [isOpen]); + + const handleSubmit = async e => { + e.preventDefault(); + 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); + } + }; + + return ( + + + + + + + + + } + onSubmit={handleSubmit} + > +
+ {showSuccess && ( + <> +

+ We've received your message! 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)} + /> + setEmail(e.target.value)} + /> + setLocation(e.target.value)} + /> +
+ + Topic + +
+ {topics.map((t, idx) => ( +
+
+
+ setTopic(t)} + /> +
+
+ +
+
+
+ ))} +
+
+
+ +
+