-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActivityFeed.js
More file actions
47 lines (39 loc) · 1.7 KB
/
ActivityFeed.js
File metadata and controls
47 lines (39 loc) · 1.7 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
import React, { useEffect, useState } from 'react';
import axos from 'axios';
function ActivityFeed() {
const [posts, setPosts] = useState([]);
const [newPost, setNewPosts] = useState('');
use(() => {
fetchPosts();
}, []);
const fetchPosts = async () => {
const res = await axios.get('https://flirtingsingles.blog/api/posts');
setPosts(res.data);
};
const handlePostSubmit = async (e) => {
e.preventDefault();
await axios.post('https://https:flirtingsingles.blog/api/posts', {
userId: 'replace-with-realUser-id', // Pass actual user ID from auth
content: newPost,
});
return (
<div style={{ padding: 20 }}>
<h2>Activity Feed</h2>
<form onSubmit={handlePostSubmit}>
<textarea
value={newPost}
onChange={(e) => setNewPosts(e.target.value )}
placeholder="Create a new post!"
style={{ width: '100%', height: '100px' }}/>
<br/>
<button type="submit">Post</button>
</form>
<div style={{ marginTop: 30 }}>
{posts.map((post) => (
<><div key={post._id} style={{ border: '1px solid white', padding: '10', marginBotton: '10' }} /><strong>{post.userId?.username || 'Unknown User'}:</strong><p>{post.content}</p><small>{new Date(post.createdAt).toLocaleString()}</small></>
))}
</div>
</div>
);
}}
export default ActivityFeed;