|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { toast } from 'react-toastify'; |
| 3 | +import { useMutation } from '@apollo/client'; |
| 4 | +import { SEND_MESSAGE_MUTATION } from '../Mutations/contactUs.mutation'; |
| 5 | + |
| 6 | +interface FormData { |
| 7 | + name: string; |
| 8 | + email: string; |
| 9 | + phone: string; |
| 10 | + message: string; |
| 11 | +} |
| 12 | + |
| 13 | +export default function ContactUs() { |
| 14 | + const [formData, setFormData] = useState<FormData>({ |
| 15 | + name: '', |
| 16 | + email: '', |
| 17 | + phone: '', |
| 18 | + message: '', |
| 19 | + }); |
| 20 | + const [sendMessage, { loading, error }] = useMutation(SEND_MESSAGE_MUTATION); |
| 21 | + |
| 22 | + const [errors, setErrors] = useState<{ [key: string]: string }>({}); |
| 23 | + |
| 24 | + // Handle form input changes |
| 25 | + const handleChange = ( |
| 26 | + e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, |
| 27 | + ) => { |
| 28 | + const { name, value } = e.target; |
| 29 | + setFormData({ |
| 30 | + ...formData, |
| 31 | + [name]: value, |
| 32 | + }); |
| 33 | + |
| 34 | + if (errors[name]) { |
| 35 | + setErrors({ |
| 36 | + ...errors, |
| 37 | + [name]: '', |
| 38 | + }); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + // Validation function |
| 43 | + const validateForm = () => { |
| 44 | + const newErrors: { [key: string]: string } = {}; |
| 45 | + |
| 46 | + if (!formData.name) { |
| 47 | + newErrors.name = 'Name is required'; |
| 48 | + } |
| 49 | + |
| 50 | + // Email validation with regex |
| 51 | + const emailPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i; |
| 52 | + if (!formData.email) { |
| 53 | + newErrors.email = 'Email is required'; |
| 54 | + } else if (!emailPattern.test(formData.email)) { |
| 55 | + newErrors.email = 'Invalid email address'; |
| 56 | + } |
| 57 | + |
| 58 | + if (!formData.message) { |
| 59 | + newErrors.message = 'Message is required'; |
| 60 | + } |
| 61 | + |
| 62 | + setErrors(newErrors); |
| 63 | + return Object.keys(newErrors).length === 0; |
| 64 | + }; |
| 65 | + |
| 66 | + // Handle form submission |
| 67 | + const handleSubmit = async (e: React.FormEvent) => { |
| 68 | + e.preventDefault(); |
| 69 | + |
| 70 | + if (!validateForm()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + const { data } = await sendMessage({ |
| 76 | + variables: { |
| 77 | + name: formData.name, |
| 78 | + email: formData.email, |
| 79 | + phone: formData.phone, |
| 80 | + message: formData.message, |
| 81 | + }, |
| 82 | + }); |
| 83 | + toast.success('Your message has been sent successfully!'); |
| 84 | + setFormData({ name: '', email: '', phone: '', message: '' }); |
| 85 | + } catch (err) { |
| 86 | + toast.error('Error submitting the form, Try again'); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className="flex items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-800"> |
| 92 | + <div className="w-full max-w-lg p-6 rounded-lg shadow-none xmd:shadow-lg dark:bg-gray-800 xmd:dark:bg-gray-900 bg-gray-100 xmd:bg-white"> |
| 93 | + <h2 className="text-center text-3xl font-bold text-gray-900 dark:text-white mb-4"> |
| 94 | + Contact Us |
| 95 | + </h2> |
| 96 | + <p className="text-center text-gray-600 dark:text-gray-300 mb-6"> |
| 97 | + We would love to hear from you. Please fill out the form below to get |
| 98 | + in touch. |
| 99 | + </p> |
| 100 | + |
| 101 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 102 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 103 | + <div className="flex flex-col"> |
| 104 | + <label |
| 105 | + htmlFor="name" |
| 106 | + className="text-gray-700 dark:text-gray-300 font-semibold" |
| 107 | + > |
| 108 | + Name <span className="text-red-500">*</span> |
| 109 | + </label> |
| 110 | + <input |
| 111 | + type="text" |
| 112 | + id="name" |
| 113 | + name="name" |
| 114 | + value={formData.name} |
| 115 | + onChange={handleChange} |
| 116 | + className="mt-2 p-2 border rounded-md border-gray-300 dark:text-black dark:border-gray-600" |
| 117 | + /> |
| 118 | + {errors.name && ( |
| 119 | + <small className="text-red-600">{errors.name}</small> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="flex flex-col"> |
| 124 | + <label |
| 125 | + htmlFor="email" |
| 126 | + className="text-gray-700 dark:text-gray-300 font-semibold" |
| 127 | + > |
| 128 | + Email <span className="text-red-500">*</span> |
| 129 | + </label> |
| 130 | + <input |
| 131 | + type="text" |
| 132 | + id="email" |
| 133 | + name="email" |
| 134 | + value={formData.email} |
| 135 | + onChange={handleChange} |
| 136 | + className="mt-2 p-2 border rounded-md border-gray-300 dark:text-black dark:border-gray-600" |
| 137 | + /> |
| 138 | + {errors.email && ( |
| 139 | + <small className="text-red-600">{errors.email}</small> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div className="flex flex-col"> |
| 145 | + <label |
| 146 | + htmlFor="phone" |
| 147 | + className="text-gray-700 dark:text-gray-300 font-semibold" |
| 148 | + > |
| 149 | + Phone Number |
| 150 | + </label> |
| 151 | + <input |
| 152 | + type="tel" |
| 153 | + id="phone" |
| 154 | + name="phone" |
| 155 | + value={formData.phone} |
| 156 | + onChange={handleChange} |
| 157 | + className="mt-2 p-2 border rounded-md border-gray-300 dark:text-black dark:border-gray-600" |
| 158 | + /> |
| 159 | + </div> |
| 160 | + |
| 161 | + <div className="flex flex-col"> |
| 162 | + <label |
| 163 | + htmlFor="message" |
| 164 | + className="text-gray-700 dark:text-gray-300 font-semibold" |
| 165 | + > |
| 166 | + Message <span className="text-red-500">*</span> |
| 167 | + </label> |
| 168 | + <textarea |
| 169 | + id="message" |
| 170 | + name="message" |
| 171 | + value={formData.message} |
| 172 | + onChange={handleChange} |
| 173 | + className="mt-2 p-2 border rounded-md border-gray-300 dark:text-black dark:border-gray-600" |
| 174 | + rows={4} |
| 175 | + /> |
| 176 | + {errors.message && ( |
| 177 | + <small className="text-red-600">{errors.message}</small> |
| 178 | + )} |
| 179 | + </div> |
| 180 | + |
| 181 | + <div className="text-center"> |
| 182 | + <button |
| 183 | + type="submit" |
| 184 | + disabled={loading} |
| 185 | + className="w-full mt-4 py-2 bg-primary text-white font-semibold rounded-lg focus:outline-none" |
| 186 | + > |
| 187 | + {loading ? 'Submitting...' : 'Submit'} |
| 188 | + </button> |
| 189 | + </div> |
| 190 | + </form> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + ); |
| 194 | +} |
0 commit comments