-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuseUser.js
55 lines (53 loc) · 1.48 KB
/
useUser.js
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 { useState, useEffect } from "react";
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { GET_OPTIONS } from "../lib/constants";
export default function useUser() {
const { siteConfig } = useDocusaurusContext();
const { DASHBOARD_URL } = siteConfig?.customFields || {}
const [user, setUser] = useState(undefined);
const [loading, setLoading] = useState(true);
const [keys, setKeys] = useState([]);
const getUserInfo = async () => {
setLoading(true);
try {
const res = await fetch(`${DASHBOARD_URL}/api/me`, GET_OPTIONS);
const response = await res.json();
if (response?.data) {
setUser(response.data);
const userId = response.data.id;
const upData = await fetch(
`${DASHBOARD_URL}/api/v1/users/${userId}/projects`,
GET_OPTIONS
);
if (upData.ok) {
const upProjects = await upData.json();
const keysArr = upProjects?.result?.projects;
const allKeys = [];
if (keysArr) {
Object.keys(keysArr).forEach((key) => {
allKeys.push(keysArr[key]);
});
}
setKeys([...allKeys]);
}
}
if (response?.error) {
setUser(undefined);
setKeys([]);
}
} catch (e) {
setUser(undefined);
setKeys([]);
} finally {
setLoading(false);
}
};
useEffect(() => {
getUserInfo();
}, []);
return {
user,
keys,
loading,
};
}