-
Notifications
You must be signed in to change notification settings - Fork 10
/
FriendListWidget.jsx
56 lines (51 loc) · 1.57 KB
/
FriendListWidget.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
56
import { Box, Typography, useTheme } from "@mui/material";
import Friend from "components/Friend";
import WidgetWrapper from "components/WidgetWrapper";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { setFriends } from "state";
const FriendListWidget = ({ userId }) => {
const dispatch = useDispatch();
const { palette } = useTheme();
const token = useSelector((state) => state.token);
const friends = useSelector((state) => state.user.friends);
const getFriends = async () => {
const response = await fetch(
`http://localhost:3001/users/${userId}/friends`,
{
method: "GET",
headers: { Authorization: `Bearer ${token}` },
}
);
const data = await response.json();
dispatch(setFriends({ friends: data }));
};
useEffect(() => {
getFriends();
}, []); // eslint-disable-line react-hooks/exhaustive-deps
return (
<WidgetWrapper>
<Typography
color={palette.neutral.dark}
variant="h5"
fontWeight="500"
sx={{ mb: "1.5rem" }}
>
Friend List
</Typography>
<Box display="flex" flexDirection="column" gap="1.5rem">
{friends?.map((friend) => (
<Friend
key={friend._id}
friendId={friend._id}
name={`${friend.firstName} ${friend.lastName}`}
subtitle={friend.occupation}
userPicturePath={friend.picturePath}
/>
))}
{/* {console.log(friends)} */}
</Box>
</WidgetWrapper>
);
};
export default FriendListWidget;