From f641cde3bfbb186dca0632d47e92e3265b8d2e2b Mon Sep 17 00:00:00 2001 From: darshan2006-op Date: Tue, 1 Jul 2025 01:14:55 +0530 Subject: [PATCH 1/2] added like functionality --- app/api/blogs/getAllBlogs.ts | 1 + app/api/blogs/toggleLike.ts | 5 +++- app/blog/[id]/page.tsx | 39 +++++++++++++++++--------- app/store/features/blogs/blogsSlice.ts | 10 +++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app/api/blogs/getAllBlogs.ts b/app/api/blogs/getAllBlogs.ts index d88191f..30f2de9 100644 --- a/app/api/blogs/getAllBlogs.ts +++ b/app/api/blogs/getAllBlogs.ts @@ -6,5 +6,6 @@ import axios from 'axios'; export default async function getAllBlogs(): Promise { const url = `${config.backendUrl}blog/`; const res = await axios.get(url); + console.log(res.data.data.blogs) return res.data.data.blogs; } diff --git a/app/api/blogs/toggleLike.ts b/app/api/blogs/toggleLike.ts index f09a2bf..567a724 100644 --- a/app/api/blogs/toggleLike.ts +++ b/app/api/blogs/toggleLike.ts @@ -13,5 +13,8 @@ export default async function toggleLike(token: string, id: string) { }, } ); - return res.data.blog; + + console.log(res.data) + + return res.data.liked; } diff --git a/app/blog/[id]/page.tsx b/app/blog/[id]/page.tsx index ead5cc0..98f4c9f 100644 --- a/app/blog/[id]/page.tsx +++ b/app/blog/[id]/page.tsx @@ -25,9 +25,12 @@ import { BlogPost } from '@/types/blog'; // import postsData from '@/data/post.json'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; -import { useSelector } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { RootState } from '@/app/store/store'; import getBlogById from '@/app/api/blogs/getBlogById'; +import toggleLike from '@/app/api/blogs/toggleLike'; +import useAuth from '@/hooks/auth/useAuth'; +import { refreshBlog } from '@/app/store/features/blogs/blogsSlice'; export default function BlogDetailPage() { const params = useParams(); @@ -52,7 +55,9 @@ export default function BlogDetailPage() { { user: string; text: string; date: string }[] >([]); const [newComment, setNewComment] = useState(''); - + const { getToken } = useAuth() + const dispatch = useDispatch() + // Get all blogs from Redux store // eslint-disable-next-line react-hooks/exhaustive-deps const allBlogs = useSelector((state: RootState) => state.blogs.blogs) || []; @@ -128,16 +133,18 @@ export default function BlogDetailPage() { return filtered; }, [post, allBlogs]); + const fetchData = async (id: string) => { + const foundPost = await getBlogById(id); + if (foundPost) { + setPost(foundPost); + dispatch(refreshBlog(foundPost)) + const wordCount = foundPost.excerpt.split(' ').length * 10; // Simulated content length + setReadingTime(Math.ceil(wordCount / 200)); + } + }; + useEffect(() => { - const fetchData = async () => { - const foundPost = await getBlogById(params.id?.toString() || ''); - if (foundPost) { - setPost(foundPost); - const wordCount = foundPost.excerpt.split(' ').length * 10; // Simulated content length - setReadingTime(Math.ceil(wordCount / 200)); - } - }; - fetchData(); + fetchData(params.id?.toString() || ''); }, [params.id]); // Handle text selection in the article @@ -185,6 +192,12 @@ export default function BlogDetailPage() { }; }, []); + const handleLike = async () => { + const liked = await toggleLike((await getToken())!, params.id?.toString() || '') + await fetchData(params.id?.toString() || ''); + setIsLiked(liked); + } + const handleTextShare = (platform: string) => { if (!selectedText) return; const url = window.location.href; @@ -499,7 +512,7 @@ export default function BlogDetailPage() {