@@ -412,9 +412,10 @@ export const blogsAPI = {
412412 // Get all public published blogs
413413 getPublishedBlogs : async ( ) => {
414414 try {
415- const q = query ( collection ( db , 'blogs' ) , where ( 'status' , '==' , 'published' ) , orderBy ( 'publishedAt' , 'desc' ) ) ;
415+ const q = query ( collection ( db , 'blogs' ) , where ( 'status' , '==' , 'published' ) ) ;
416416 const querySnapshot = await getDocs ( q ) ;
417- return querySnapshot . docs . map ( doc => ( { id : doc . id , ...doc . data ( ) } ) ) ;
417+ const blogs = querySnapshot . docs . map ( doc => ( { id : doc . id , ...doc . data ( ) } ) ) ;
418+ return blogs . sort ( ( a , b ) => ( b . publishedAt ?. seconds || 0 ) - ( a . publishedAt ?. seconds || 0 ) ) ;
418419 } catch ( error ) {
419420 console . error ( "Error fetching blogs:" , error ) ;
420421 return [ ] ;
@@ -425,14 +426,15 @@ export const blogsAPI = {
425426 getBlogs : async ( authorId = null , status = null ) => {
426427 try {
427428 let q = collection ( db , 'blogs' ) ;
428- const constraints = [ orderBy ( 'createdAt' , 'desc' ) ] ;
429+ const constraints = [ ] ;
429430
430431 if ( authorId ) constraints . push ( where ( 'authorId' , '==' , authorId ) ) ;
431432 if ( status ) constraints . push ( where ( 'status' , '==' , status ) ) ;
432433
433434 q = query ( q , ...constraints ) ;
434435 const querySnapshot = await getDocs ( q ) ;
435- return querySnapshot . docs . map ( doc => ( { id : doc . id , ...doc . data ( ) } ) ) ;
436+ const blogs = querySnapshot . docs . map ( doc => ( { id : doc . id , ...doc . data ( ) } ) ) ;
437+ return blogs . sort ( ( a , b ) => ( b . createdAt ?. seconds || 0 ) - ( a . createdAt ?. seconds || 0 ) ) ;
436438 } catch ( error ) {
437439 console . error ( "Error fetching blogs:" , error ) ;
438440 return [ ] ;
0 commit comments