@@ -36,6 +36,7 @@ type ArticleData = {
36
36
label : string ;
37
37
slug : string ;
38
38
authorUID : string ;
39
+ publish : boolean ; // Added publish field
39
40
} ;
40
41
41
42
// Function to fetch author data
@@ -50,20 +51,53 @@ async function getAuthorData(slug: string) {
50
51
51
52
const authorData = authorSnapshot . docs [ 0 ] . data ( ) as AuthorData ;
52
53
53
- const articlesQuery = query (
54
- collection ( db , "articles" ) ,
55
- where ( "authorUID" , "==" , authorData . uid ) ,
56
- orderBy ( "date" , "desc" )
57
- ) ;
58
- const articlesSnapshot = await getDocs ( articlesQuery ) ;
59
-
60
- const articles = articlesSnapshot . docs . map ( ( doc ) => ( {
61
- uid : doc . id ,
62
- ...doc . data ( ) ,
63
- date : doc . data ( ) . date ?. toDate ( ) || new Date ( ) ,
64
- } ) ) as ArticleData [ ] ;
65
-
66
- return { author : authorData , articles } ;
54
+ // Approach 1: Try with composite index (if you've created it in Firebase console)
55
+ try {
56
+ const articlesQuery = query (
57
+ collection ( db , "articles" ) ,
58
+ where ( "authorUID" , "==" , authorData . uid ) ,
59
+ where ( "publish" , "==" , true ) ,
60
+ orderBy ( "date" , "desc" )
61
+ ) ;
62
+ const articlesSnapshot = await getDocs ( articlesQuery ) ;
63
+
64
+ const articles = articlesSnapshot . docs . map ( ( doc ) => ( {
65
+ uid : doc . id ,
66
+ ...doc . data ( ) ,
67
+ date : doc . data ( ) . date ?. toDate ( ) || new Date ( ) ,
68
+ } ) ) as ArticleData [ ] ;
69
+
70
+ return { author : authorData , articles } ;
71
+ }
72
+ // Fallback approach if composite index isn't available
73
+ catch ( error ) {
74
+ console . log ( "Using fallback approach for querying articles:" , error ) ;
75
+
76
+ // First get all articles by this author
77
+ const articlesQuery = query (
78
+ collection ( db , "articles" ) ,
79
+ where ( "authorUID" , "==" , authorData . uid ) ,
80
+ orderBy ( "date" , "desc" )
81
+ ) ;
82
+ const articlesSnapshot = await getDocs ( articlesQuery ) ;
83
+
84
+ // Then filter for published articles client-side
85
+ const articles = articlesSnapshot . docs
86
+ . map ( ( doc ) => {
87
+ const data = doc . data ( ) ;
88
+ return {
89
+ uid : doc . id ,
90
+ ...data ,
91
+ date : data . date && typeof data . date . toDate === 'function'
92
+ ? data . date . toDate ( )
93
+ : ( data . date instanceof Date ? data . date : new Date ( ) ) ,
94
+ publish : data . publish ?? false
95
+ } as ArticleData ;
96
+ } )
97
+ . filter ( article => article . publish === true ) ;
98
+
99
+ return { author : authorData , articles } ;
100
+ }
67
101
} catch ( error ) {
68
102
console . error ( "Error fetching author details:" , error ) ;
69
103
return null ;
@@ -83,14 +117,10 @@ const SOCIAL_ICONS: { [key: string]: any } = {
83
117
discord : RiDiscordFill ,
84
118
} ;
85
119
86
- // **Non-async page component**
87
- export default function Page ( { params } : { params : { author : string } } ) {
88
- return < AuthorPage authorSlug = { params . author } /> ;
89
- }
90
-
91
- // **Async server component**
92
- async function AuthorPage ( { authorSlug } : { authorSlug : string } ) {
93
- const data = await getAuthorData ( authorSlug ) ;
120
+ // **Async page component**
121
+ export default async function Page ( { params } : { params : Promise < { author : string } > } ) {
122
+ const { author } = await params ; // Await the params object
123
+ const data = await getAuthorData ( author ) ;
94
124
95
125
if ( ! data ) {
96
126
return < div className = "p-8" > Author not found</ div > ;
@@ -117,7 +147,7 @@ async function AuthorPage({ authorSlug }: { authorSlug: string }) {
117
147
{ /* **Author Profile Section** */ }
118
148
< div className = "w-fit" >
119
149
< img
120
- src = { authorData . avatar }
150
+ src = { authorData . avatar || "/default-avatar.png" }
121
151
alt = { authorData . imgAlt }
122
152
className = "w-full max-w-[300px] h-auto rounded-full"
123
153
/>
@@ -140,20 +170,20 @@ async function AuthorPage({ authorSlug }: { authorSlug: string }) {
140
170
{ /* **Author Articles** */ }
141
171
< div className = "pb-12 md:pb-48" >
142
172
< h2 className = "text-blog-subheading mt-[9.5rem] pt-12 pb-12 md:pb-24" >
143
- Articles by { authorData . name }
173
+ Posts by { authorData . name }
144
174
</ h2 >
145
175
< AuthorArticles articles = { articles } />
146
176
</ div >
147
177
</ main >
148
178
) ;
149
179
}
150
180
151
- // **Component to Render Author’ s Articles**
181
+ // **Component to Render Author' s Articles**
152
182
function AuthorArticles ( { articles } : { articles : ArticleData [ ] } ) {
153
183
if ( articles . length === 0 ) {
154
184
return (
155
185
< div className = "p-8 text-center" >
156
- < p > No articles found for this author</ p >
186
+ < p className = "font-medium" > No Posts found for this author</ p >
157
187
</ div >
158
188
) ;
159
189
}
0 commit comments