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
57 lines (51 loc) · 1.44 KB
/
Copy pathApp.jsx
File metadata and controls
57 lines (51 loc) · 1.44 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
import React, { useState, useEffect } from "react";
import Home from "./components/Home";
import Search from "./components/Search";
import PostModal from "./components/PostModal";
import NameInputModal from "./components/NameInputModal";
function App() {
const [posts, setPosts] = useState([]);
const [username, setUsername] = useState(
localStorage.getItem("username") || "",
);
const [isModalOpen, setIsModalOpen] = useState(false);
// Gọi API lấy bài đăng từ Backend Python
const fetchPosts = async () => {
try {
const response = await fetch("http://localhost:8000/posts");
const data = await response.json();
setPosts(data);
} catch (err) {
console.error("Không thể kết nối Backend:", err);
}
};
useEffect(() => {
if (username) fetchPosts();
}, [username]);
if (!username) {
return (
<NameInputModal
onSubmit={(name) => {
localStorage.setItem("username", name);
setUsername(name);
}}
/>
);
}
return (
<div className="container">
<nav className="navbar">
<h1>Contoso Vibe</h1>
<button onClick={() => setIsModalOpen(true)}>Đăng bài</button>
</nav>
<Search
onSearch={(term) => {
/* Logic tìm kiếm */
}}
/>
<Home posts={posts} />
<PostModal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)} />
</div>
);
}
export default App;