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