From 3efc943dd5d5cf7b62867014e827653221f8650b Mon Sep 17 00:00:00 2001
From: Matt Kirby <MattJKirby@outlook.com>
Date: Mon, 17 Apr 2023 02:06:04 +0100
Subject: [PATCH] Added usefriends hook

---
 .../src/hooks/useFriends.ts                   | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 daily-thought-frontend/src/hooks/useFriends.ts

diff --git a/daily-thought-frontend/src/hooks/useFriends.ts b/daily-thought-frontend/src/hooks/useFriends.ts
new file mode 100644
index 00000000..385bb9c9
--- /dev/null
+++ b/daily-thought-frontend/src/hooks/useFriends.ts
@@ -0,0 +1,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 
+}
\ No newline at end of file
-- 
GitLab