-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChatOnline.jsx
55 lines (49 loc) · 1.49 KB
/
ChatOnline.jsx
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
import axios from "axios";
import { useEffect, useState } from "react";
import "./chatOnline.css";
export default function ChatOnline({ onlineUsers, currentId, setCurrentChat }) {
const [friends, setFriends] = useState([]);
const [onlineFriends, setOnlineFriends] = useState([]);
const PF = process.env.REACT_APP_PUBLIC_FOLDER;
useEffect(() => {
const getFriends = async () => {
const res = await axios.get("/users/friends/" + currentId);
setFriends(res.data);
};
getFriends();
}, [currentId]);
useEffect(() => {
setOnlineFriends(friends.filter((f) => onlineUsers.includes(f._id)));
}, [friends, onlineUsers]);
const handleClick = async (user) => {
try {
const res = await axios.get(
`/conversations/find/${currentId}/${user._id}`
);
setCurrentChat(res.data);
} catch (err) {
console.log(err);
}
};
return (
<div className="chatOnline">
{onlineFriends.map((o) => (
<div className="chatOnlineFriend" onClick={() => handleClick(o)}>
<div className="chatOnlineImgContainer">
<img
className="chatOnlineImg"
src={
o?.profilePicture
? PF + o.profilePicture
: PF + "person/noAvatar.png"
}
alt=""
/>
<div className="chatOnlineBadge"></div>
</div>
<span className="chatOnlineName">{o?.username}</span>
</div>
))}
</div>
);
}