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

fix: Updated feedback.js to accept only non-zero ratings #807

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Changes from all 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
38 changes: 30 additions & 8 deletions feedback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const ratings = [];
document.addEventListener('DOMContentLoaded', () => {
const starsContainers = document.getElementsByClassName("stars");
const ratingTexts = document.querySelectorAll('[id$="-rating-text"]');
let currentRating = 0;
//let currentRating = 0;

function colourStar(ind , container) {

Expand All @@ -20,6 +21,7 @@ document.addEventListener('DOMContentLoaded', () => {

// Create 5 stars dynamically for each stars container
Array.from(starsContainers).forEach((starsContainer, index) => {
ratings[index] = 0;
for (let i = 1; i <= 5; i++) {
const star = document.createElement('span');
star.classList.add('star');
Expand All @@ -28,7 +30,7 @@ document.addEventListener('DOMContentLoaded', () => {
starsContainer.appendChild(star);

star.addEventListener('click', () => {
currentRating = i;
ratings[index] = i;
updateRating(index);
colourStar(i , starsContainer);
});
Expand All @@ -37,15 +39,34 @@ document.addEventListener('DOMContentLoaded', () => {
});

const updateRating = (index) => {
ratingTexts[index].textContent = `Rating: ${currentRating}`;
ratingTexts[index].textContent = `Rating: ${ratings[index]}`;
};
});

function resetStars() {
Array.from(starsContainers).forEach((starsContainer, index) => {
ratings[index] = 0;
colourStar(0, starsContainer);
const ratingText = document.getElementById(starsContainer.id.replace("-stars-container", "-rating-text"));
if (ratingText) {
ratingText.textContent = "Rating: 0";
}
});
}

function submitFeedback() {
window.submitFeedback = function submitFeedback() {
var feedbackInput = document.getElementById("feedback-input");
var feedback = feedbackInput.value.trim();


// Check if any rating is zero
if (ratings.some(rating => rating === 0)) {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Please provide a non-zero rating for all categories before submitting.',
});
return; // Exit the function if any rating is zero
}

// Check if the feedback is empty
if (feedback === "") {
// Show a SweetAlert error modal
Expand All @@ -65,7 +86,8 @@ function submitFeedback() {
// Clear the feedback input box after the user acknowledges the success modal
if (result.isConfirmed || result.isDismissed) {
feedbackInput.value = "";
resetStars();
}
});

}
};
});
Loading