@@ -29,7 +29,7 @@ interface BlogFormData {
2929 tags : string ;
3030 featured : boolean ;
3131 image : string ;
32- views : string ;
32+ views ? : string ; // Optional since it's managed by the views API
3333 likes ?: number ; // Optional since it's calculated from blog_likes table
3434}
3535
@@ -66,8 +66,7 @@ const getEmptyPost = (): BlogFormData => ({
6666 tags : "" ,
6767 featured : false ,
6868 image : "" ,
69- views : "0" ,
70- likes : 0 , // This will be calculated automatically from blog_likes table
69+ // views and likes are managed automatically
7170} )
7271
7372// custom hooks
@@ -98,8 +97,8 @@ const useBlogPosts = () => {
9897 }
9998
10099 if ( postsData ) {
101- // Fetch real like counts for each blog post
102- const postsWithRealLikes = await Promise . all (
100+ // Fetch real like counts and ensure views are up-to-date for each blog post
101+ const postsWithRealCounts = await Promise . all (
103102 postsData . map ( async ( post ) => {
104103 // Get the real like count from blog_likes table
105104 const { count : likeCount , error : likeError } = await supabase
@@ -109,14 +108,28 @@ const useBlogPosts = () => {
109108
110109 if ( likeError ) {
111110 console . error ( 'Error fetching likes for post:' , post . title , likeError )
112- return { ...post , likes : 0 }
113111 }
114112
115- return { ...post , likes : likeCount || 0 }
113+ // Get the most up-to-date view count from blogs table
114+ const { data : viewData , error : viewError } = await supabase
115+ . from ( 'blogs' )
116+ . select ( 'views' )
117+ . eq ( 'id' , post . id )
118+ . single ( )
119+
120+ if ( viewError ) {
121+ console . error ( 'Error fetching views for post:' , post . title , viewError )
122+ }
123+
124+ return {
125+ ...post ,
126+ likes : likeCount || 0 ,
127+ views : viewData ?. views ?. toString ( ) || post . views || '0'
128+ }
116129 } )
117130 )
118131
119- setBlogPosts ( postsWithRealLikes as BlogPost [ ] )
132+ setBlogPosts ( postsWithRealCounts as BlogPost [ ] )
120133 }
121134 } catch ( err ) {
122135 const errorMessage = err instanceof Error ? err . message : "Failed to fetch blog posts"
@@ -337,16 +350,7 @@ const BlogPostForm = ({
337350 </ div >
338351 </ div >
339352
340- < div className = "grid gap-2" >
341- < Label htmlFor = "views" > Views</ Label >
342- < Input
343- id = "views"
344- placeholder = "e.g., 0"
345- value = { formData . views }
346- onChange = { handleInputChange ( 'views' ) }
347- className = "text-sm"
348- />
349- </ div >
353+ { /* Views are managed automatically by the views API */ }
350354
351355 < div className = "grid gap-2" >
352356 < Label htmlFor = "tags" > Tags</ Label >
@@ -497,7 +501,6 @@ export default function AdminBlogPage() {
497501 tags : parseTags ( formData . tags ) ,
498502 featured : Boolean ( formData . featured ) ,
499503 image : formData . image . trim ( ) ,
500- views : formData . views . toString ( ) ,
501504 // Generate slug from title if needed
502505 slug : formData . title . trim ( ) . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] + / g, '-' ) . replace ( / - + / g, '-' ) . replace ( / ^ - | - $ / g, '' )
503506 }
@@ -553,7 +556,6 @@ export default function AdminBlogPage() {
553556 tags : parseTags ( formData . tags ) ,
554557 featured : Boolean ( formData . featured ) ,
555558 image : formData . image . trim ( ) ,
556- views : formData . views . toString ( ) ,
557559 }
558560
559561 console . log ( 'Update data:' , updateData ) // Debug log
0 commit comments