From 6cddfdf8fe9bccc291ee8625d42cb42fd4ce2134 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Mon, 8 May 2023 17:28:42 -0600 Subject: [PATCH] implemented contact form --- app/contact/page.tsx | 91 ++++++++++++++++++++++++++++++++++++++++++ styles/form.module.css | 34 ++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 app/contact/page.tsx create mode 100644 styles/form.module.css diff --git a/app/contact/page.tsx b/app/contact/page.tsx new file mode 100644 index 0000000..1ed2eb8 --- /dev/null +++ b/app/contact/page.tsx @@ -0,0 +1,91 @@ +'use client' +import { useState, useRef, FormEvent } from 'react' +import InfoBar from "~/components/InfoBar/" + +import styles from '~/styles/form.module.css' + +const submitUrl = 'https://kae.tempest.dev/api/contact/me' + +export default function Contact() { + const [submitting, setSubmitting] = useState(false) + const [status, setStatus] = useState('') + + const nameRef = useRef() + const emailRef = useRef() + const messageRef = useRef() + + const submit = async (ev: FormEvent) => { + ev.preventDefault() + + setStatus('') + + const name: string = nameRef.current?.value ?? '' + const email: string = emailRef.current?.value ?? '' + const message: string = messageRef.current?.value ?? '' + + if (!name) setStatus(s => s + ' Name required.') + if (!email) setStatus(s => s + ' Email required.') + if (!message) setStatus(s => s + ' Message required.') + + if (!name || !email || !message) + return + + setSubmitting(true) + + try { + + await fetch(submitUrl, { + method: 'post', + headers: { + 'content-type': 'application/json' + }, + body: JSON.stringify({ + name, email, message + }) + }) + + setStatus('Message sent successfully') + } catch { + setStatus('Error submitting message, please try again') + } finally { + setSubmitting(false) + } + } + + return ( + <> +

+ Contact +

+
+ + Be nice. Please don't make us regret putting this here + +
+ + + + + + + +