Contact.js
6.54 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
import FadeInStagger from "@/components/FadeInStagger";
import { useRouter } from "next/router";
import React from "react";
import { Col, Container, Row } from "react-bootstrap";
import axios from "axios";
import { useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
export const Contact = () => {
/* ---------------- VALIDATION SCHEMA ---------------- */
const schema = yup.object({
name: yup.string().required("Full name is required"),
email: yup
.string()
.email("Enter a valid email address")
.required("Email is required"),
phone: yup
.string()
.matches(/^[0-9]{10}$/, "Phone number must be 10 digits")
.required("Phone number is required"),
message: yup.string().required("Message is required"),
consent: yup.boolean().oneOf([true], "You must accept the terms"),
});
const router = useRouter();
const {
register,
handleSubmit,
watch,
formState: { errors, isSubmitting, isValid },
reset,
} = useForm({
resolver: yupResolver(schema),
mode: "onChange", // important for button enable/disable
});
const onSubmit = async (data) => {
try {
const payload = {
data: {
// FormSource: "Contact Form",
Name: data.name,
Email: data.email,
MobileNumber: data.phone,
Message: data.message,
},
};
await axios.post(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/website-leads`,
payload,
);
reset();
router.push("/thankyou");
} catch (error) {
console.error(error);
alert("Failed to submit form. Please try again.");
}
};
return (
<>
<section className="contact_sec">
<Container className="custom_container">
<Row className="justify-content-end">
<Col md={4}>
<div className="">
<FadeInStagger direction="top">
<h2 className="heading">Let’s Connect</h2>
<p>Create your Dream space together, Visit Us Today!</p>
</FadeInStagger>
<form
className="let-connect-form"
onSubmit={handleSubmit(onSubmit)}
noValidate
>
<Row>
<Col md={6} className="mb-3">
<div className="mb-1">Name</div>
<div>
<input
type="text"
placeholder="Type here"
name="name"
className="inputField nameInput"
{...register("name")}
/>
{errors.name && (
<p className="error">{errors.name.message}</p>
)}
</div>
</Col>
<Col md={6} className="mb-3">
<div className="mb-1">Mobile Number</div>
<div>
<input
type="text"
placeholder="Type here"
name="Mobile Number"
className="inputField nameInput"
{...register("phone")}
/>
{errors.phone && (
<p className="error">{errors.phone.message}</p>
)}
</div>
</Col>
</Row>
<Row>
<Col md={12} className="mb-3">
<div className="mb-1">Email Address</div>
<div>
<input
type="text"
placeholder="Type here"
name="Email Address"
className="inputField nameInput"
{...register("email")}
/>
{errors.email && (
<p className="error">{errors.email.message}</p>
)}
</div>
</Col>
</Row>
<Row>
<Col md={12} className="mb-3">
<div className="mb-1">Message</div>
<div>
<textarea
rows="4"
type="text"
placeholder="Type here"
name="name"
className="inputField nameInput"
{...register("message")}
/>
{errors.message && (
<p className="error">{errors.message.message}</p>
)}
</div>
</Col>
</Row>
<Row>
<Col className="mb-3">
<div className="form-check">
<input
className="form-check-input"
type="checkbox"
id="checkChecked"
{...register("consent")}
/>
<label
className="form-check-label"
htmlFor="checkChecked"
>
I accept the processing of my personal data for
traditional and automated direct marketing purposes.
</label>
</div>
{errors.consent && (
<p className="error">{errors.consent.message}</p>
)}
</Col>
</Row>
<Row>
<Col className="mb-3">
<div className="d-flex gap-3">
<button
type="submit"
className="btn3"
disabled={!watch("consent") || isSubmitting}
>
{isSubmitting ? "Submitting..." : "Submit"} <i className="fa-solid fa-arrow-right"></i>
</button>
</div>
</Col>
</Row>
</form>
</div>
</Col>
</Row>
</Container>
</section>
</>
);
};