-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathPostDetailPage.jsx
More file actions
343 lines (322 loc) · 10.3 KB
/
Copy pathPostDetailPage.jsx
File metadata and controls
343 lines (322 loc) · 10.3 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { useState, useEffect, useCallback } from "react";
import { useParams, useNavigate } from "react-router-dom";
import { postApi, commentApi } from "../api/apiService";
import { useAuth } from "../context/AuthContext";
import Layout from "../components/common/Layout";
import CommentItem from "../components/comment/CommentItem";
import CommentInput from "../components/comment/CommentInput";
const HeartIcon = ({ filled }) => (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"
fill={filled ? "currentColor" : "none"}
stroke={filled ? "none" : "currentColor"}
strokeWidth="2"
/>
</svg>
);
const BackIcon = () => (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"
fill="currentColor"
/>
</svg>
);
const PostDetailPage = () => {
const { postId } = useParams();
const navigate = useNavigate();
const { user } = useAuth();
const [post, setPost] = useState(null);
const [comments, setComments] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [isCommentsLoading, setIsCommentsLoading] = useState(true);
const [error, setError] = useState("");
const [isLiked, setIsLiked] = useState(false);
const [likesCount, setLikesCount] = useState(0);
const [isEditing, setIsEditing] = useState(false);
const [editContent, setEditContent] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false);
const fetchPostDetail = useCallback(async () => {
try {
setIsLoading(true);
setError("");
const response = await postApi.getPost(postId);
setPost(response.data);
setIsLiked(response.data.isLiked || false);
setLikesCount(response.data.likesCount || 0);
setEditContent(response.data.content);
} catch (error) {
console.error("Error loading post detail:", error);
setError("Error occurred while loading the post.");
} finally {
setIsLoading(false);
}
}, [postId]);
const fetchComments = useCallback(async () => {
try {
setIsCommentsLoading(true);
const response = await commentApi.getComments(postId);
const items = response.data; // 응답이 배열임
setComments(items);
} catch (error) {
console.error("Error loading comments:", error);
} finally {
setIsCommentsLoading(false);
}
}, [postId]);
useEffect(() => {
fetchPostDetail();
}, [fetchPostDetail]);
useEffect(() => {
if (postId) {
fetchComments();
}
}, [fetchComments, postId]);
const handleLikeToggle = async () => {
if (!user) return;
try {
if (isLiked) {
const response = await postApi.unlikePost(postId, user.username);
setLikesCount(response.data.likesCount);
setIsLiked(false);
} else {
const response = await postApi.likePost(postId, user.username);
setLikesCount(response.data.likesCount);
setIsLiked(true);
}
} catch (error) {
console.error("Error toggling like:", error);
}
};
const handleGoBack = () => {
navigate(-1);
};
const handleCommentAdded = (newComment) => {
setComments((prevComments) => [newComment, ...prevComments]);
if (post) {
setPost({
...post,
commentsCount: (post.commentsCount || 0) + 1,
});
}
};
const handleCommentDelete = (commentId) => {
setComments((prevComments) =>
prevComments.filter((comment) => comment.id !== commentId)
);
if (post) {
setPost({
...post,
commentsCount: Math.max((post.commentsCount || 0) - 1, 0),
});
}
};
const handleCommentUpdate = (updatedComment) => {
setComments((prevComments) =>
prevComments.map((comment) =>
comment.id === updatedComment.id ? updatedComment : comment
)
);
};
const isAuthor = user && post && user.username === post.username;
const handleEditClick = () => {
setIsEditing(true);
setEditContent(post.content);
};
const handleEditCancel = () => {
setIsEditing(false);
setEditContent(post.content);
};
const handleEditSave = async () => {
if (!editContent.trim()) {
return;
}
try {
setIsSubmitting(true);
const response = await postApi.updatePost(
postId,
editContent,
user.username
);
setPost(response.data);
setIsEditing(false);
} catch (error) {
console.error("Error updating post:", error);
alert("Error occurred while updating the post.");
} finally {
setIsSubmitting(false);
}
};
const handleDeleteClick = async () => {
if (!window.confirm("Are you sure you want to delete this post?")) {
return;
}
try {
setIsSubmitting(true);
await postApi.deletePost(postId, user.username);
alert("Post deleted.");
navigate("/");
} catch (error) {
console.error("Error deleting post:", error);
alert("Error occurred while deleting the post.");
} finally {
setIsSubmitting(false);
}
};
if (isLoading) {
return (
<Layout>
<p className="text-center py-4 text-gray-400">Loading...</p>
</Layout>
);
}
if (error || !post) {
return (
<Layout>
<div className="flex flex-col items-center justify-center h-[50vh] gap-4">
<p className="text-red-500 text-center mb-2">
{error || "Post not found."}
</p>
<button
onClick={handleGoBack}
className="flex items-center gap-1 px-4 py-2 rounded bg-gray-100 hover:bg-gray-200 text-gray-700"
>
<BackIcon /> Go Back
</button>
</div>
</Layout>
);
}
return (
<Layout>
<div className="w-full max-w-2xl flex flex-col py-4 mx-auto">
<div className="flex items-center mb-4">
<button
onClick={handleGoBack}
className="flex items-center gap-1 px-2 py-1 rounded-full hover:bg-gray-100 text-gray-700"
>
<BackIcon /> Go Back
</button>
<h1 className="text-lg font-bold ml-4 text-gray-900 dark:text-white">
Post Detail
</h1>
</div>
<div className="w-full border-b border-gray-300 pb-4 mb-4">
<div className="flex items-center mb-4">
<div className="w-10 h-10 rounded-full bg-gray-200 dark:bg-gray-700 mr-2" />
<div className="text-base font-bold text-gray-900 dark:text-white">
{post.username}
</div>
</div>
<div className="mb-4">
{isEditing ? (
<div className="flex flex-col gap-2">
<textarea
className="w-full text-base p-2 border border-gray-300 rounded"
value={editContent}
onChange={(e) => setEditContent(e.target.value)}
disabled={isSubmitting}
/>
<div className="flex gap-2">
<button
onClick={handleEditSave}
disabled={isSubmitting}
className="bg-blue-600 text-white rounded px-4 py-2 text-sm disabled:opacity-50"
>
Save
</button>
<button
onClick={handleEditCancel}
disabled={isSubmitting}
className="bg-gray-200 text-gray-800 rounded px-4 py-2 text-sm disabled:opacity-50"
>
Cancel
</button>
</div>
</div>
) : (
<p className="text-base text-gray-900 dark:text-white leading-relaxed whitespace-pre-wrap break-words">
{post.content}
</p>
)}
</div>
<div className="flex items-center gap-4 mt-2">
<button
onClick={handleLikeToggle}
className={`flex items-center gap-1 ${
isLiked
? "text-red-500"
: "text-gray-500 hover:text-red-500"
}`}
aria-label="Like"
>
<HeartIcon filled={isLiked} />
{likesCount > 0 && <span className="text-xs">{likesCount}</span>}
</button>
<span className="text-sm text-gray-600">
Comments {post.commentsCount}
</span>
{isAuthor && !isEditing && (
<div className="flex gap-2 ml-auto">
<button
onClick={handleEditClick}
className="bg-blue-600 text-white rounded px-3 py-1 text-xs"
>
Edit
</button>
<button
onClick={handleDeleteClick}
className="bg-red-500 text-white rounded px-3 py-1 text-xs"
>
Delete
</button>
</div>
)}
</div>
</div>
<section className="w-full">
<h2 className="text-base font-bold mb-4 text-gray-900 dark:text-white">
Comments
</h2>
<CommentInput postId={postId} onCommentAdded={handleCommentAdded} />
{comments.length > 0 ? (
<div className="flex flex-col gap-2 mt-4">
{comments.map((comment) => (
<CommentItem
key={comment.id}
comment={comment}
postId={postId}
onCommentDelete={handleCommentDelete}
onCommentUpdate={handleCommentUpdate}
/>
))}
</div>
) : (
!isCommentsLoading && (
<p className="text-center py-8 text-gray-400">
No comments yet. Be the first to comment!
</p>
)
)}
{isCommentsLoading && (
<p className="text-center py-4 text-gray-400">Loading comments...</p>
)}
</section>
</div>
</Layout>
);
};
export default PostDetailPage;