Skip to content

Commit

Permalink
Merge branch 'main' into backend/feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Sawan-Kushwah authored Nov 1, 2024
2 parents 45bef13 + cf01b47 commit ab74008
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 7 deletions.
6 changes: 4 additions & 2 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import dotenv from "dotenv";
import connectDB from "./utils/db.js";
import blogRoutes from "./routes/blogRoutes.js";
import userRoutes from "./routes/userRoutes.js";
import feedbackRoutes from "./routes/feedbackRoute.js";
import cors from "cors";
import feedbackRoutes from "./routes/feedbackRoute.js";
import contactRoutes from "./routes/contactRoute.js";
import cors from "cors";

dotenv.config();
const app = express();
Expand All @@ -18,6 +19,7 @@ app.use(cors());
app.use("/api/users", userRoutes);
app.use("/api/blogs", blogRoutes);
app.use("/api/feedback", feedbackRoutes);
app.use("/api/contact", contactRoutes);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
Expand Down
30 changes: 30 additions & 0 deletions backend/controllers/contactController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Contact from "../models/contact.js";

export async function saveContact(req, resp) {
try {
const { name, email, subject, message } = req.body;

// Check if all fields are provided
if (!name || !email || !subject || !message) {
return resp.status(400).json({ message: "All fields are required." });
}

// Create new contact document
const newContact = new Contact({ name, email, subject, message });

// Save contact form data to the database
await newContact.save();

// Respond with success message
resp
.status(201)
.json({ message: "Contact form submitted successfully!", newContact });
} catch (error) {
console.error("Error saving contact form:", error);
resp.status(500).json({ message: "Failed to submit contact form.", error });
}
}

export async function getContact(req, resp) {
resp.send('hello contact')
}
33 changes: 33 additions & 0 deletions backend/models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose from "mongoose";

const contactSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
trim: true,
match: [/^\S+@\S+\.\S+$/, "Please enter a valid email address"]
},
subject: {
type: String,
required: true,
trim: true
},
message: {
type: String,
required: true,
trim: true
},
createdAt: {
type: Date,
default: Date.now
}
});

const Contact = mongoose.model("Contact", contactSchema);

export default Contact;
8 changes: 8 additions & 0 deletions backend/routes/contactRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express";
const router = express.Router();
import { getContact, saveContact } from "../controllers/contactController.js";

router.post("/saveContact", saveContact);
router.get("/saveContact", getContact);

export default router;
38 changes: 33 additions & 5 deletions frontend/src/pages/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,39 @@ export function renderContact(container) {
`;

// Form submission
document.getElementById('contactForm').addEventListener('submit', function(e) {
document.getElementById('contactForm').addEventListener('submit', async function (e) {
e.preventDefault();
// Here you would typically send the form data to your server
// For this example, we'll just show a success message
document.getElementById('formSuccess').classList.remove('hidden');
this.reset();

// Create a new FormData object from the form
const formData = new FormData(this);

// Convert FormData to an object
const data = Object.fromEntries(formData.entries());

try {
// Send form data to backend API
const response = await fetch('http://localhost:5000/api/contact/saveContact', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
console.log(response)

if (response.ok) {
// Display success message and reset the form
document.getElementById('formSuccess').classList.remove('hidden');
this.reset();
} else {
// Handle error response
const errorData = await response.json();
console.error("Error submitting form:", errorData.message);
}
} catch (error) {
console.error("Error submitting form:", error);
}
});


}

0 comments on commit ab74008

Please sign in to comment.