forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostDetailsPage.jsx
More file actions
95 lines (80 loc) · 2.59 KB
/
Copy pathPostDetailsPage.jsx
File metadata and controls
95 lines (80 loc) · 2.59 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
import { useState, useEffect } from 'react';
import PostDetail from './PostDetail';
import CommentItem from './CommentItem';
import CommentInput from './CommentInput';
import { apiService } from '../services/api';
const PostDetailsPage = ({ postId }) => {
const [post, setPost] = useState(null);
const [comments, setComments] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [refreshKey, setRefreshKey] = useState(0);
useEffect(() => {
const fetchPostAndComments = async () => {
try {
setLoading(true);
setError(null);
const [postData, commentsData] = await Promise.all([
apiService.getPostById(postId),
apiService.getCommentsByPostId(postId)
]);
setPost(postData);
setComments(commentsData);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
if (postId) {
fetchPostAndComments();
}
}, [postId, refreshKey]);
const handleLike = async (postId) => {
try {
const username = prompt('Enter your username to like this post:');
if (!username) return;
await apiService.likePost(postId, { username });
setRefreshKey(prev => prev + 1);
} catch (err) {
setError(err.message);
}
};
const handleAddComment = async (content) => {
try {
const username = prompt('Enter your username:');
if (!username) return;
await apiService.createComment(postId, { username, content });
setRefreshKey(prev => prev + 1);
} catch (err) {
setError(err.message);
}
};
if (loading) {
return (
<div className="w-[760px] h-[832px] bg-[#E5A000] rounded-[20px] flex items-center justify-center">
<div className="text-[24px] text-white">Loading post details...</div>
</div>
);
}
if (error || !post) {
return (
<div className="w-[760px] h-[832px] bg-[#E5A000] rounded-[20px] flex items-center justify-center">
<div className="text-[24px] text-white">Error loading post: {error}</div>
</div>
);
}
return (
<div className="w-[760px] h-[832px] bg-[#E5A000] rounded-[20px] overflow-y-auto">
<PostDetail post={post} onLike={handleLike} />
<div className="w-full h-[2px] bg-white"></div>
<div className="comments-section">
{comments.map((comment) => (
<CommentItem key={comment.id} comment={comment} />
))}
</div>
<CommentInput onSubmit={handleAddComment} />
</div>
);
};
export default PostDetailsPage;