-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFriends.js
More file actions
64 lines (56 loc) · 2.63 KB
/
Friends.js
File metadata and controls
64 lines (56 loc) · 2.63 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
import React, { useEffect, useState } from 'react';
import axios from 'axios';
export default function Friends() {
const [requests, setRequests] = useState([]);
const [friends, setFriends] = useState([]);
const token = localStorage.getItem('token');
const headers = { Authorization: token };
const fetchRequests = async () => {
const res = await axios.get('https://flirtingsingles.blog/api/users/requests', { headers });
setRequests(res.data);
};
const fetchFriends = async () => {
const res = await axios.get('https://flirtingsingles.blog/api/users/friends', { headers });
};
const accept = async (id) => {
await axios.post('https://flirtingsingles.blog/api/users/accept-request/${id}', {}, { headers });
fetchRequests();
fetchFriends();
};
const decline = async (id) => {
await axios.post('https://flirtingsingles.blog/api/users/decline-request/${id}', {}, { headers });
fetchRequests();
};
useEffect(() => {
fetchRequests();
fetchFriends();
}, []);
return (
<div style={{ maxWidth: '600px', margin: 'auto' }}>
<h2>Friend Requests</h2>
{requests.length === 0 && <p>No friend requests</p>}
{requests.map((request) => (
<div key={req._id} style={{ marginBottom: '10px', padding: '10px', border: '1px solid #ccc'}}>
<p>{request.username}</p>
{req.profilePicture && (
<img src={'https://flirtingsingles.blog/${request.profilePicture}'} alt="Profile" style={{ width: '30', borderRadius: '50%' }}/>
)}
<strong>{req.username}</strong>
<button onClick={() => accept(request._id)} style={{ marginRight: '10px' }}>Accept</button>
<button onClick={() => decline(request._id)}>Decline</button>
</div>
))}
<h2>Friends</h2>
{friends.length === 0 && <p>No friends</p>}
{friends.map((friend) => (
<div key={friend._id} style={{ marginBottom: '10px', padding: '10px', border: '1px solid #ccc'}}>
<p>{friend.username}</p>
{friend.profilePicture && (
<img src={`https://flirtingsingles.blog/${friend.profilePicture}`} alt="Profile" style={{ width: '30px', borderRadius: '50%' }}/>
)}
<strong>{friend.username}</strong>
</div>
))}
</div>
);
}