From 380052896d4bca2789ab2842a9e547b60421a353 Mon Sep 17 00:00:00 2001 From: YoungHypo Date: Fri, 6 Dec 2024 10:05:07 -0800 Subject: [PATCH] update logic of profile's posts --- react/src/app/user/[id]/page.tsx | 153 ++++++++++++++++--------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/react/src/app/user/[id]/page.tsx b/react/src/app/user/[id]/page.tsx index 3f0dbda..828158b 100644 --- a/react/src/app/user/[id]/page.tsx +++ b/react/src/app/user/[id]/page.tsx @@ -1,32 +1,33 @@ 'use client'; import { useEffect, useState } from 'react'; -import Layout from '@/components/layout/Layout'; import { Post } from '@/types'; import { postService } from '@/services/post'; -import { UserCircleIcon, CalendarIcon } from '@heroicons/react/24/outline'; +import { CalendarIcon } from '@heroicons/react/24/outline'; +import { UserCircleIcon } from '@heroicons/react/24/solid'; import { formatDistanceToNow } from 'date-fns'; import { enUS } from 'date-fns/locale'; -import PostList from '@/components/post/PostList'; -import { Tab } from '@headlessui/react'; -import clsx from 'clsx'; +import Layout from '@/components/layout/Layout'; +import PostCard from '@/components/post/PostCard'; import { useParams } from 'next/navigation'; +type Tab = 'posts' | 'likes'; + const UserProfilePage = () => { const { id } = useParams(); - const [userPosts, setUserPosts] = useState([]); - const [likedPosts, setLikedPosts] = useState([]); - const [isLoading, setIsLoading] = useState(true); - const [error, setError] = useState(''); - const [currentUserId, setCurrentUserId] = useState(null); + const [currentUserId, setCurrentUserId] = useState(null); const [username, setUsername] = useState(null); const [joinedDate, setJoinedDate] = useState(null); + const [activeTab, setActiveTab] = useState('posts'); + const [posts, setPosts] = useState([]); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(null); useEffect(() => { const currentId = parseInt(localStorage.getItem('userId') || '0'); setCurrentUserId(currentId); - const cuurentUserName = localStorage.getItem('username'); - setUsername(cuurentUserName); + const currentUserName = localStorage.getItem('username'); + setUsername(currentUserName); const joinedAt = localStorage.getItem('joined_at'); setJoinedDate(joinedAt); @@ -35,26 +36,28 @@ const UserProfilePage = () => { } }, [id]); + // listen to activeTab change + useEffect(() => { + if (id) { + fetchUserData(id as number); + } + }, [activeTab]); + const fetchUserData = async (userId: number) => { setIsLoading(true); + setError(null); try { - const [posts, liked] = await Promise.all([ - postService.getUserPosts(userId), - postService.getLikedPosts(userId) - ]); + const data = activeTab === 'posts' + ? await postService.getUserPosts(userId) + : await postService.getLikedPosts(userId); - const postsWithIntLikes = posts.map(post => ({ + // make sure likes is an array of integers + const postsWithIntLikes = data.map((post: Post) => ({ ...post, likes: post.likes.map(like => parseInt(like)) })); - const likedWithIntLikes = liked.map(post => ({ - ...post, - likes: post.likes.map(like => parseInt(like)) - })); - - setUserPosts(postsWithIntLikes); - setLikedPosts(likedWithIntLikes); + setPosts(postsWithIntLikes); } catch (err: any) { setError('Failed to fetch user data'); console.error('Error fetching user data:', err); @@ -62,27 +65,24 @@ const UserProfilePage = () => { setIsLoading(false); } }; + const handleTabChange = (tab: Tab) => { + setActiveTab(tab); + }; const handlePostUpdate = (updatedPost: Post) => { - setUserPosts(posts => posts.map(post => - post.id === updatedPost.id ? updatedPost : post - )); - setLikedPosts(posts => posts.map(post => + setPosts(posts.map((post: Post) => post.id === updatedPost.id ? updatedPost : post )); }; const handlePostDelete = (postId: number) => { - setUserPosts(posts => posts.filter(post => post.id !== postId)); - setLikedPosts(posts => posts.filter(post => post.id !== postId)); + setPosts(posts.filter((post: Post) => post.id !== postId)); }; - if (isLoading) { + if (error) { return ( -
-
Loading...
-
+
{error}
); } @@ -99,9 +99,9 @@ const UserProfilePage = () => {
- Joined {formatDistanceToNow(new Date(joinedDate), { - locale: enUS, - addSuffix: true + Joined {formatDistanceToNow(new Date(joinedDate), { + locale: enUS, + addSuffix: true })}
@@ -110,47 +110,50 @@ const UserProfilePage = () => { - - - clsx( - 'flex-1 py-4 text-sm text-center focus:outline-none transition-all', - selected - ? 'text-blue-500 border-b-2 border-blue-500 font-bold' - : 'text-gray-500 hover:text-gray-300 font-normal' - )}> - Posts - - clsx( - 'flex-1 py-4 text-sm text-center focus:outline-none transition-all', - selected - ? 'text-blue-500 border-b-2 border-blue-500 font-bold' - : 'text-gray-500 hover:text-gray-300 font-normal' - )}> - Likes - - - - - - - - + + + + + {isLoading ? ( +
+
Loading...
+
+ ) : ( +
+ {posts.map((post) => ( + - - - + ))} + {posts.length === 0 && ( +
+ {activeTab === 'posts' ? 'No posts yet' : 'No liked posts yet'} +
+ )} +
+ )} ); -}; +} -export default UserProfilePage; \ No newline at end of file +export default UserProfilePage; \ No newline at end of file