Skip to content

Commit fb2ec83

Browse files
authored
All ratings included (#125)
* More ratings
1 parent a045323 commit fb2ec83

3 files changed

Lines changed: 78 additions & 54 deletions

File tree

my-app/firebase.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -126,50 +126,6 @@ export function syncScrollPositionToFirebase(model, containerRef) {
126126
containerRef.current?.removeEventListener("scroll", handleScroll);
127127
}
128128

129-
function startAverageRatingListener(model) {
130-
const coursesRef = ref(db, "reviews");
131-
132-
// Step 1: One-time fetch if model.avgRating is not initialized
133-
if (!model.avgRating || Object.keys(model.avgRating).length === 0) {
134-
get(coursesRef).then((snapshot) => {
135-
if (!snapshot.exists()) return;
136-
137-
const initialRatings = {};
138-
139-
snapshot.forEach((courseSnapshot) => {
140-
const courseCode = courseSnapshot.key;
141-
const avgRating = courseSnapshot.child("avgRating").val();
142-
143-
if (typeof avgRating === "number") {
144-
initialRatings[courseCode] = avgRating;
145-
}
146-
});
147-
148-
model.setAverageRatings(initialRatings);
149-
});
150-
}
151-
152-
// Step 2: listener for each courses avgRating
153-
onChildAdded(coursesRef, (courseSnapshot) => {
154-
const courseCode = courseSnapshot.key;
155-
const avgRatingRef = ref(db, `reviews/${courseCode}/avgRating`);
156-
157-
onValue(avgRatingRef, (ratingSnapshot) => {
158-
if (!ratingSnapshot.exists()) return;
159-
160-
const rating = ratingSnapshot.val();
161-
162-
if (typeof rating === "number") {
163-
model.updateAverageRating(courseCode, rating);
164-
}
165-
});
166-
});
167-
168-
onChildRemoved(coursesRef, (courseSnapshot) => {
169-
const courseCode = courseSnapshot.key;
170-
model.updateAverageRating(courseCode, null);
171-
});
172-
}
173129

174130

175131
function saveCoursesToCache(courses, timestamp) {
@@ -348,6 +304,51 @@ export async function addReviewForCourse(courseCode, review) {
348304
}
349305
}
350306

307+
function startAverageRatingListener(model) {
308+
const coursesRef = ref(db, "reviews");
309+
310+
// Step 1: One-time fetch if model.avgRating is not initialized
311+
if (!model.avgRating || Object.keys(model.avgRating).length === 0) {
312+
get(coursesRef).then((snapshot) => {
313+
if (!snapshot.exists()) return;
314+
315+
const initialRatings = {};
316+
317+
snapshot.forEach((courseSnapshot) => {
318+
const courseCode = courseSnapshot.key;
319+
const avgRating = courseSnapshot.child("avgRating").val();
320+
321+
if (avgRating && Array.isArray(avgRating)) {
322+
initialRatings[courseCode] = avgRating;
323+
}
324+
});
325+
326+
model.setAverageRatings(initialRatings);
327+
});
328+
}
329+
330+
// Step 2: listener for each courses avgRating
331+
onChildAdded(coursesRef, (courseSnapshot) => {
332+
const courseCode = courseSnapshot.key;
333+
const avgRatingRef = ref(db, `reviews/${courseCode}/avgRating`);
334+
335+
onValue(avgRatingRef, (ratingSnapshot) => {
336+
if (!ratingSnapshot.exists()) return;
337+
338+
const rating = ratingSnapshot.val();
339+
340+
if (typeof rating === "number") {
341+
model.updateAverageRating(courseCode, rating);
342+
}
343+
});
344+
});
345+
346+
onChildRemoved(coursesRef, (courseSnapshot) => {
347+
const courseCode = courseSnapshot.key;
348+
model.updateAverageRating(courseCode, null);
349+
});
350+
}
351+
351352
export async function getReviewsForCourse(courseCode) {
352353
const reviewsRef = ref(db, `reviews/${courseCode}`);
353354
const snapshot = await get(reviewsRef);

my-app/functions/index.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,49 @@ exports.updateCourseAvgRating = functions.https.onCall(async (data, context) =>
2020
}
2121

2222
// Compute average from overallRating
23-
let total = 0;
24-
let count = 0;
23+
let totalOverall = 0;
24+
let countOverall = 0;
25+
let totalDifficulty = 0;
26+
let countDifficulty = 0;
27+
let totalProfessor = 0;
28+
let countProfessor = 0;
2529

2630
Object.values(reviews).forEach(review => {
2731
if (typeof review.overallRating === 'number') {
28-
total += review.overallRating;
29-
count++;
32+
totalOverall += review.overallRating;
33+
countOverall++;
34+
}
35+
if (typeof review.difficultyRating === 'number') {
36+
totalDifficulty += review.difficultyRating;
37+
countDifficulty++;
38+
}
39+
if (typeof review.professorRating === 'number') {
40+
totalProfessor += review.professorRating;
41+
countProfessor++;
3042
}
3143
});
3244

33-
if (count === 0) {
45+
let avgRatingOverall = 0;
46+
let avgRatingDifficulty = 0;
47+
let avgRatingProfessor = 0;
48+
49+
if (!(countOverall === 0)) {
50+
avgRatingOverall = parseFloat((totalOverall / countOverall).toFixed(2));
51+
}
52+
if (!(countDifficulty === 0)) {
53+
avgRatingDifficulty = parseFloat((totalDifficulty / countDifficulty).toFixed(2));
54+
}
55+
if (!(countProfessor === 0)) {
56+
avgRatingProfessor = parseFloat((totalProfessor / countProfessor).toFixed(2));
57+
}
58+
if (countOverall === 0 && countDifficulty === 0 && countProfessor === 0) {
3459
throw new functions.https.HttpsError('failed-precondition', 'No valid ratings');
3560
}
3661

37-
const avgRating = parseFloat((total / count).toFixed(2));
38-
3962
// Update the avgRating in reviews
40-
await db.ref(`reviews/${courseCode}`).update({ avgRating });
63+
await db.ref(`reviews/${courseCode}`).update({"avgRating" :[avgRatingOverall, avgRatingDifficulty, avgRatingProfessor] } );
4164

42-
return { success: true, avgRating };
65+
return { success: true, "avgRating": [avgRatingOverall, avgRatingDifficulty, avgRatingProfessor] };
4366
} catch (error) {
4467
throw new functions.https.HttpsError('internal', error.message);
4568
}

my-app/src/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const model = {
1616
courses: [],
1717
departments : [],
1818
locations: [],
19+
// indexes: 0 -> overall rating; 1 -> difficulty; 2->teacher rating
1920
avgRatings: [],
2021
/* courses the user selected as their favourite */
2122
favourites: [],
@@ -296,7 +297,6 @@ export const model = {
296297
if (isOpen) {
297298
window.history.pushState({}, '', '/' + this.selectedCourse.code);
298299
}
299-
console.log("POPOPOOPOPOOOOOOP")
300300
if (!isOpen) {
301301
let current_url = window.location.href;
302302
console.log(current_url);

0 commit comments

Comments
 (0)