summaryrefslogtreecommitdiff
path: root/microservices/04-react-app/src/features/posts
diff options
context:
space:
mode:
authorKamal Wickramanayake <kamal@inbox.lk>2026-07-03 19:02:36 +0530
committerKamal Wickramanayake <kamal@inbox.lk>2026-07-03 19:02:36 +0530
commitaa122113ade36f02dc8fdbebdf1232b5c4b8742c (patch)
treec345b83db6c769bf5e72a09e3f19d43431961695 /microservices/04-react-app/src/features/posts
parentb221a83ecef1dd7f9583d5107017d24e668205a6 (diff)
Microservice sample projects
Diffstat (limited to 'microservices/04-react-app/src/features/posts')
-rw-r--r--microservices/04-react-app/src/features/posts/components/PostList.jsx42
1 files changed, 42 insertions, 0 deletions
diff --git a/microservices/04-react-app/src/features/posts/components/PostList.jsx b/microservices/04-react-app/src/features/posts/components/PostList.jsx
new file mode 100644
index 0000000..a7ccd3b
--- /dev/null
+++ b/microservices/04-react-app/src/features/posts/components/PostList.jsx
@@ -0,0 +1,42 @@
+import { useState, useEffect } from 'react'
+import api from '../../../services/api'
+
+const PostList = () => {
+ const [posts, setPosts] = useState([]);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState(null);
+
+ // Fetch data on component mount
+ useEffect(() => {
+ api
+ .get("/posts") // Replace with your actual API endpoint
+ .then((response) => {
+ setPosts(response.data);
+ setLoading(false);
+ })
+ .catch((err) => {
+ setError(err.message);
+ setLoading(false);
+ });
+ }, []);
+
+ if (loading) return <p>Loading posts...</p>;
+ if (error) return <p style={{ color: "red" }}>Error: {error}</p>;
+
+ return (
+ <div style={{ padding: "10px", fontFamily: "sans-serif" }}>
+ <h3>Latest posts</h3>
+
+ {/* 1. LIST VIEW */}
+ <ul>
+ {posts.map((post) => (
+ <li key={post.id}>
+ <strong>{post.title}</strong> by {post.author} (ID: {post.id})
+ </li>
+ ))}
+ </ul>
+ </div>
+ );
+}
+
+export default PostList \ No newline at end of file