diff --git a/backend/index.js b/backend/index.js index fa783ac..9e96c05 100644 --- a/backend/index.js +++ b/backend/index.js @@ -371,6 +371,37 @@ const transporter = nodemailer.createTransport({ }, }); +// api route for contactus page +app.post("/contactus", async (req, res) => { + const { name, email, message } = req.body; + + try { + // Set up Nodemailer transporter + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: process.env.EMAIL, + pass: process.env.PASS, + }, + }); + + const mailOptions = { + from: process.env.EMAIL, + to: process.env.EMAIL, + subject: `Contact Us message from ${name}`, + text: `From: ${name}\nEmail: ${email}\n\nMessage:\n${message}`, + }; + + await transporter.sendMail(mailOptions); + res.status(200).json({ message: "Message sent successfully" }); + } catch (error) { + res.status(500).json({ + message: "Server Error. Please try again after sometime.", + error: error.message, + }); + } +}); + // Route to handle forgot password app.post("/forgot-password", async (req, res) => { const { email } = req.body; @@ -428,7 +459,9 @@ app.post("/user/reset-password/:token", async (req, res) => { }); if (!user) { - return res.status(400).json({ message: "Password reset token is invalid or has expired." }); + return res + .status(400) + .json({ message: "Password reset token is invalid or has expired." }); } const hashedPassword = await bcrypt.hash(password, 10); @@ -447,7 +480,7 @@ app.post("/user/reset-password/:token", async (req, res) => { res.status(500).json({ message: "Server error", error: error.message }); } }); -app.put('/products/:id', async (req, res) => { +app.put("/products/:id", async (req, res) => { try { const product = await Product.findByIdAndUpdate(req.params.id, req.body, { new: true, @@ -470,9 +503,6 @@ app.delete("/products/:id", async (req, res) => { } }); - - - // Start the server const server = app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); diff --git a/backend/package-lock.json b/backend/package-lock.json index 7d9b65f..a66a124 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -15,11 +15,7 @@ "express": "^4.19.2", "jsonwebtoken": "^9.0.2", "mongoose": "^8.4.0", -<<<<<<< HEAD - "nodemailer": "^6.9.13", -======= "nodemailer": "^6.9.14", ->>>>>>> 53b4556520ccfc77bbb053ad39f15f4f69ced03a "nodemon": "^3.1.1", "twilio": "^5.1.1" }, @@ -5525,15 +5521,9 @@ "dev": true }, "node_modules/nodemailer": { -<<<<<<< HEAD - "version": "6.9.13", - "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.13.tgz", - "integrity": "sha512-7o38Yogx6krdoBf3jCAqnIN4oSQFx+fMa0I7dK1D+me9kBxx12D+/33wSb+fhOCtIxvYJ+4x4IMEhmhCKfAiOA==", -======= "version": "6.9.14", "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.14.tgz", "integrity": "sha512-Dobp/ebDKBvz91sbtRKhcznLThrKxKt97GI2FAlAyy+fk19j73Uz3sBXolVtmcXjaorivqsbbbjDY+Jkt4/bQA==", ->>>>>>> 53b4556520ccfc77bbb053ad39f15f4f69ced03a "engines": { "node": ">=6.0.0" } diff --git a/src/pages/Contact.js b/src/pages/Contact.js index b3d8fb7..0018e05 100644 --- a/src/pages/Contact.js +++ b/src/pages/Contact.js @@ -1,12 +1,39 @@ -import React from "react"; +import React, { useState } from "react"; import Navbar from "../components/Navbar"; +import { toast, ToastContainer } from "react-toastify"; import Footer from "../components/Footer"; +import axios from "axios"; +import "react-toastify/dist/ReactToastify.css"; const CustomerVoices = () => { + const [name, setName] = useState(""); + const [email, setEmail] = useState(""); + const [message, setMessage] = useState(""); + + const handleSubmit = async (e) => { + e.preventDefault(); + try { + const response = await axios.post("http://localhost:6352/contactus", { + name, + email, + message, + }); + if (response.status === 200) { + toast.success("Message sent successfully"); + setName(""); + setEmail(""); + setMessage(""); + } + } catch (error) { + toast.error("Error occurred. Try again after sometime."); + console.log(error); + } + }; + return ( <> - - {/* old style contact page, not removed due to a testing */} + +
@@ -24,7 +51,7 @@ const CustomerVoices = () => { Share your queries and message here and our team will contact you.

-
+
-
- -

Our team will be in contact with you as soon as possibles.

-
-
- */} +