|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import Notification from "../ui/notification"; |
| 3 | +import classes from "./contact-form.module.css"; |
| 4 | + |
| 5 | +const sendContactData = async (contactDetails) => { |
| 6 | + const response = await fetch("/api/contact", { |
| 7 | + method: "POST", |
| 8 | + body: JSON.stringify(contactDetails), |
| 9 | + headers: { |
| 10 | + "Content-Type": "application/json", |
| 11 | + }, |
| 12 | + }); |
| 13 | + |
| 14 | + const data = await response.json(); |
| 15 | + |
| 16 | + if (!response.ok) { |
| 17 | + throw new Error(data.message || "Something went wrong!"); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +const ContactForm = (props) => { |
| 22 | + const [enteredEmail, setEnteredEmail] = useState(""); |
| 23 | + const [enteredName, setEnteredName] = useState(""); |
| 24 | + const [enteredMessage, setEnteredMessage] = useState(""); |
| 25 | + |
| 26 | + const [reqStatus, setReqStatus] = useState(); // "pending" | "success" | " error" |
| 27 | + const [reqError, setReqError] = useState(); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (reqStatus === "success" || reqStatus === "error") { |
| 31 | + const timer = setTimeout(() => { |
| 32 | + setReqStatus(null); |
| 33 | + setReqError(null); |
| 34 | + }, 3000); |
| 35 | + |
| 36 | + return () => clearTimeout(timer); |
| 37 | + } |
| 38 | + }, [reqStatus]); |
| 39 | + |
| 40 | + const sendMessageHandler = async (event) => { |
| 41 | + event.preventDefault(); |
| 42 | + |
| 43 | + setReqStatus("pending"); |
| 44 | + |
| 45 | + try { |
| 46 | + await sendContactData({ |
| 47 | + email: enteredEmail, |
| 48 | + name: enteredName, |
| 49 | + message: enteredMessage, |
| 50 | + }); |
| 51 | + |
| 52 | + setEnteredMessage(""); |
| 53 | + setEnteredEmail(""); |
| 54 | + setEnteredName(""); |
| 55 | + |
| 56 | + setReqStatus("success"); |
| 57 | + } catch (error) { |
| 58 | + console.log(error); |
| 59 | + setReqStatus("error"); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + let notification; |
| 64 | + |
| 65 | + if (reqStatus === "pending") { |
| 66 | + notification = { |
| 67 | + status: "pending", |
| 68 | + title: "Sending message...", |
| 69 | + message: "Your message is on its way!", |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + if (reqStatus === "success") { |
| 74 | + notification = { |
| 75 | + status: "success", |
| 76 | + title: "Success!", |
| 77 | + message: "Message sent successfully!", |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + if (reqStatus === "error") { |
| 82 | + notification = { |
| 83 | + status: "error", |
| 84 | + title: "Error!", |
| 85 | + message: reqError, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <section className={classes.contact}> |
| 91 | + <h1>How can I help you?</h1> |
| 92 | + <form className={classes.form} onSubmit={sendMessageHandler}> |
| 93 | + <div className={classes.controls}> |
| 94 | + <div className={classes.control}> |
| 95 | + <label htmlFor="email">Your Email</label> |
| 96 | + <input |
| 97 | + type="email" |
| 98 | + id="email" |
| 99 | + required |
| 100 | + value={enteredEmail} |
| 101 | + onChange={(event) => setEnteredEmail(event.target.value)} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + <div className={classes.control}> |
| 105 | + <label htmlFor="name">Your Name</label> |
| 106 | + <input |
| 107 | + type="text" |
| 108 | + id="name" |
| 109 | + required |
| 110 | + value={enteredName} |
| 111 | + onChange={(event) => setEnteredName(event.target.value)} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <div className={classes.control}> |
| 116 | + <label htmlFor="message">Your Message</label> |
| 117 | + <textarea |
| 118 | + id="message" |
| 119 | + rows="5" |
| 120 | + required |
| 121 | + value={enteredMessage} |
| 122 | + onChange={(event) => setEnteredMessage(event.target.value)} |
| 123 | + ></textarea> |
| 124 | + </div> |
| 125 | + |
| 126 | + <div className={classes.actions}> |
| 127 | + <button>Send Message</button> |
| 128 | + </div> |
| 129 | + </form> |
| 130 | + |
| 131 | + {notification && ( |
| 132 | + <Notification |
| 133 | + status={notification.status} |
| 134 | + title={notification.title} |
| 135 | + message={notification.message} |
| 136 | + /> |
| 137 | + )} |
| 138 | + </section> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default ContactForm; |
0 commit comments