@@ -2,6 +2,8 @@ import { initializeApp } from "firebase/app";
22import { getAuth , GoogleAuthProvider , onAuthStateChanged } from "firebase/auth" ;
33import { get , getDatabase , ref , set , onValue , push } from "firebase/database" ;
44import { reaction , toJS } from "mobx" ;
5+ import throttle from "lodash.throttle" ;
6+
57// Your web app's Firebase configuration
68const firebaseConfig = {
79 apiKey : "AIzaSyCBckVI9nhAP62u5jZJW3F4SLulUv7znis" ,
@@ -24,53 +26,54 @@ googleProvider.addScope("email");
2426let noUpload = false ;
2527
2628export function connectToFirebase ( model ) {
27- loadCoursesFromCacheOrFirebase ( model ) ;
29+ loadCoursesFromCacheOrFirebase ( model ) ;
2830 onAuthStateChanged ( auth , ( user ) => {
2931 if ( user ) {
3032 model . setUser ( user ) ; // Set the user ID once authenticated
31- firebaseToModel ( model ) ; // Set up listeners for user-specific data
32- syncModelToFirebase ( model ) ; // Start syncing changes to Firebase
33+ firebaseToModel ( model ) ; // Set up listeners for user-specific data
34+ syncModelToFirebase ( model ) ; // Start syncing changes to Firebase
35+ syncScrollPositionToFirebase ( model ) ;
3336 } else {
34- model . setUser ( null ) ; // If no user, clear user-specific data
37+ model . setUser ( null ) ; // If no user, clear user-specific data
3538 }
3639 } ) ;
3740}
3841
3942// fetches all relevant information to create the model
4043async function firebaseToModel ( model ) {
41- if ( ! model . user )
42- return ;
44+ if ( ! model . user ) return ;
4345 const userRef = ref ( db , `users/${ model . user . uid } ` ) ;
4446 onValue ( userRef , ( snapshot ) => {
45- if ( ! snapshot . exists ( ) )
46- return ;
47+ if ( ! snapshot . exists ( ) ) return ;
4748 const data = snapshot . val ( ) ;
4849 noUpload = true ;
49- if ( data . favourites )
50- model . setFavourite ( data . favourites ) ;
51- // if (data.currentSearch)
52- // model.setCurrentSearch(data.currentSearch);
50+ if ( data . favourites ) model . setFavourite ( data . favourites ) ;
51+ if ( data . currentSearchText )
52+ model . setCurrentSearchText ( data . currentSearchText ) ;
53+ if ( data . scrollPosition )
54+ model . setScrollPosition ( data . scrollPosition ) ;
55+ // if (data.currentSearch)
56+ // model.setCurrentSearch(data.currentSearch);
5357 noUpload = false ;
5458 } ) ;
5559}
5660
57-
5861export function syncModelToFirebase ( model ) {
5962 reaction (
6063 ( ) => ( {
6164 userId : model ?. user . uid ,
6265 favourites : toJS ( model . favourites ) ,
66+ currentSearchText : toJS ( model . currentSearchText ) ,
6367 // currentSearch: toJS(model.currentSearch),
6468 // Add more per-user attributes here
6569 } ) ,
6670 // eslint-disable-next-line no-unused-vars
67- ( { userId, favourites, currentSearch } ) => {
68- if ( noUpload || ! userId )
69- return ;
71+ ( { userId, favourites, currentSearchText } ) => {
72+ if ( noUpload || ! userId ) return ;
7073 const userRef = ref ( db , `users/${ userId } ` ) ;
7174 const dataToSync = {
7275 favourites,
73- //currentSearch ,
76+ currentSearchText ,
7477 } ;
7578
7679 set ( userRef , dataToSync )
@@ -80,6 +83,34 @@ export function syncModelToFirebase(model) {
8083 ) ;
8184}
8285
86+ export function syncScrollPositionToFirebase ( model , containerRef ) {
87+ if ( ! containerRef ?. current ) return ;
88+ let lastSavedPosition = 0 ;
89+
90+ // const throttledSet = throttle((scrollPixel) => {
91+ // if (model?.user?.uid) {
92+ // const userRef = ref(db, `users/${model.user.uid}/scrollPosition`);
93+ // set(userRef, scrollPixel).catch(console.error);
94+ // }
95+ // }, 500);
96+
97+ const handleScroll = ( ) => {
98+ const scrollTop = containerRef . current . scrollTop ;
99+ // make a 100px threshold
100+ if ( Math . abs ( scrollTop - lastSavedPosition ) < 100 )
101+ return ;
102+
103+ lastSavedPosition = scrollTop ;
104+ model . setScrollPosition ( scrollTop ) ;
105+ localStorage . setItem ( "scrollPosition" , scrollTop ) ;
106+ // throttledSet(scrollTop);
107+ } ;
108+
109+ containerRef . current . addEventListener ( 'scroll' , handleScroll ) ;
110+ return ( ) => containerRef . current ?. removeEventListener ( 'scroll' , handleScroll ) ;
111+ }
112+
113+
83114function saveCoursesInChunks ( courses , timestamp ) {
84115 const parts = 3 ; // Adjust this based on course size
85116 const chunkSize = Math . ceil ( courses . length / parts ) ;
@@ -170,23 +201,20 @@ export async function saveJSONCoursesToFirebase(model, data) {
170201 } ) ;
171202}
172203
173-
174204export async function addReviewForCourse ( courseCode , review ) {
175- try {
176- const reviewsRef = ref ( db , `reviews/${ courseCode } ` ) ;
177- const newReviewRef = push ( reviewsRef ) ;
178- await set ( newReviewRef , review ) ;
179- } catch ( error ) {
180- console . error ( "Error when adding a course to firebase:" , error ) ;
205+ try {
206+ const reviewsRef = ref ( db , `reviews/${ courseCode } ` ) ;
207+ const newReviewRef = push ( reviewsRef ) ;
208+ await set ( newReviewRef , review ) ;
209+ } catch ( error ) {
210+ console . error ( "Error when adding a course to firebase:" , error ) ;
181211 }
182212}
183213
184-
185214export async function getReviewsForCourse ( courseCode ) {
186215 const reviewsRef = ref ( db , `reviews/${ courseCode } ` ) ;
187216 const snapshot = await get ( reviewsRef ) ;
188217 if ( ! snapshot . exists ( ) ) return [ ] ;
189-
190218 const reviews = [ ] ;
191219 snapshot . forEach ( childSnapshot => {
192220 reviews . push ( {
@@ -195,9 +223,4 @@ export async function getReviewsForCourse(courseCode) {
195223 } ) ;
196224 } ) ;
197225 return reviews ;
198- }
199-
200-
201-
202-
203-
226+ }
0 commit comments