diff --git a/src/pages/MyPage/dto.tsx b/src/pages/MyPage/dto.tsx deleted file mode 100644 index 30e058b1..00000000 --- a/src/pages/MyPage/dto.tsx +++ /dev/null @@ -1,40 +0,0 @@ -export interface PostData { - id: string; - imgUrl: string; - likes: number; - comments: number; -} - -// src/pages/MyPage/dto.ts - -export interface UserResponse { - id: number; - name: string; - email: string; - nickname: string | null; - phoneNumber: string | null; - profilePictureUrl: string; - bio: string | null; - joinedAt: string; -} - -// API 응답에 맞는 타입 정의 -export interface PostItem { - postId: number; - userId: number; - likes: number; - firstPhoto: string; - isRepresentative: boolean; - commentsCount?: number; // Optional since it might not be included for other users -} - -export interface PostsResponse { - isSuccess: boolean; - code: number; - message: string; - result: { - totalPosts: number; - totalLikes: number; - posts: PostItem[]; - }; -} diff --git a/src/pages/MyPage/index.tsx b/src/pages/MyPage/index.tsx index b296fd79..b54fa878 100644 --- a/src/pages/MyPage/index.tsx +++ b/src/pages/MyPage/index.tsx @@ -1,19 +1,19 @@ import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { - ProfileContainer, - Header, - StatsContainer, - Stat, - StatNumber, - StatLabel, - PostsContainer, - AddButton, -} from './styles'; + ProfileContainer, + Header, + StatsContainer, + Stat, + StatNumber, + StatLabel, + PostsContainer, + AddButton, +} from "./styles"; import { OODDFrame } from '../../components/Frame/Frame'; import NavbarProfile from '../../components/NavbarProfile'; import NavBar from '../../components/NavBar'; -import ButtonSecondary from './ButtonSecondary'; +import ButtonSecondary from "./ButtonSecondary"; import PostItem from '../../components/PostItem'; import imageBasic from '../../assets/default/defaultProfile.svg'; import Loading from '../../components/Loading'; @@ -26,141 +26,128 @@ import insta from '../../assets/default/insta.svg'; import photo from '../../assets/default/photo.svg'; import UserProfile from '../../components/UserProfile'; -import { getUserPostListApi } from '../../apis/post'; -import { UserPostSummary } from '../../apis/post/dto'; +import { getUserPostListApi } from "../../apis/post"; +import { UserPostSummary } from "../../apis/post/dto"; const MyPage: React.FC = () => { - const [isLoading, setIsLoading] = useState(true); - const [isBottomSheetOpen, setIsBottomSheetOpen] = useState(false); - const [posts, setPosts] = useState([]); - const [totalStats, setTotalStats] = useState<{ - totalPostsCount: number; - totalPostCommentsCount: number; - totalPostLikesCount: number; - }>(); - const navigate = useNavigate(); + const [isLoading, setIsLoading] = useState(true); + const [isBottomSheetOpen, setIsBottomSheetOpen] = useState(false); + const [posts, setPosts] = useState([]); + const [totalPosts, setTotalPosts] = useState(0); + const navigate = useNavigate(); - const bottomSheetMenuProps: BottomSheetMenuProps = { - items: [ - { - text: '인스타 피드 가져오기', - action: () => { - setIsBottomSheetOpen(false); - navigate('/insta-connect'); - }, - icon: insta, - }, - { - text: '사진 올리기', - action: () => { - setIsBottomSheetOpen(false); - navigate('/image-select'); - }, - icon: photo, - }, - ], - marginBottom: '50px', - }; + const bottomSheetMenuProps: BottomSheetMenuProps = { + items: [ + { + text: '인스타 피드 가져오기', + action: () => { + setIsBottomSheetOpen(false); + navigate('/insta-connect'); + }, + icon: insta, + }, + { + text: '사진 올리기', + action: () => { + setIsBottomSheetOpen(false); + navigate('/image-select'); + }, + icon: photo, + }, + ], + marginBottom: '50px', + }; - const bottomSheetProps: BottomSheetProps = { - isOpenBottomSheet: isBottomSheetOpen, - isHandlerVisible: true, - Component: BottomSheetMenu, - componentProps: bottomSheetMenuProps, - onCloseBottomSheet: () => { - setIsBottomSheetOpen(false); - }, - }; + const bottomSheetProps: BottomSheetProps = { + isOpenBottomSheet: isBottomSheetOpen, + isHandlerVisible: true, + Component: BottomSheetMenu, + componentProps: bottomSheetMenuProps, + onCloseBottomSheet: () => { + setIsBottomSheetOpen(false); + }, + }; - const handleOpenSheet = () => { - setIsBottomSheetOpen(true); - }; + const handleOpenSheet = () => { + setIsBottomSheetOpen(true); + }; - //게시물 리스트 조회 api - 콘솔 삭제 예정! - const fetchPostList = async () => { - try { - const storedUserId = localStorage.getItem('my_id'); // my_id로 변경되었음 - if (!storedUserId) { - console.error('User ID not found in localStorage'); - return; - } + // 게시물 리스트 조회 API + const fetchPostList = async () => { + try { + const storedUserId = localStorage.getItem('my_id'); + if (!storedUserId) { + console.error('User ID not found in localStorage'); + return; + } - console.log('Fetching posts for user ID:', storedUserId); // 디버깅: User ID 확인 + const response = await getUserPostListApi(1, 10, Number(storedUserId)); + const { post, totalPostsCount } = response.data; - // API 호출 - const response = await getUserPostListApi(1, 10, Number(storedUserId)); - console.log('API Response:', response); // 디버깅: API 응답 확인 + setPosts(post); // 게시물 목록 상태 업데이트 (UserPostSummary 사용!) + setTotalPosts(totalPostsCount); // 총 게시물 수 상태 업데이트 + } catch (error) { + console.error('Error fetching user post list:', error); + } finally { + setIsLoading(false); // 로딩 상태 종료 + } + }; - const { post, totalPostsCount, totalPostCommentsCount, totalPostLikesCount, meta } = response.data; + useEffect(() => { + fetchPostList(); + }, []); - console.log('Post List:', post); // 디버깅: 게시물 리스트 확인 - console.log('Pagination Meta:', meta); // 디버깅: 페이지네이션 정보 확인 + if (isLoading) { + return ; + } - // 상태 업데이트 - setPosts(post); - setTotalStats({ totalPostsCount, totalPostCommentsCount: totalPostCommentsCount ?? 0, totalPostLikesCount }); - - if (totalPostsCount === 0) { - console.log('No posts available for the user.'); - } - } catch (error) { - console.error('Error fetching post list:', error); // 디버깅: 에러 확인 - } finally { - setIsLoading(false); - console.log('Loading completed.'); // 디버깅: 로딩 완료 확인 - } - }; - useEffect(() => { - fetchPostList(); - }, []); - - if (isLoading) { - return ; - } - - return ( - - - - Add - - - -
- -
- - - - OOTD - {totalStats?.totalPostsCount} - - - 코멘트 - {totalStats?.totalPostCommentsCount} - - - 좋아요 - {totalStats?.totalPostLikesCount} - - - - {posts.length > 0 ? ( - posts - .sort((a, b) => { - if (b.isRepresentative && !a.isRepresentative) return 1; - if (a.isRepresentative && !b.isRepresentative) return -1; - return 0; - }) - .map((post) => ) - ) : ( -

게시물이 없습니다.

- )} -
- -
-
- ); + return ( + + + + Add + + + +
+ +
+ + + + OOTD + {totalPosts || 0} + + + 코멘트 + + {posts.reduce((sum, post) => sum + (post.postCommentsCount || 0), 0)} + + + + 좋아요 + + {posts.reduce((sum, post) => sum + (post.postLikesCount || 0), 0)} + + + + + {posts.length > 0 ? ( + posts + .sort((a, b) => Number(b.isRepresentative) - Number(a.isRepresentative)) + .map((post) => ) + ) : ( +

게시물이 없습니다.

+ )} +
+ +
+
+ ); }; export default MyPage;