CallBackRequest.js
8.32 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import Image from "next/image";
import React from "react";
import { Col, Container, Form, Row } from "react-bootstrap";
import Heading from "./Heading";
import { motion } from "framer-motion";
import { slideFromLeft } from "./variants";
import { useRouter } from "next/router";
import { useForm } from "react-hook-form";
import axios from "axios";
const CallBackRequest = () => {
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,
service: data.service,
source: data.source,
};
console.log(leadData, "form-data");
const strapiData = {
data: {
Name: data.Name,
Phone: data.Mobile,
Email: data.Email,
Service: data.service,
Source: data.source,
},
};
console.log("strapiData", strapiData);
const config = {
headers: {
"Content-Type": "multipart/form-data",
},
};
try {
const response = await axios.post(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/service-forms`,
strapiData,
config
);
if (response.status === 200) {
console.log("Form submited to strapi");
}
} catch (error) {
console.error(error);
}
try {
const response = await axios.post("/api/homegooglesheet", 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/sendEmailHome", {
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="free-quote-area ptb-100">
<Container>
<Row className="align-items-center">
<Col lg={6} md={12}>
<div className="free-quote-image">
<motion.div
variants={slideFromLeft(0.5)}
initial={"hidden"}
whileInView={"show"}
viewport={{ once: false, amount: 0.4 }}
>
<Image
src="/images/contact-us.png"
layout="fill"
className="img-fluid image"
alt="image"
/>
</motion.div>
</div>
</Col>
<Col lg={6} md={12}>
<div className="free-quote-text">
<Heading el="h2" heading="Write to Us!" />
<span>
Have a quick question or need more information? Fill out the
form below, and we'll get back to you as soon as possible
</span>
<form onSubmit={handleSubmit(onSubmit)}>
<Row>
<Col lg={6} md={6}>
<div className="form-group">
<label>Your Name</label>
<Form.Group>
<Form.Control
type="text"
className="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={"Home page form"} // Set defaultValue
className="text-dark d-none"
{...register("source", {})}
/>
</Form.Group>
</div>
</Col>
<Col lg={6} md={6}>
<div className="form-group">
<label>Your Email</label>
<Form.Group>
<Form.Control
required
type="text"
className="form-control"
{...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}>
<div className="form-group">
<label>Phone Number</label>
<Form.Group>
<Form.Control
required
type="text"
className="form-control"
onInput={(e) => {
e.target.value = e.target.value.replace(/[^0-9]/g, '');
}}
{...register("Mobile", {
required: "Enter Your Mobile Number",
maxLength: {
value: /^[6-9]\d{9}$/,
message: "Invalid Mobile number",
},
})}
/>
{errors.Mobile && (
<span className="error">{errors.Mobile.message}</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={6} md={6}>
<div className="form-group">
<label>Services</label>
<Form.Group>
<Form.Control
type="text"
placeholder="Enter Service"
required
{...register("service", {
required: "Enter Service",
})}
className="form-control"
/>
{errors.service && (
<span className="error">{errors.service.message}</span>
)}
</Form.Group>
</div>
</Col>
<Col lg={6} md={6}>
<div className="form-group">
<button type="submit" className="default-btn">
Submit
<i className="ri-arrow-right-line"></i>
</button>
</div>
</Col>
</Row>
</form>
</div>
</Col>
</Row>
</Container>
</div>
</>
);
};
export default CallBackRequest;