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 3 commits
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
1 change: 0 additions & 1 deletion backend/.env

This file was deleted.

2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
dist

.env
package-lock.json
16 changes: 16 additions & 0 deletions backend/config/api.info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
message: "Welcome to the feedback API!",
version: "1.0.0",
endpoints: {
createFeedback: {
path: "/create",
method: "POST",
parameters: {
name: "string (required)",
email: "string (required)",
message: "string (required)",
},
},
},
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
documentation: "https://api-docs-url.com",
};
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().min(2).max(100),
email: z.string().email(),
feedback: z.string().min(10),
});

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"
// }
41 changes: 41 additions & 0 deletions backend/models/feedback.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const feedbackSchema = new Schema(
{
name: {
type: String,
required: true,
trim: true,
maxlength: [100, "Name cannot be more than 100 characters"],
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
maxlength: [255, "Email cannot be more than 255 characters"],
match: [
/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/,
"Please fill a valid email address",
],
},
feedback: {
type: String,
required: true,
trim: true,
maxlength: [1000, "Feedback cannot be more than 1000 characters"],
},
createdAt: {
type: Date,
default: Date.now,
},
},
{
timestamps: true,
}
);
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

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

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

const router = express.Router();

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

const apiInfo = require("../config/api.info");

router.get("/", (req, res) => {
try {
res.json(apiInfo);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});

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

const router = express.Router();

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
7 changes: 7 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# Frontend build
.env.local
.env.development.local
.env.test.local
.env.production.local
.env
36 changes: 33 additions & 3 deletions frontend/src/components/ui/FeedbackForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@ const FeedbackForm = () => {
hidden: { opacity: 0, y: 50 },
visible: { opacity: 1, y: 0, transition: { duration: 0.5 } },
};

const API_URL = import.meta.env.VITE_BACKEND_URI || "http://localhost:3000/";
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [feedback, setFeedback] = useState("");
const [submitted, setSubmitted] = useState(false);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(false);

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
console.log(`Name: ${name}, Email: ${email}, Feedback: ${feedback}`);
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
const response = await fetch(`${API_URL}/feedback/create`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, email, feedback }),
});
const data = await response.json();
if (!response.ok) {
const errorMessage =
data.message || "An error occurred while submitting feedback.";
setError(errorMessage);
console.error("Feedback submission failed:", errorMessage);
return;
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
}

setSubmitted(true);
setIsLoading(false);
setError(null);
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(() => {
setName("");
setEmail("");
Expand Down Expand Up @@ -113,7 +134,7 @@ const FeedbackForm = () => {
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-[#004D43] hover:bg-[#003d35] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#004D43]"
>
Submit Feedback
{isLoading ? "Submitting..." : "Submit Feedback"}
</button>
</div>
</form>
Expand All @@ -126,6 +147,15 @@ const FeedbackForm = () => {
Thank you for your feedback!
</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>
Expand Down
Loading