-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseFetchDimensions.ts
More file actions
57 lines (51 loc) · 1.7 KB
/
useFetchDimensions.ts
File metadata and controls
57 lines (51 loc) · 1.7 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
52
53
54
55
56
57
import React from 'react';
import { GroupByList } from 'shared/constants';
import { Dropdown } from 'shared/types/dropdown.interface';
import { Post } from 'shared/utils/request';
export const getDimensions = async (
spaceId: string,
roomId: string,
contextId: string,
nodeIDs: string[],
baseUrl: string
) => {
const response = await Post({
path: `/v2/spaces/${spaceId}/rooms/${roomId}/charts/${contextId}`,
baseUrl,
data: {
filter: { nodeIDs },
},
});
return response?.data?.results;
};
export const useFetchDimensions = (baseUrl: string) => {
const [isError, setIsError] = React.useState(false);
const [allDimensions, setDimensions] = React.useState<any[]>([]);
const [filters, setFilters] = React.useState<{ [key in string]: [] }>({});
const [groupingByList, setGroupingByList] = React.useState<Dropdown[]>(GroupByList);
const [units, setUnits] = React.useState('');
const fetchDimensions = async ({ spaceId, roomId, contextId, nodeIDs }: any) => {
setIsError(false);
try {
const result = await getDimensions(spaceId, roomId, contextId, nodeIDs, baseUrl);
setUnits(result?.[contextId].units);
setDimensions(Object.values(result?.[contextId]?.dimensions)?.map((c: any) => ({ label: c.name, value: c.id })));
setFilters({ 'No filter': [], ...result?.[contextId]?.chartLabels });
const groupByData = [
...GroupByList,
...Object.keys(result?.[contextId]?.chartLabels).map((g: any) => ({ label: g, value: g })),
];
setGroupingByList(groupByData);
} catch (error) {
setIsError(true);
}
};
return {
isError,
allDimensions,
filters,
groupingByList,
units,
fetchDimensions,
};
};