-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add feat/ContactUs-form branch #381
Closed
PriyanshuValiya
wants to merge
18
commits into
RamakrushnaBiswal:main
from
PriyanshuValiya:feat/ContactUs-form
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
56f7052
add feat/ContactUs-form branch
PriyanshuValiya a03eecc
Update index.js
PriyanshuValiya cfc2fa8
Update oauth.config.js
PriyanshuValiya cc084c0
Update contact.controller.js
PriyanshuValiya 6a933b9
Update contact.controller.js
PriyanshuValiya 012d5ae
Update contact.controller.js
PriyanshuValiya a760726
Update ContactUs.jsx
PriyanshuValiya 689029b
Update ContactUs.jsx
PriyanshuValiya 58fcfba
Update ContactUs.jsx
PriyanshuValiya 90e0fa0
Update contact.controller.js
PriyanshuValiya 1f8f7e3
Merge branch 'main' into feat/ContactUs-form
PriyanshuValiya c9bf8af
Update contact.controller.js
PriyanshuValiya 429c94e
Update ContactUs.jsx
PriyanshuValiya c472329
Update ContactUs.jsx
PriyanshuValiya ff1422f
Update ContactUs.jsx
PriyanshuValiya 8cebaec
Update ContactUs.jsx
PriyanshuValiya bb5409f
Update ContactUs.jsx
PriyanshuValiya 6426ecd
Update ContactUs.jsx
PriyanshuValiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { z } = require("zod"); | ||
const nodemailer = require("nodemailer"); | ||
require("dotenv").config(); | ||
|
||
// Define the Zod schema for contact form validation | ||
const contactSchema = z.object({ | ||
mail: z.string().email(), | ||
subject: z.string().min(5, "Subject must be at least 5 characters long."), | ||
message: z.string().min(5, "Message must be at least 5 characters long."), | ||
}); | ||
|
||
const createContactUs = async (req, res) => { | ||
const validation = contactSchema.safeParse(req.body); | ||
|
||
if (!validation.success) { | ||
console.error("Error at validation"); | ||
return res.status(400).json({ | ||
status: "error", | ||
errors: "contactSchema is not validate", | ||
}); | ||
} | ||
|
||
const { mail, subject, message } = req.body; | ||
|
||
try { | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
host: "smtp.gmail.com", | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
// Uncomment this if needed | ||
// tls: { | ||
// rejectUnauthorized: false, // Disable strict SSL verification | ||
// }, | ||
}); | ||
|
||
const mailOptions = { | ||
// from: mail, | ||
from: process.env.EMAIL_USER, | ||
+ replyTo: mail, | ||
to: process.env.EMAIL_USER, | ||
subject: subject, | ||
text: message, | ||
}; | ||
|
||
// Send mail with defined transport object | ||
|
||
+ await new Promise((resolve, reject) => { | ||
+ transporter.sendMail(mailOptions, (error, info) => { | ||
+ if (error) { | ||
+ console.error("Error occurred: " + error.message); | ||
+ reject(error); | ||
+ } | ||
+ resolve(info); | ||
+ }); | ||
+ }); | ||
+ | ||
+ res.status(200).json({ | ||
+ status: "success", | ||
+ message: "Your contact request has been successfully received.", | ||
+ }); | ||
|
||
} catch (err) { | ||
console.error(`Error at transport: ${err}`); | ||
res.status(500).json({ | ||
status: "error", | ||
message: | ||
"There was an error sending your message. Please try again later.", | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { createContactUs }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { createContactUs } = require("../controller/contact.controller"); // Correct controller path | ||
|
||
router.post("/contactus", createContactUs); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,45 @@ | ||
const express = require("express"); | ||
const logger = require("../config/logger"); // Import your Winston logger | ||
const logger = require("../config/logger"); // Import Winston logger | ||
require("dotenv").config(); | ||
|
||
const config = { | ||
JWT_SECRET: process.env.JWT_SECRET, | ||
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, | ||
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET, | ||
}; | ||
|
||
const router = express.Router(); | ||
|
||
let feedbackRouter; | ||
|
||
try { | ||
feedbackRouter = require("./feedbackRouter"); | ||
} catch (error) { | ||
logger.error("Error loading feedbackRouter:", error); // Log the error with Winston | ||
feedbackRouter = (req, res) => { | ||
res | ||
.status(500) | ||
.json({ error: "Feedback functionality is currently unavailable" }); | ||
}; | ||
} | ||
// Utility function to safely load modules and handle errors | ||
const safeRequire = (modulePath, fallbackMessage) => { | ||
try { | ||
return require(modulePath); | ||
} catch (error) { | ||
logger.error(`Error loading ${modulePath}:`, error); | ||
return (req, res) => { | ||
res.status(500).json({ error: fallbackMessage }); | ||
}; | ||
} | ||
}; | ||
|
||
let eventRouter; | ||
try { | ||
eventRouter = require("./eventRouter"); | ||
} catch (error) { | ||
logger.error("Error loading eventRouter:", error); // Log the error with Winston | ||
eventRouter = (req, res) => { | ||
res | ||
.status(500) | ||
.json({ error: "Event functionality is currently unavailable" }); | ||
}; | ||
} | ||
// Safely load routers with error handling | ||
const feedbackRouter = safeRequire("./feedbackRouter", "Feedback functionality is currently unavailable"); | ||
const contactUsRouter = safeRequire("./contactUsRouter", "Contact Us functionality is currently unavailable"); | ||
const eventRouter = safeRequire("./eventRouter", "Event functionality is currently unavailable"); | ||
|
||
router.get("/", (req, res) => { | ||
return res.json({ | ||
message: "Welcome to the restaurant API!", | ||
version: "1.0.0", | ||
endpoints: { | ||
Reservation: "/reservation", | ||
Feedback: "/feedback", // Added feedback endpoint documentation | ||
Feedback: "/feedback", | ||
}, | ||
documentation: "https://api-docs-url.com", | ||
}); | ||
}); | ||
|
||
router.use("/event", eventRouter); | ||
router.use("/admin", require("./adminRouter")); | ||
router.use("/admin", safeRequire("./adminRouter", "Admin functionality is currently unavailable")); | ||
router.use("/feedback", feedbackRouter); | ||
router.use("/user", require("./customerRouter")); | ||
router.use("/reservation", require("./reservationRouter")); | ||
router.use("/newsletter", require("./newsletterRoute")); | ||
router.use("/forgot", require("./forgotRouter")); | ||
router.use("/user", safeRequire("./customerRouter", "User functionality is currently unavailable")); | ||
router.use("/reservation", safeRequire("./reservationRouter", "Reservation functionality is currently unavailable")); | ||
router.use("/newsletter", safeRequire("./newsletterRoute", "Newsletter functionality is currently unavailable")); | ||
router.use("/forgot", safeRequire("./forgotRouter", "Forgot password functionality is currently unavailable")); | ||
router.use("/contact", contactUsRouter); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* eslint-disable prettier/prettier */ | ||
/* eslint-disable no-unused-vars */ | ||
import { useState } from 'react'; | ||
import { motion } from 'framer-motion'; | ||
import { useInView } from 'react-intersection-observer'; | ||
import chess from '../../assets/img/chess.gif'; | ||
|
||
const ContactUs = () => { | ||
const { ref, inView } = useInView({ | ||
threshold: 0.2, | ||
triggerOnce: true, | ||
}); | ||
|
||
const animationVariants = { | ||
hidden: { opacity: 0, y: 50 }, | ||
visible: { opacity: 1, y: 0, transition: { duration: 0.5 } }, | ||
}; | ||
|
||
// Use an environment variable for backend URL | ||
const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'; | ||
const [mail, setMail] = useState(''); | ||
const [subject, setSubject] = useState(''); | ||
const [message, setMessage] = useState(''); | ||
const [submitted, setSubmitted] = useState(false); | ||
const [error, setError] = useState(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
|
||
// Basic client-side validation for security | ||
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
+ if (!mail || !subject || !message) { | ||
+ setError('All fields are required.'); | ||
+ return; | ||
+ } | ||
+ if (!emailRegex.test(mail)) { | ||
+ setError('Please enter a valid email address.'); | ||
+ return; | ||
+ } | ||
+ if (subject.length > 100) { | ||
+ setError('Subject must be less than 100 characters.'); | ||
+ return; | ||
+ } | ||
+ if (message.length > 1000) { | ||
+ setError('Message must be less than 1000 characters.'); | ||
+ return; | ||
+ } | ||
|
||
// Clear any previous errors | ||
setError(null); | ||
|
||
setIsLoading(true); | ||
try { | ||
const response = await fetch(`${API_URL}/api/contact/contactus`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ mail, subject, message }), | ||
}); | ||
+ if (!response.ok) { | ||
+ throw new Error('Network response was not ok'); | ||
+ } | ||
setSubmitted(true); | ||
} | ||
setSubmitted(true); | ||
setTimeout(() => { | ||
setMail(''); | ||
setSubject(''); | ||
setMessage(''); | ||
setSubmitted(false); | ||
}, 3000); | ||
} catch (error) { | ||
setError('An error occurred while sending Mail...'); | ||
console.error('Mail sending failed : ', error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="bg-amber-100 h-full py-24 px-4 sm:px-6 lg:px-8"> | ||
<div className="max-w-7xl mx-auto"> | ||
<motion.div | ||
ref={ref} | ||
initial="hidden" | ||
animate={inView ? 'visible' : 'hidden'} | ||
variants={animationVariants} | ||
className="lg:grid lg:grid-cols-2 lg:gap-8 lg:items-center" | ||
> | ||
<div className="mt-8 mb-8 lg:mb-0 relative"> | ||
<h2 className="text-5xl font-black text-[#004D43]"> | ||
Feel Free To Mail Us.. | ||
</h2> | ||
<p className="mt-5 text-lg text-gray-700 pb-3"> | ||
Have questions or need assistance ? Reach out to us, and we'll be | ||
happy to help !! | ||
</p> | ||
<div className="flex md:h-[40vh] md:w-[60vh] ml-20 mt-20 items-center justify-center mt-12"> | ||
<img | ||
src={chess} | ||
alt="Chess" | ||
loading="lazy" | ||
className="md:p-10 p-5 object-contain bg-[#004D43] rounded-full shadow-2xl" | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div className="bg-[#004D43] rounded-xl p-3 pt-4 mt-40 h-fit"> | ||
<form onSubmit={handleSubmit} className="space-y-4"> | ||
<div> | ||
+ <label htmlFor="mail" className="sr-only">Email Address</label> | ||
<input | ||
type="email" | ||
id="mail" | ||
value={mail} | ||
placeholder="Email ID" | ||
+ aria-label="Email Address" | ||
onChange={(e) => setMail(e.target.value)} | ||
required | ||
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-[#004D43] focus:border-[#004D43]" | ||
/> | ||
</div> | ||
<div> | ||
<input | ||
type="text" | ||
id="text" | ||
placeholder="Subject" | ||
value={subject} | ||
onChange={(e) => setSubject(e.target.value)} | ||
required | ||
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-[#004D43] focus:border-[#004D43]" | ||
/> | ||
</div> | ||
<div> | ||
<textarea | ||
id="message" | ||
placeholder="Write your message..." | ||
rows="6" | ||
value={message} | ||
onChange={(e) => setMessage(e.target.value)} | ||
required | ||
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-[#004D43] focus:border-[#004D43] resize-none" | ||
></textarea> | ||
</div> | ||
<div> | ||
<button | ||
type="submit" | ||
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-[#09342e] hover:bg-[#072d28] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#004D43]" | ||
disabled={isLoading} | ||
> | ||
{isLoading ? 'Sending...' : 'Send Mail'} | ||
</button> | ||
</div> | ||
</form> | ||
{submitted && ( | ||
<motion.div | ||
initial={{ opacity: 0, y: -10, display: 'none', height: 0 }} | ||
animate={{ opacity: 1, y: 0, display: 'block', height: 'auto' }} | ||
className="mt-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded" | ||
> | ||
Thank you, We will reply you soon... | ||
</motion.div> | ||
)} | ||
{error && ( | ||
<motion.div | ||
initial={{ opacity: 0, y: -10 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
className="mt-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded" | ||
> | ||
{error} | ||
</motion.div> | ||
)} | ||
</div> | ||
</motion.div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ContactUs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Environment variables EMAIL_USER and EMAIL_PASS are not set.
Please set
EMAIL_USER
andEMAIL_PASS
in your deployment environment to prevent authentication issues.🔗 Analysis chain
Ensure environment variables are loaded correctly.
While you have
require("dotenv").config();
at the top, it's important to ensure that the environment variablesEMAIL_USER
andEMAIL_PASS
are properly loaded and handled in case they're missing.Run the following script to check if the environment variables are set:
Ensure that these variables are set in your deployment environment to prevent authentication errors.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 225