-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeedback.html
More file actions
161 lines (136 loc) Β· 5.69 KB
/
feedback.html
File metadata and controls
161 lines (136 loc) Β· 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mess Feedback</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet" type="text/css" />
</head>
<body class="flex items-center justify-center min-h-screen bg-cover bg-center" style="background-image: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.5)), url('https://www.nitk.ac.in/design-system/events/defaults/nitk.jpg');">
<div class="max-w-3xl mx-auto bg-white p-6 rounded-xl shadow-md">
<h1 class="text-2xl font-bold mb-4 text-center">π Mess Feedback Form</h1>
<!-- Feedback Form -->
<form id="feedbackForm" class="space-y-4">
<div>
<label for="mess" class="block font-medium">Select Mess</label>
<select id="mess" class="w-full p-2 border rounded" required></select>
</div>
<label class="label">Mess Rating</label>
<div id="star-rating" class="flex gap-1 cursor-pointer">
<!-- Stars will be generated by JS -->
</div>
<div>
<label for="feedback" class="block font-medium">Feedback</label>
<textarea id="feedback" class="w-full p-2 border rounded" rows="4" required></textarea>
</div>
<button type="submit" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">
Submit Feedback
</button>
</form>
<div class="mt-8">
<h2 class="text-xl font-semibold mb-2">π Recent Feedbacks</h2>
<div id="feedbackList" class="space-y-3"></div>
</div>
</div>
<script>
let selectedRating = 0;
document.addEventListener("DOMContentLoaded", async () => {
const messDropdown = document.getElementById("mess");
const feedbackList = document.getElementById("feedbackList");
const studentId = sessionStorage.getItem("student_id");
if (!studentId) {
alert("Please log in first.");
window.location.href = "index.html";
return;
}
// Load messes from backend
async function loadMesses() {
const res = await fetch("http://localhost:3000/api/messes");
const messes = await res.json();
messes.forEach(mess => {
const option = document.createElement("option");
option.value = mess.name;
option.textContent = mess.name;
messDropdown.appendChild(option);
});
if (messes.length > 0) {
loadFeedbacks(messes[0].name);
}
}
// Load feedbacks for selected mess
async function loadFeedbacks(messName) {
const res = await fetch(`http://localhost:3000/api/feedbacks/${messName}`);
const feedbacks = await res.json();
feedbackList.innerHTML = "";
if (feedbacks.length === 0) {
feedbackList.innerHTML = "<p class='text-gray-500'>No feedbacks yet.</p>";
return;
}
feedbacks.forEach(fb => {
const div = document.createElement("div");
div.className = "p-4 border rounded bg-gray-50";
const stars = "β
".repeat(fb.rating) + "β".repeat(5 - fb.rating);
div.innerHTML = `
<p class="font-bold text-primary">Roll No: ${fb.student_id}</p>
<p class="text-yellow-500 text-lg">${stars}</p>
<p class="text-sm">${fb.feedback}</p>
<p class="text-xs text-gray-400">${new Date(fb.timestamp).toLocaleString()}</p>
`;
feedbackList.appendChild(div);
});
}
// Mess change triggers feedback reload
messDropdown.addEventListener("change", (e) => {
loadFeedbacks(e.target.value);
});
// Star Rating UI
const starContainer = document.getElementById("star-rating");
for (let i = 1; i <= 5; i++) {
const star = document.createElement("span");
star.innerHTML = "★";
star.className = "text-2xl text-gray-400 hover:text-yellow-500";
star.dataset.value = i;
star.addEventListener("click", () => {
selectedRating = i;
updateStars(i);
});
starContainer.appendChild(star);
}
function updateStars(rating) {
const stars = starContainer.querySelectorAll("span");
stars.forEach((star, index) => {
star.classList.toggle("text-yellow-400", index < rating);
star.classList.toggle("text-gray-400", index >= rating);
});
}
// Handle form submission
const form = document.getElementById("feedbackForm");
form.addEventListener("submit", async (e) => {
e.preventDefault();
const messName = messDropdown.value;
const rating = selectedRating;
const feedbackText = document.getElementById("feedback").value;
const response = await fetch("http://localhost:3000/api/submit-feedback", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
student_id: studentId,
mess_name: messName,
rating: rating,
feedback: feedbackText
})
});
if (response.ok) {
alert("β
Feedback submitted successfully!");
form.reset();
loadFeedbacks(messName);
} else {
alert("β Failed to submit feedback");
}
});
loadMesses();
});
</script>
</body>
</html>