-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathProfilePage.jsx
More file actions
128 lines (118 loc) · 3.8 KB
/
Copy pathProfilePage.jsx
File metadata and controls
128 lines (118 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useState, useEffect } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { postApi } from "../api/apiService";
import { useAuth } from "../context/AuthContext";
import Layout from "../components/common/Layout";
import PostCard from "../components/post/PostCard";
import FloatingActionButton from "../components/common/FloatingActionButton";
import PostingModal from "../components/modal/PostingModal";
const ProfilePage = () => {
const { user, logout } = useAuth();
const navigate = useNavigate();
const { userId: urlUserId } = useParams();
// userId가 아니라 username을 기준으로 동작하도록 변경
const username = urlUserId || (user && user.username);
const isMyProfile = user && username === user.username;
const [userPosts, setUserPosts] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState("");
const [isPostModalOpen, setIsPostModalOpen] = useState(false);
useEffect(() => {
if (!username) {
setIsLoading(false);
return;
}
const fetchPosts = async () => {
try {
setIsLoading(true);
setError("");
const response = await postApi.getPosts();
// username이 일치하는 포스트만 필터링
const posts = (response.data || []).filter(
(post) => post.username === username
);
// 최신순 정렬
posts.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
setUserPosts(posts);
} catch (err) {
setError("Failed to load profile information.");
} finally {
setIsLoading(false);
}
};
fetchPosts();
}, [username]);
const handleLogout = () => {
if (window.confirm("Are you sure you want to logout?")) {
logout();
navigate("/");
}
};
const togglePostModal = () => {
setIsPostModalOpen(!isPostModalOpen);
};
const handlePostCreated = (newPost) => {
setUserPosts((prev) => [newPost, ...prev]);
};
if (!username) {
return (
<Layout>
<div className="flex flex-col items-center justify-center h-screen gap-4">
<p className="text-lg text-gray-700">Login is required.</p>
<button
onClick={() => navigate("/")}
className="px-4 py-2 text-white bg-blue-600 rounded-md hover:bg-blue-700"
>
Go to Home
</button>
</div>
</Layout>
);
}
if (isLoading) {
return (
<Layout>
<div className="text-center py-10 text-gray-500">
Loading profile information...
</div>
</Layout>
);
}
return (
<Layout>
<div className="w-full max-w-2xl mx-auto">
<div className="flex items-center gap-4 mb-4">
<div className="w-16 h-16 rounded-full bg-gray-200" />
<div>
<div className="text-xl font-bold text-gray-900 dark:text-white">
{username}
</div>
</div>
</div>
{isMyProfile && (
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600 mb-6"
onClick={handleLogout}
>
Logout
</button>
)}
{error && <div className="text-red-500 text-center py-4">{error}</div>}
<div className="flex flex-col gap-4">
{userPosts && userPosts.length > 0 ? (
userPosts.map((post) => <PostCard key={post.id} post={post} />)
) : (
<div className="text-center py-10 text-gray-500">No posts yet.</div>
)}
</div>
<FloatingActionButton onClick={togglePostModal} />
<PostingModal
isOpen={isPostModalOpen}
onClose={togglePostModal}
onPostCreated={handlePostCreated}
/>
</div>
</Layout>
);
};
export default ProfilePage;