diff --git a/components/FeedbackWidget.tsx b/components/FeedbackWidget.tsx index d5f982e..1074b6f 100644 --- a/components/FeedbackWidget.tsx +++ b/components/FeedbackWidget.tsx @@ -4,10 +4,18 @@ import * as React from "react" import useClickOutside from "@/hooks/useClickOutside" import Input from "./atoms/Input" +import Button from "./atoms/Button" -interface Props {} +interface ResponseDataType { + ok: boolean +} + +const defaultFormState = { + name: "", + description: "", +} -const FeedbackWidget: React.FC = () => { +const FeedbackWidget: React.FC = () => { const ref = React.useRef(null) const [isWidgetOpen, setIsWidgetOpen] = React.useState(false) @@ -15,38 +23,77 @@ const FeedbackWidget: React.FC = () => { setIsWidgetOpen(false) }) - const [formState, setFormState] = React.useState({ - animationName: "", - }) + const [formState, setFormState] = React.useState(defaultFormState) + const [isSending, setIsSending] = React.useState(false) + const [isSent, setIsSent] = React.useState(false) - const handleChange = (e: React.ChangeEvent) => { + const handleChange = ( + e: React.ChangeEvent + ) => { setFormState({ ...formState, [e.target.name]: e.target.value, }) } + const handleSubmit = (e: React.FormEvent): void => { + e.preventDefault() + setIsSending(true) + fetch("https://formspree.io/f/xleqvadd", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(formState), + }) + .then(async (response) => await response.json()) + .then((data: ResponseDataType) => { + setIsSending(false) + if (data.ok) { + setIsSent(true) + setTimeout(() => { + setIsSent(false) + setFormState(defaultFormState) + }, 3000) + } + }) + .catch(() => { + setIsSending(false) + }) + } + if (isWidgetOpen) { return (
-

Request Animation

+
+

Request Animation

- + +