|
| 1 | +import React, { useState } from "react"; |
| 2 | +import HomeLayout from "../Layouts/HomeLayout"; |
| 3 | +import toast from "react-hot-toast"; |
| 4 | +import axiosInstance from "../Helpers/axiosinstance"; |
| 5 | + |
| 6 | +const Contect = () => { |
| 7 | + const [contectFrom, setContectForm] = useState({ |
| 8 | + name: "", |
| 9 | + email: "", |
| 10 | + message: "", |
| 11 | + }); |
| 12 | + |
| 13 | + const handleInputs = (event) => { |
| 14 | + event.preventDefault(); |
| 15 | + const { name, value } = event.target; |
| 16 | + |
| 17 | + setContectForm({ |
| 18 | + ...contectFrom, |
| 19 | + [name]: value, |
| 20 | + }); |
| 21 | + }; |
| 22 | + |
| 23 | + const onSubmitForm = async (e) => { |
| 24 | + e.preventDefault(); |
| 25 | + |
| 26 | + if (!contectFrom.name || !contectFrom.email || !contectFrom.message) { |
| 27 | + toast.error("All fields are required"); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if ( |
| 32 | + !contectFrom.email.match( |
| 33 | + /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ |
| 34 | + ) |
| 35 | + ) { |
| 36 | + toast.error("Invalid email address"); |
| 37 | + return; |
| 38 | + } |
| 39 | + try { |
| 40 | + const responce = axiosInstance.post("/contact", contectFrom); |
| 41 | + toast.promise(responce, { |
| 42 | + loading: "Submitting your message...", |
| 43 | + success: "Form submitted successfully", |
| 44 | + error: "Failed to submit the form", |
| 45 | + }); |
| 46 | + const contectresponce = await responce; |
| 47 | + console.log(contectresponce); |
| 48 | + if (contectresponce?.data?.success) { |
| 49 | + setContectForm({ |
| 50 | + ...contectFrom, |
| 51 | + name: "", |
| 52 | + email: "", |
| 53 | + message: "", |
| 54 | + }); |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + toast.error(error.message); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <div> |
| 63 | + <HomeLayout> |
| 64 | + <div className="flex items-center justify-center h-[100vh]"> |
| 65 | + <form |
| 66 | + onSubmit={onSubmitForm} |
| 67 | + className="flex flex-col items-center justify-center gap-2 p-5 rounded-md text-white shadow-[0_0_10px_black] w-[22rem]" |
| 68 | + > |
| 69 | + <h1 className="text-3xl font-semibold">Contact Form</h1> |
| 70 | + |
| 71 | + <div className="flex flex-col w-full gap-1"> |
| 72 | + <label htmlFor="name" className="text-xl font-semibold"> |
| 73 | + Name |
| 74 | + </label> |
| 75 | + <input |
| 76 | + onChange={handleInputs} |
| 77 | + className="bg-transparent border px-2 py-1 rounded-sm" |
| 78 | + id="name" |
| 79 | + type="text" |
| 80 | + name="name" |
| 81 | + value={contectFrom.name} |
| 82 | + placeholder="Enter your name" |
| 83 | + /> |
| 84 | + </div> |
| 85 | + |
| 86 | + <div className="flex flex-col w-full gap-1"> |
| 87 | + <label htmlFor="email" className="text-xl font-semibold"> |
| 88 | + Email |
| 89 | + </label> |
| 90 | + <input |
| 91 | + onChange={handleInputs} |
| 92 | + className="bg-transparent border px-2 py-1 rounded-sm" |
| 93 | + id="email" |
| 94 | + type="email" |
| 95 | + name="email" |
| 96 | + value={contectFrom.email} |
| 97 | + placeholder="Enter your email" |
| 98 | + /> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="flex flex-col w-full gap-1"> |
| 102 | + <label htmlFor="message" className="text-xl font-semibold"> |
| 103 | + Message |
| 104 | + </label> |
| 105 | + <textarea |
| 106 | + onChange={handleInputs} |
| 107 | + className="bg-transparent border px-2 py-1 rounded-sm resize-none h-40" |
| 108 | + id="message" |
| 109 | + type="message" |
| 110 | + name="message" |
| 111 | + value={contectFrom.message} |
| 112 | + placeholder="Enter your message" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + <button |
| 116 | + type="submit" |
| 117 | + className="w-full |
| 118 | + py-2 bg-yellow-600 hover:bg-yellow-500 transition-all ease-in-out duration-300 rounded-sm font-semibold text-xl" |
| 119 | + > |
| 120 | + Submit |
| 121 | + </button> |
| 122 | + </form> |
| 123 | + </div> |
| 124 | + </HomeLayout> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +export default Contect; |
0 commit comments