-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfirebase.js
More file actions
562 lines (512 loc) · 17 KB
/
firebase.js
File metadata and controls
562 lines (512 loc) · 17 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import { initializeApp } from "firebase/app";
import { getAuth, GoogleAuthProvider, onAuthStateChanged } from "firebase/auth";
import { getFunctions, httpsCallable } from "firebase/functions";
import {
get,
getDatabase,
ref,
set,
onValue,
onChildRemoved,
onChildAdded,
runTransaction,
} from "firebase/database";
import { reaction, toJS } from "mobx";
import { push } from "firebase/database";
/**
* Firebase configuration and initialization.
* This code connects to Firebase, sets up authentication and allows to save and fetch courses as well as user data.
* Data Synchronization and caching are also handled.
* The firebase realtime database is used to store courses, user data, and reviews.
* If you would like to reuse this project, make sure to configure rules as shown in firebas_rules.json.
*/
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyCBckVI9nhAP62u5jZJW3F4SLulUv7znis",
authDomain: "findmynextcourse.firebaseapp.com",
databaseURL:
"https://findmynextcourse-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "findmynextcourse",
storageBucket: "findmynextcourse.firebasestorage.app",
messagingSenderId: "893484115963",
appId: "1:893484115963:web:59ac087d280dec919ccd5e",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app);
export const auth = getAuth(app);
export const db = getDatabase(app);
export const googleProvider = new GoogleAuthProvider();
googleProvider.addScope("profile");
googleProvider.addScope("email");
/**
* Startup hook to connect the model to Firebase.
* This function sets up the Firebase connection, fetches initial data and starts listener for syncing data.
* @param {object} model The reactive model
*/
export async function connectToFirebase(model) {
await loadCoursesFromCacheOrFirebase(model);
fetchDepartmentsAndLocations(model);
startAverageRatingListener(model);
// setting missing
// also save filters to local storage
//
const options = JSON.parse(localStorage.getItem("filterOptions"));
if (options) {
model.setFilterOptions(options);
}
if (!model?.currentSearchText) {
const search = localStorage.getItem("search");
if (search) {
model.setCurrentSearchText(search);
}
}
// automaticaly save filter and search to local storage whenever they change
reaction(
() => ({
filterOptions: JSON.stringify(model.filterOptions),
search: model.currentSearchText,
}),
({ filterOptions, search }) => {
localStorage.setItem("filterOptions", filterOptions);
localStorage.setItem("search", search);
}
);
/**
* Hook to start synchronization when user is authenticated.
*/
onAuthStateChanged(auth, (user) => {
if (user) {
model.setUser(user); // Set the user ID once authenticated
firebaseToModel(model); // Set up listeners for user-specific data
syncModelToFirebase(model); // Start syncing changes to Firebase
syncScrollPositionToFirebase(model);
} else {
model.setUser(null); // If no user, clear user-specific data
model.setReady();
}
});
}
// fetches all relevant information to create the model
async function firebaseToModel(model) {
const userRef = ref(db, `users/${model.user.uid}`);
onValue(userRef, async (snapshot) => {
if (!snapshot.exists()) return;
const data = snapshot.val();
// Use a transaction to ensure atomicity
await runTransaction(userRef, (currentData) => {
if (currentData) {
if (data?.favourites) model.setFavourite(data.favourites);
if (data?.currentSearchText)
model.setCurrentSearchText(data.currentSearchText);
// Add other fields as needed
}
return currentData; // Return the current data to avoid overwriting
});
});
model.setReady();
}
/**
* If the userid, favourites or search changes, sync to firebase.
* @param {object} model reactive model object
*/
export function syncModelToFirebase(model) {
reaction(
() => ({
userId: model?.user.uid,
favourites: toJS(model.favourites),
currentSearchText: toJS(model.currentSearchText),
}),
async ({ userId, favourites, currentSearchText }) => {
if (!userId) return;
const userRef = ref(db, `users/${userId}`);
await runTransaction(userRef, (currentData) => {
// Merge the new data with the existing data
return {
...currentData,
favourites,
currentSearchText,
};
}).catch((error) => {
console.error("Error syncing model to Firebase:", error);
});
}
);
}
/**
* Synchronizes the scroll position of the container to Firebase / local storage.
* @param {object} model
* @param {*} containerRef
* @returns
*/
export function syncScrollPositionToFirebase(model, containerRef) {
if (!containerRef?.current) return;
let lastSavedPosition = 0;
// const throttledSet = throttle((scrollPixel) => {
// if (model?.user?.uid) {
// const userRef = ref(db, `users/${model.user.uid}/scrollPosition`);
// set(userRef, scrollPixel).catch(console.error);
// }
// }, 500);
const handleScroll = () => {
const scrollTop = containerRef.current.scrollTop;
// make a 100px threshold
if (Math.abs(scrollTop - lastSavedPosition) < 100) return;
lastSavedPosition = scrollTop;
model.setScrollPosition(scrollTop);
localStorage.setItem("scrollPosition", scrollTop);
// throttledSet(scrollTop);
};
containerRef.current.addEventListener("scroll", handleScroll);
return () =>
containerRef.current?.removeEventListener("scroll", handleScroll);
}
/**
* Caches the courses to IndexedDB.
* @param {Array} courses The course array to save
* @param {number} timestamp The last update of the course array
*/
function saveCoursesToCache(courses, timestamp) {
const request = indexedDB.open("CourseDB", 1);
function validateCourseData(course) {
return course && typeof course === "object" && course.code;
}
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains("courses")) {
db.createObjectStore("courses", { keyPath: "id" });
}
if (!db.objectStoreNames.contains("metadata")) {
db.createObjectStore("metadata", { keyPath: "key" });
}
};
request.onsuccess = (event) => {
const db = event.target.result;
const tx = db.transaction(["courses", "metadata"], "readwrite");
const courseStore = tx.objectStore("courses");
const metaStore = tx.objectStore("metadata");
courseStore.clear();
courses
.filter(validateCourseData)
.forEach((course) => courseStore.put(course));
metaStore.put({ key: "timestamp", value: timestamp });
tx.oncomplete = () => console.log("Saved courses to IndexedDB");
tx.onerror = (e) => console.error("IndexedDB save error", e);
};
request.onerror = (e) => {
console.error("Failed to open IndexedDB", e);
};
}
async function updateLastUpdatedTimestamp() {
const timestampRef = ref(db, "metadata/lastUpdated");
await set(timestampRef, Date.now());
}
async function fetchLastUpdatedTimestamp() {
const timestampRef = ref(db, "metadata/lastUpdated");
const snapshot = await get(timestampRef);
return snapshot.exists() ? snapshot.val() : 0;
}
/**
* Admin function to add a course to the database.
* @param {Array} course
*/
export async function addCourse(course) {
if (!auth.currentUser) throw new Error("User must be authenticated");
if (!course?.code) throw new Error("Invalid course data");
if (!course || typeof course !== "object")
throw new Error("Invalid course object");
if (!course.code || typeof course.code !== "string")
throw new Error("Invalid course code");
const myRef = ref(db, `courses/${course.code}`);
await set(myRef, course);
updateLastUpdatedTimestamp();
}
/**
* Fetches all courses from the database.
* @returns {Array} Array of courses
*/
export async function fetchAllCourses() {
const myRef = ref(db, `courses`);
const snapshot = await get(myRef);
if (!snapshot.exists()) return [];
const value = snapshot.val(); // Firebase returns an object where keys are course IDs
const courses = [];
for (const id of Object.keys(value)) {
courses.push({ id, ...value[id] });
}
return courses;
}
/**
* Admin function to upload departments and locations to the database.
* @param {} departments
* @param {*} locations
* @returns
*/
export async function uploadDepartmentsAndLocations(departments, locations) {
if (departments) {
const departmentsRef = ref(db, "departments");
try {
await set(departmentsRef, departments);
} catch (error) {
console.error("Failed to upload departments:", error);
return false;
}
}
if (locations) {
const locationsRef = ref(db, "locations");
try {
await set(locationsRef, locations);
} catch (error) {
console.error("Failed to upload locations:", error);
return false;
}
}
return true;
}
/**
* Fetches departments and locations from the database.
* @param {object} model
* @returns {Array} Array of departments and locations
*/
export async function fetchDepartmentsAndLocations(model) {
const departmentsRef = ref(db, "departments");
const locationsRef = ref(db, "locations");
let snapshot = await get(departmentsRef);
if (snapshot.exists()) {
const value = snapshot.val();
const set = new Set();
for (const id of Object.keys(value)) {
set.add(value[id]);
}
model.setDepartments(Array.from(set));
}
snapshot = await get(locationsRef);
if (snapshot.exists()) {
const value = snapshot.val();
const set = new Set();
for (const id of Object.keys(value)) {
set.add(value[id]);
}
model.setLocations(Array.from(set));
}
}
/**
* Try to restore the courses from IndexedDB.
* @param {object} model
* @returns void
* @throws Error if IndexedDB is not available or no courses are found
*/
async function loadCoursesFromCacheOrFirebase(model) {
const firebaseTimestamp = await fetchLastUpdatedTimestamp();
const dbPromise = new Promise((resolve, reject) => {
const request = indexedDB.open("CourseDB", 1);
// check if courses and metadata dirs exist
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains("courses")) {
db.createObjectStore("courses", { keyPath: "id" });
}
if (!db.objectStoreNames.contains("metadata")) {
db.createObjectStore("metadata", { keyPath: "key" });
}
};
request.onsuccess = (event) => resolve(event.target.result);
request.onerror = (e) => reject(e);
});
try {
const db = await dbPromise;
const metaTx = db.transaction("metadata", "readonly");
const metaStore = metaTx.objectStore("metadata");
const metaReq = metaStore.get("timestamp");
const cachedTimestamp = await new Promise((resolve) => {
metaReq.onsuccess = () => resolve(metaReq.result?.value ?? 0);
metaReq.onerror = () => resolve(0);
});
if (cachedTimestamp === firebaseTimestamp) {
const courseTx = db.transaction("courses", "readonly");
const courseStore = courseTx.objectStore("courses");
const getAllReq = courseStore.getAll();
const cachedCourses = await new Promise((resolve) => {
getAllReq.onsuccess = () => resolve(getAllReq.result);
getAllReq.onerror = () => resolve([]);
});
if (!cachedCourses) throw new Error("No courses found in IndexedDB");
model.setCourses(cachedCourses);
return;
}
} catch (err) {
console.warn(
"IndexedDB unavailable, falling back Posting anonymously is possible. Firebase:",
err
);
}
// fallback: fetch from Firebase
const courses = await fetchAllCourses();
model.setCourses(courses);
saveCoursesToCache(courses, firebaseTimestamp);
}
/**
* Function to add a review for a course. The firebase rules disallow to have more than one course per person.
* Adding a review will automatically update the average rating of the course.
* @param {string} courseCode The course code
* @param {object} review The review object containing the review data
* @param {string} review.userName The name of the user
* @param {string} review.uid The user ID
* @param {number} review.timestamp The timestamp of the review
* @param {string} review.text The review text
* @param {number} review.overallRating The overall rating
* @param {number} review.difficultyRating The difficulty rating
* @param {string} review.professorName The name of the professor
* @param {string} review.grade The grade received
* @param {boolean} review.recommended Whether the course is recommended
* @throws {Error} If there is an error adding the review or updating the average rating
*/
export async function addReviewForCourse(courseCode, review) {
try {
const reviewsRef = ref(db, `reviews/${courseCode}/${review.uid}`);
await set(reviewsRef, review);
const updateCourseAvgRating = httpsCallable(
functions,
"updateCourseAvgRating"
);
await updateCourseAvgRating({ courseCode });
} catch (error) {
console.error(
"Error when adding a course to firebase or updating the average:",
error
);
}
}
/**
* Adding a course triggers a firebase function to update the average rating of the course.
* This function sets up a listener for the average rating of each course, to update the model onChange.
* It also fetches the initial average ratings if the model is not initialized.
* @param {object} model
*/
function startAverageRatingListener(model) {
const coursesRef = ref(db, "reviews");
// Step 1: One-time fetch if model.avgRating is not initialized
if (!model.avgRating || Object.keys(model.avgRating).length === 0) {
get(coursesRef).then((snapshot) => {
if (!snapshot.exists()) return;
const initialRatings = {};
snapshot.forEach((courseSnapshot) => {
const courseCode = courseSnapshot.key;
const avgRating = courseSnapshot.child("avgRating").val();
if (avgRating && Array.isArray(avgRating)) {
initialRatings[courseCode] = avgRating;
}
});
model.setAverageRatings(initialRatings);
});
}
// Step 2: listener for each courses avgRating
onChildAdded(coursesRef, (courseSnapshot) => {
const courseCode = courseSnapshot.key;
const avgRatingRef = ref(db, `reviews/${courseCode}/avgRating`);
onValue(avgRatingRef, (ratingSnapshot) => {
if (!ratingSnapshot.exists()) return;
const rating = ratingSnapshot.val();
if (typeof rating === "number") {
model.updateAverageRating(courseCode, rating);
}
});
});
onChildRemoved(coursesRef, (courseSnapshot) => {
const courseCode = courseSnapshot.key;
model.updateAverageRating(courseCode, null);
});
}
/**
* Fetches reviews for a specific course.
* @param {string} courseCode
* @returns
*/
export async function getReviewsForCourse(courseCode) {
const reviewsRef = ref(db, `reviews/${courseCode}`);
const snapshot = await get(reviewsRef);
if (!snapshot.exists()) return [];
const reviews = [];
snapshot.forEach((childSnapshot) => {
if (childSnapshot.key != "avgRating")
reviews.push({
id: childSnapshot.key,
...childSnapshot.val(),
});
});
return reviews;
}
/**
* Add a comment to a specific review
* @param {string} courseCode - Course code (e.g. "A11HIB")
* @param {string} reviewUserId - The user ID of the person who wrote the main review
* @param {Object} commentObj - Object with { userName, text, timestamp }
*/
export async function addCommentToReview(courseCode, reviewUserId, commentObj) {
const commentsRef = ref(db, `reviews/${courseCode}/${reviewUserId}/comments`);
await push(commentsRef, commentObj);
}
/**
* Get comments for a specific review
* @param {string} courseCode
* @param {string} reviewUserId
* @returns {Promise<Array<Object>>} Array of comments: { id, userName, text, timestamp }
*/
export async function getCommentsForReview(courseCode, reviewUserId) {
const commentsRef = ref(db, `reviews/${courseCode}/${reviewUserId}/comments`);
const snapshot = await get(commentsRef);
if (!snapshot.exists()) return [];
const comments = [];
snapshot.forEach((childSnapshot) => {
comments.push({
id: childSnapshot.key,
...childSnapshot.val(),
});
});
return comments;
}
/**
* Delete a review for a course (by userId).
* @param {string} courseCode
* @param {string} userId
*/
export async function deleteReview(courseCode, userId) {
const reviewRef = ref(db, `reviews/${courseCode}/${userId}`);
await set(reviewRef, null);
}
/**
* Delete a specific comment from a review.
* @param {string} courseCode
* @param {string} reviewUserId - UID of the review's author
* @param {string} commentId - ID of the comment (Firebase push key)
*/
export async function deleteComment(courseCode, reviewUserId, commentId) {
const commentRef = ref(
db,
`reviews/${courseCode}/${reviewUserId}/comments/${commentId}`
);
await set(commentRef, null);
}
// Delete a review or comment by its ID
export const deleteReviewById = async (
courseCode,
commentId,
parentId = null
) => {
const db = getDatabase();
if (!parentId) {
// Top-level review
const reviewRef = ref(db, `reviews/${courseCode}/${commentId}`);
await remove(reviewRef);
} else {
// Nested reply - remove it from the parent's replies array
const parentRef = ref(db, `reviews/${courseCode}/${parentId}`);
const snapshot = await get(parentRef);
if (snapshot.exists()) {
const parentData = snapshot.val();
const replies = parentData.replies || [];
const updatedReplies = replies.filter((r) => r.id !== commentId);
await update(parentRef, { replies: updatedReplies });
}
}
};