@@ -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
175131function 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+
351352export async function getReviewsForCourse ( courseCode ) {
352353 const reviewsRef = ref ( db , `reviews/${ courseCode } ` ) ;
353354 const snapshot = await get ( reviewsRef ) ;
0 commit comments