ContactForm.js
7.97 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import React, { useState } from "react";
import Link from "next/link";
import GoogleMap from "./GoogleMap";
import { Col, Container, Row, Form } from "react-bootstrap";
import Heading from "@/components/reuseables/Heading";
import { useForm } from "react-hook-form";
import axios from "axios";
import { useRouter } from "next/router";
const ContactForm = () => {
const {
handleSubmit,
control,
formState: { errors },
register,
reset,
} = useForm({
mode: "onBlur",
});
// email api
const router = useRouter();
const onSubmit = async (data) => {
const leadData = {
mobilenumber: data.Mobile,
email: data.Email,
message: data.AdditionalMessage || "",
name: data.Name,
company: data.Company,
source: data.source,
};
console.log(leadData, "form-data");
try {
const response = await axios.post("/api/googlesheetapi", leadData, {
headers: {
"Content-Type": "application/json",
},
});
console.log("Google Sheet API response:", response?.data);
if (response.data.success) {
router.push("/thank-you");
sendEmaill(data);
}
} catch (error) {
console.error(
"Error submitting to Google Sheet API:",
error.response ? error.response.data : error.message
);
}
reset();
};
const sendEmaill = (data) => {
fetch("/api/sendEmail", {
method: "POST",
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => {
console.log("response received", response);
if (response.status === 200) {
console.log("response succeeded");
} else {
console.log("response failed");
}
})
.catch((error) => {
console.error(error);
});
};
return (
<>
<div className="contact-area ptb-100">
<Container>
<Row>
<Col lg={6} sm={12}>
<GoogleMap />
</Col>
<Col lg={6} sm={12}>
<div className="contact-form">
<span className="sub-title">SEND MESSAGE</span>
<Heading heading="Write to Us!" />
<p>
Connect with Advith Consulting today for an insightful
consultation on how our tailored services can help propel your
business towards success.
</p>
<form onSubmit={handleSubmit(onSubmit)}>
<Row>
<Col lg={6} md={6} sm={6}>
<div className="form-group">
<Form.Group>
<Form.Control
required
type="text"
placeholder="Name"
className="text-dark form-control"
{...register("Name", {
required: "Enter Your Name",
maxLength: {
value: 100,
message: "Name is too long",
},
})}
/>
{errors.Name && (
<span className="error">{errors.Name.message}</span>
)}
<Form.Control
required
type="text"
placeholder="Name"
defaultValue={"contact form"} // Set defaultValue
className="text-dark d-none"
{...register("source", {})}
/>
</Form.Group>
</div>
</Col>
<Col lg={6} md={6} sm={6}>
<div className="form-group">
<Form.Group>
<Form.Control
required
type="email"
className="text-dark form-control"
placeholder="Email"
{...register("Email", {
required: "Enter Your Email",
pattern: {
value:
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
message: "Invalid email address",
},
})}
/>
{errors.Email && (
<span className="error">{errors.Email.message}</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={6} md={6} sm={6}>
<div className="form-group">
<Form.Group>
<Form.Control
required
type="text"
placeholder="Mobile"
className="text-dark form-control"
{...register("Mobile", {
required: "Enter Your Mobile Number",
maxLength: {
value: 10,
message: "Invalid Mobile number",
},
})}
/>
{errors.Mobile && (
<span className="error">{errors.Mobile.message}</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={6} md={6} sm={6}>
<div className="form-group">
<Form.Group>
<Form.Control
required
type="text"
className="text-dark form-control"
placeholder="Company Name"
{...register("Company", {
required: "Enter your Company",
})}
/>
{errors.Company && (
<span className="error">
{errors.Company.message}
</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={12} md={12} sm={6}>
<div className="form-group">
<Form.Group>
<Form.Control
as="textarea"
rows={5}
required
type="text"
placeholder="Additional message"
className="text-dark form-control"
{...register("AdditionalMessage", {})}
/>
{errors.AdditionalMessage && (
<span className="error">
{errors.AdditionalMessage.message}
</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={12} md={12} sm={12}>
<button
type="submit"
className="default-btn submit submit-btn"
>
Send Message <i className="ri-arrow-right-line"></i>
</button>
</Col>
</Row>
</form>
</div>
</Col>
</Row>
</Container>
</div>
</>
);
};
export default ContactForm;