Skip to content

Commit

Permalink
Merge pull request #1740 from AyushSharma72/feedback
Browse files Browse the repository at this point in the history
added backend in feedback form
  • Loading branch information
PriyaGhosal authored Oct 30, 2024
2 parents 2dcc9bf + 388d082 commit e099991
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 7 deletions.
45 changes: 45 additions & 0 deletions backend/controllers/ContactController.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
const ContactUs = require("../models/ContactUs");

const FeedbackModal = require("../models/Feedback");

const nodemailer = require("nodemailer");

exports.submitContactForm = async (req, res) => {
const { name, email, message } = req.body;

try {
const newContact = new ContactUs({ name, email, message });
await newContact.save();
return res.status(201).json({ message: "Message sent successfully!" });

} catch (error) {
console.error("Error saving contact form:", error);
return res
.status(500)
.json({ message: "Failed to send message. Please try again later." });
}
};

exports.userfeedback = async (req, res) => {
try {
const { Name, Destination, Rating, Review, Complaint } = req.body;
if (!Name || !Destination || !Rating || !Review) {
return res.status(400).send({
message: "All fields are required",
});
}
const response = new FeedbackModal({
name: Name,
destination: Destination,
rating: Rating,
review: Review,
complaint: Complaint,
});

const feedback = await response.save();
if (response) {
res.status(201).send({
success: true,
message: "feedback recorded ",
feedback,
});
}
} catch (error) {
console.log(error);
res.status(500).send({
success: false,
message: "internal server error ",
});
=======
} catch (error) {
console.error("Error saving contact form:", error);
return res
.status(500)
.json({ message: "Failed to send message. Please try again later." });

}
};

Expand Down
23 changes: 17 additions & 6 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ const app = express();
connectDB();

// CORS configuration
app.use(
cors({
origin: "http://127.0.0.1:5505", // Correct: specify the base URL only
credentials: true, // Allow credentials (cookies) to be sent with requests
})
);

const allowedOrigins = [
'http://127.0.0.1:5505',
'https://deploy-preview-1740--buddytrail.netlify.app'
];

app.use(cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true // Allow credentials (cookies) to be sent with requests
}));


// Middleware
app.use(express.json()); // Parse JSON bodies
Expand Down
31 changes: 31 additions & 0 deletions backend/models/Feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const mongoose = require("mongoose");

const FeedbackSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
},
destination: {
type: String,
trim: true,
},
rating: {
type: Number,
required: true,
},
review: {
type: String,
required: true,
trim: true,
},
complaint: {
type: String,
trim: true,
},
},
{ timestamps: true }
);

module.exports = mongoose.model("Feedback", FeedbackSchema);
9 changes: 9 additions & 0 deletions backend/routes/ContactRoutes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const express = require("express");
const {
submitContactForm,

userfeedback,

sendEmail,

} = require("../controllers/ContactController.js");

const router = express.Router();

router.post("/", submitContactForm);


router.post("/feedback", userfeedback);

router.post("/email", sendEmail);

module.exports = router;
46 changes: 45 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2590,7 +2590,7 @@ <h2 style="color: #0057B3;" id="h1darkbtn">Rate your experience</h2>
placeholder="Describe any issue with the staff here. Please provide details"></textarea>

<div class="form-button">
<button type="submit">Send</button>
<button type="submit" onclick="submitFeedback(event)">Send</button>
</div>
</form>

Expand All @@ -2604,6 +2604,50 @@ <h2 style="color: #0057B3;" id="h1darkbtn">Rate your experience</h2>
</div>
</section>

<script>
async function submitFeedback(event) {
event.preventDefault();
const form = document.getElementById('reviewForm');

// Collecting form data
const Name = form.elements['name'].value;
const Destination = form.elements['destination'].value;
const Rating = form.querySelector('input[name="rate"]:checked')?.id.split('-')[1];
const Review = form.elements['review'].value;
const Complaint = form.elements['complaint'].value;

// Creating the payload
const feedbackData = {
Name,
Destination,
Rating,
Review,
Complaint,
};

try {
const response = await fetch('http://localhost:5000/api/contact/feedback', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(feedbackData),
});

const result = await response.json();
if (response.ok) {
alert('Feedback submitted successfully');
} else {
alert(`Error: ${result.message}`);
}
} catch (error) {
console.error('Error submitting feedback:', error);
alert('An error occurred while submitting feedback.');
}
}

</script>


<!--Cookie pop up-->
<div id="cookie-popup" class="cookie-container">
Expand Down

0 comments on commit e099991

Please sign in to comment.