Newer
Older
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
import { useState, useEffect } from "react"
export const useFriends = () => {
const [friends, setFriends] = useState<Map<string, any> | undefined>(undefined)
const fetchFriends = async () => {
const endpoint = `${process.env.NEXT_PUBLIC_FRIEND_SERVICE_URL}friends`
const headers = {'Authorization': `Bearer ${sessionStorage.getItem("token")}`}
const response = await fetch(endpoint, {headers})
return await response.json()
}
const fetchUserList = async (userIdList: string[]) => {
const endpoint = `${process.env.NEXT_PUBLIC_USER_SERVICE_URL}api/userlist`
console.log(userIdList, true)
const JSONdata = JSON.stringify({
userIdList
})
const options = {
method: 'POST',
headers: {'Content-Type': 'application/json',},
body: JSONdata,
}
const response = await fetch(endpoint, options)
return await response.json()
}
useEffect(() => {
if(friends === undefined){
fetchFriends().then(res => {
fetchUserList(res.result).then(userlistRes => {
setFriends(new Map(userlistRes.userList))
})
})
}
})
return friends
}