Skip to content

Commit 8f999d1

Browse files
authored
Merge pull request #51 from codeunia-dev/likefunctiontoblogpage
Feature: Enhance blog post pages with like counts and improved like button functionality
2 parents c9f54d4 + 02766b4 commit 8f999d1

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

app/blog/[slug]/page.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,15 @@ import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip
1717
import Header from "@/components/header";
1818
import Footer from "@/components/footer";
1919

20-
function LikeButton({ slug, isAuthenticated }: { slug: string, isAuthenticated: boolean }) {
21-
const [likeCount, setLikeCount] = useState(0);
22-
const [likedByUser, setLikedByUser] = useState(false);
23-
const [loading, setLoading] = useState(true);
24-
25-
useEffect(() => {
26-
async function fetchLikeData() {
27-
setLoading(true);
28-
try {
29-
const res = await fetch(`/api/blog/${slug}/like`);
30-
if (res.ok) {
31-
const data = await res.json();
32-
setLikeCount(data.count);
33-
setLikedByUser(data.likedByUser);
34-
}
35-
} catch {}
36-
setLoading(false);
37-
}
38-
if (slug) fetchLikeData();
39-
}, [slug]);
20+
function LikeButton({ slug, isAuthenticated, likeCount, setLikeCount, likedByUser, setLikedByUser }: {
21+
slug: string,
22+
isAuthenticated: boolean,
23+
likeCount: number,
24+
setLikeCount: React.Dispatch<React.SetStateAction<number>>,
25+
likedByUser: boolean,
26+
setLikedByUser: React.Dispatch<React.SetStateAction<boolean>>
27+
}) {
28+
const [loading, setLoading] = useState(false);
4029

4130
const handleLike = async () => {
4231
if (!isAuthenticated) {
@@ -93,6 +82,8 @@ export default function BlogPostPage() {
9382
const [isLoading, setIsLoading] = useState(true)
9483
const [post, setPost] = useState<BlogPost | null>(null)
9584
const [fetchError, setFetchError] = useState<string | null>(null)
85+
const [likeCount, setLikeCount] = useState(0);
86+
const [likedByUser, setLikedByUser] = useState(false);
9687
const params = useParams()
9788

9889
const slug = params?.slug as string
@@ -130,6 +121,18 @@ export default function BlogPostPage() {
130121
if (slug) fetchPost()
131122
}, [slug])
132123

124+
useEffect(() => {
125+
async function fetchLikeData() {
126+
const res = await fetch(`/api/blog/${slug}/like`);
127+
if (res.ok) {
128+
const data = await res.json();
129+
setLikeCount(data.count);
130+
setLikedByUser(data.likedByUser);
131+
}
132+
}
133+
if (slug) fetchLikeData();
134+
}, [slug]);
135+
133136
const getCategoryColor = (category: string) => {
134137
switch (category) {
135138
case "Frontend":
@@ -213,7 +216,7 @@ export default function BlogPostPage() {
213216
<Button variant="ghost" size="sm" className="hover:bg-primary/10 transition-colors">
214217
<Share2 className="h-4 w-4" />
215218
</Button>
216-
<LikeButton slug={slug} isAuthenticated={isAuthenticated} />
219+
<LikeButton slug={slug} isAuthenticated={isAuthenticated} likeCount={likeCount} setLikeCount={setLikeCount} likedByUser={likedByUser} setLikedByUser={setLikedByUser} />
217220
</motion.div>
218221
</div>
219222
</div>
@@ -271,7 +274,7 @@ export default function BlogPostPage() {
271274
</div>
272275
<div className="flex items-center space-x-2 bg-background/80 backdrop-blur-sm px-3 py-2 rounded-full">
273276
<Heart className="h-4 w-4" />
274-
<span>{post?.likes} likes</span>
277+
<span>{likeCount} likes</span>
275278
</div>
276279
</div>
277280
</motion.div>

app/blog/page.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,26 @@ export default function BlogPage() {
4141
setFetchError('Failed to fetch blog posts.')
4242
setBlogPosts([])
4343
} else {
44-
45-
const posts = data.map((post: BlogPost) => ({
44+
// Fetch like counts for all posts
45+
const postsWithTags = data.map((post: BlogPost) => ({
4646
...post,
4747
tags: Array.isArray(post.tags)
4848
? post.tags
4949
: (typeof post.tags === 'string' && post.tags
5050
? (post.tags as string).split(',').map((t) => t.trim())
5151
: []),
5252
}))
53+
// Fetch like counts in parallel
54+
const likeCounts = await Promise.all(
55+
postsWithTags.map(async (post) => {
56+
const { count } = await supabase
57+
.from('blog_likes')
58+
.select('*', { count: 'exact', head: true })
59+
.eq('blog_slug', post.slug)
60+
return count ?? 0
61+
})
62+
)
63+
const posts = postsWithTags.map((post, i) => ({ ...post, likes: likeCounts[i] }))
5364
setBlogPosts(posts)
5465
}
5566
setIsLoading(false)

0 commit comments

Comments
 (0)