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.tsx
More file actions
45 lines (39 loc) · 1.27 KB
/
Copy pathApp.tsx
File metadata and controls
45 lines (39 loc) · 1.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
import React, { useEffect, useState } from "react";
import PostList from "./components/PostList";
import PostForm from "./components/PostForm";
import ErrorBanner from "./components/ErrorBanner";
import Loader from "./components/Loader";
import { getPosts } from "./api/client";
import type { Post, ApiError } from "./api/types";
const App: React.FC = () => {
const [posts, setPosts] = useState<Post[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchPosts = async () => {
setLoading(true);
setError(null);
try {
const data = await getPosts();
setPosts(data);
} catch (err) {
const apiError = err as ApiError;
setError(apiError.message || "API unavailable");
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchPosts();
}, []);
return (
<main className="min-h-screen bg-gray-100">
{error && <ErrorBanner message={error} />}
<div className="max-w-2xl mx-auto py-8 px-2 flex flex-col gap-6">
<h1 className="text-3xl font-bold text-center">Simple Social Media</h1>
<PostForm onPostCreated={fetchPosts} />
{loading ? <Loader /> : <PostList posts={posts} />}
</div>
</main>
);
};
export default App;