-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseFetchNodes.ts
More file actions
51 lines (43 loc) · 1.09 KB
/
useFetchNodes.ts
File metadata and controls
51 lines (43 loc) · 1.09 KB
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
import React from 'react';
import { Post } from 'shared/utils/request';
type Node = {
nd: string;
mg: string;
nm: string;
[key: string]: any;
};
const transformNodes = (nodes: Node[] = []) =>
nodes.map(({ nd, mg, nm, ...rest }) => ({ id: nd || mg, name: nm, ...rest }));
export const getNodes = async (spaceId: string, roomId: string, baseUrl: string) => {
const response = await Post({
path: `/v3/spaces/${spaceId}/rooms/${roomId}/nodes`,
baseUrl,
data: {
scope: {
nodes: [],
},
},
});
return response?.data?.nodes;
};
export const useFetchNodes = (baseUrl: string) => {
const [isError, setIsError] = React.useState(false);
const [nodes, setNodes] = React.useState([]);
const fetchNodes = React.useCallback(
async (spaceId: string, roomId: string) => {
setIsError(false);
try {
const result = await getNodes(spaceId, roomId, baseUrl);
setNodes(result);
} catch (error) {
setIsError(true);
}
},
[baseUrl]
);
return {
isError,
nodes: transformNodes(nodes),
fetchNodes,
};
};