forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
118 lines (101 loc) · 3.27 KB
/
Copy pathApp.jsx
File metadata and controls
118 lines (101 loc) · 3.27 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
import { useState, useEffect } from 'react';
import PostList from './components/PostList';
import PostDetailsPage from './components/PostDetailsPage';
import ApiStatusIndicator from './components/ApiStatusIndicator';
import { apiService } from './services/api';
import './App.css';
const App = () => {
const [posts, setPosts] = useState([]);
const [filteredPosts, setFilteredPosts] = useState([]);
const [isApiAvailable, setIsApiAvailable] = useState(true);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [selectedPostId, setSelectedPostId] = useState(null);
const fetchPosts = async () => {
try {
setLoading(true);
setError(null);
const data = await apiService.getPosts();
setPosts(data);
setFilteredPosts(data);
setIsApiAvailable(true);
} catch (err) {
setError(err.message);
setIsApiAvailable(apiService.getAvailability());
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchPosts();
const interval = setInterval(() => {
fetchPosts();
}, 30000);
return () => clearInterval(interval);
}, []);
const handleLike = async (postId) => {
try {
const username = prompt('Enter your username to like this post:');
if (!username) return;
await apiService.likePost(postId, { username });
await fetchPosts();
} catch (err) {
setError(err.message);
setIsApiAvailable(apiService.getAvailability());
}
};
const handleComment = async (postId) => {
setSelectedPostId(postId);
};
const handleSearch = (searchTerm) => {
if (!searchTerm.trim()) {
setFilteredPosts(posts);
return;
}
const lowercaseSearch = searchTerm.toLowerCase();
const filtered = posts.filter(post =>
post.username.toLowerCase().includes(lowercaseSearch) ||
post.content.toLowerCase().includes(lowercaseSearch)
);
setFilteredPosts(filtered);
};
const handleBackToList = () => {
setSelectedPostId(null);
fetchPosts();
};
return (
<div className="app-container">
<ApiStatusIndicator isAvailable={isApiAvailable} />
<div className="home-frame">
<div className="sidebar-left"></div>
<div className="main-content">
{selectedPostId ? (
<PostDetailsPage postId={selectedPostId} onBack={handleBackToList} />
) : (
<>
{loading && <div className="loading">Loading posts...</div>}
{error && !loading && (
<div className="error-message">
{isApiAvailable ? `Error: ${error}` : 'Unable to load posts. Backend is unavailable.'}
</div>
)}
{!loading && !error && posts.length === 0 && (
<div className="empty-message">No posts available</div>
)}
{!loading && posts.length > 0 && (
<PostList
posts={filteredPosts}
onLike={handleLike}
onComment={handleComment}
onSearch={handleSearch}
/>
)}
</>
)}
</div>
<div className="sidebar-right"></div>
</div>
</div>
);
};
export default App;