Skip to content
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

Created Feedback Submission System #65

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MONGO_URI=mongodb+srv://vsamarth1212:[email protected]/
MONGO_URI=mongodb+srv://vsamarth1212:[email protected]/playcafe
47 changes: 47 additions & 0 deletions backend/controller/feedback.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { z } = require("zod");
const Feedback = require("../models/feedback.model");

// Define the Zod schema for feedback validation
const feedbackSchema = z.object({
name: z.string(),
email: z.string(),
feedback: z.string(),
});
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

async function createFeedback(req, res) {
try {
const validationResult = feedbackSchema.safeParse(req.body);

if (!validationResult.success) {
console.error("Validation error:", validationResult.error.errors);
return res.status(400).json({
success: false,
message: "Validation failed",
errors: validationResult.error.errors,
});
}

const feedback = await Feedback.create(validationResult.data);

res.status(201).json({
success: true,
message: "Feedback created successfully",
data: feedback,
});
} catch (error) {
console.error("Error creating feedback:", error);
res.status(500).json({
success: false,
message: "An error occurred while creating the feedback",
});
}
}
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

module.exports = createFeedback;
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

//dummy api call for feedback
// {
// "name": "John Doe",
// "email": "[email protected]",
// "feedback": "This is a dummy feedback"
// }
21 changes: 21 additions & 0 deletions backend/models/feedback.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const feedbackSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
feedback: {
type: String,
required: true,
},
});
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

const Feedback = mongoose.model("Feedback", feedbackSchema);

module.exports = Feedback;
20 changes: 20 additions & 0 deletions backend/routes/feedbackRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require("express");
const Feedback = require("../models/feedback.model");
const createFeedback = require("../controller/feedback.controller");

const router = express.Router();

router.post("/create", createFeedback);

router.get("/", (req, res) => {
res.json({
message: "Welcome to the feedback API!",
version: "1.0.0",
endpoints: {
createFeedback: "/create [POST]",
},
documentation: "https://api-docs-url.com",
});
});
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

module.exports = router;
1 change: 1 addition & 0 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Reservation = require("../models/reservation.model");

const router = express.Router();

router.use("/feedback", require("./feedbackRouter"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling when requiring feedbackRouter.

To improve robustness, consider wrapping the require statement in a try-catch block. This will help gracefully handle any potential issues that might occur when loading the feedbackRouter module.

Here's a suggested implementation:

let feedbackRouter;
try {
  feedbackRouter = require("./feedbackRouter");
} catch (error) {
  console.error("Error loading feedbackRouter:", error);
  feedbackRouter = (req, res, next) => {
    res.status(500).json({ error: "Feedback functionality is currently unavailable" });
  };
}
router.use("/feedback", feedbackRouter);

router.use("/reservation", require("./reservationRouter"));
router.get("/", (req, res) => {
res.json({
Expand Down
15 changes: 14 additions & 1 deletion frontend/src/components/ui/FeedbackForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ const FeedbackForm = () => {
const [feedback, setFeedback] = useState("");
const [submitted, setSubmitted] = useState(false);

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
console.log(`Name: ${name}, Email: ${email}, Feedback: ${feedback}`);
const response = await fetch("http://localhost:3000/api/feedback/create", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use environment variables for API endpoint.

The API endpoint is currently hardcoded, which may cause issues when deploying to different environments (e.g., staging, production).

Consider using environment variables to manage the API URL. For example:

const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:3000';
const response = await fetch(`${API_URL}/api/feedback/create`, {
  // ... rest of the code
});

Make sure to set up the appropriate environment variables in your deployment process.

method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, email, feedback }),
});
const data = await response.json();
if (!response.ok) {
console.error("Feedback submission failed:", data.message);
return;
}
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
setSubmitted(true);
setTimeout(() => {
setName("");
Expand Down
Loading