forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomePage.jsx
More file actions
73 lines (66 loc) · 2.57 KB
/
Copy pathHomePage.jsx
File metadata and controls
73 lines (66 loc) · 2.57 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
import { useState, useEffect, useCallback } from "react";
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";
import NameInputModal from "../components/modal/NameInputModal";
const HomePage = () => {
const [posts, setPosts] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState("");
const [isPostModalOpen, setIsPostModalOpen] = useState(false);
const [isNameModalOpen, setIsNameModalOpen] = useState(false);
const { isAuthenticated, isLoading: authLoading } = useAuth();
const fetchPosts = useCallback(async () => {
if (!isAuthenticated) return;
try {
setIsLoading(true);
setError("");
const response = await postApi.getPosts();
setPosts(response.data);
} catch (error) {
setError("An error occurred while loading posts.");
} finally {
setIsLoading(false);
}
}, [isAuthenticated]);
useEffect(() => {
if (!authLoading && !isAuthenticated) {
setIsNameModalOpen(true);
} else if (isAuthenticated) {
fetchPosts();
}
}, [authLoading, isAuthenticated, fetchPosts]);
const handleOpenPostModal = () => setIsPostModalOpen(true);
const handleClosePostModal = () => setIsPostModalOpen(false);
const handlePostCreated = () => {
fetchPosts();
setIsPostModalOpen(false);
};
return (
<Layout>
<div className="w-full max-w-2xl mx-auto">
<h1 className="text-2xl font-bold mb-6 text-gray-900 dark:text-white">Contoso Outdoor Social</h1>
{isLoading ? (
<div className="text-center py-10 text-gray-500">Loading posts...</div>
) : error ? (
<div className="text-center py-10 text-red-500">{error}</div>
) : posts.length === 0 ? (
<div className="text-center py-10 text-gray-400">No posts yet.</div>
) : (
<div className="flex flex-col gap-4">
{posts.map((post) => (
<PostCard key={post.id} post={post} />
))}
</div>
)}
<FloatingActionButton onClick={handleOpenPostModal} />
<PostingModal isOpen={isPostModalOpen} onClose={handleClosePostModal} onPostCreated={handlePostCreated} />
<NameInputModal isOpen={isNameModalOpen} onClose={() => setIsNameModalOpen(false)} />
</div>
</Layout>
);
};
export default HomePage;