-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRating System.js
90 lines (76 loc) · 3.97 KB
/
Rating System.js
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
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyC40hXX0N67X6TeT_IshAD-MjtlvmlWSeY",
authDomain: "gp-web-rating.firebaseapp.com",
projectId: "gp-web-rating",
storageBucket: "gp-web-rating.appspot.com",
messagingSenderId: "558006730604",
appId: "1:558006730604:web:f87460ac4ab7776ffbbd49",
measurementId: "G-NTJ4K7W5WQ"
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
document.addEventListener('DOMContentLoaded', () => {
const ratingDiv = document.getElementById('rating');
const ratingButtons = ratingDiv.querySelectorAll('button');
const averageDiv = document.getElementById('average');
// Check if rating was already done
if (localStorage.getItem('ratingDone')) {
ratingButtons.forEach(button => button.disabled = true);
}
// Function to update the average display
const updateAverageDisplay = async () => {
const doc = await db.collection('ratings').doc('summary').get();
if (doc.exists) {
const data = doc.data();
const totalRatings = data.totalRatings;
const totalSum = data.totalSum;
const average = (totalRatings > 0) ? (totalSum / totalRatings).toFixed(2) : 0;
averageDiv.textContent = `Average Rating: ${average}`;
} else {
averageDiv.textContent = `Average Rating: 0`;
}
};
updateAverageDisplay();
ratingButtons.forEach(button => {
button.addEventListener('click', async (event) => {
const rating = parseInt(event.target.getAttribute('data-rating'));
if (!localStorage.getItem('ratingDone')) {
const ratingDoc = db.collection('ratings').doc('summary');
try {
await db.runTransaction(async (transaction) => {
const doc = await transaction.get(ratingDoc);
if (!doc.exists) {
transaction.set(ratingDoc, {
'1_star': 0,
'2_star': 0,
'3_star': 0,
'4_star': 0,
'5_star': 0,
totalRatings: 0,
totalSum: 0
});
}
const data = doc.data();
const newRatingCount = (data[`${rating}_star`] || 0) + 1;
const newTotalRatings = (data.totalRatings || 0) + 1;
const newTotalSum = (data.totalSum || 0) + rating;
transaction.update(ratingDoc, {
[`${rating}_star`]: newRatingCount,
totalRatings: newTotalRatings,
totalSum: newTotalSum
});
});
// Store the rating done flag in localStorage
localStorage.setItem('ratingDone', 'true');
// Disable all buttons
ratingButtons.forEach(button => button.disabled = true);
updateAverageDisplay();
} catch (error) {
console.error("Error saving rating: ", error);
}
}
});
});
});