-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseFetchSpaces.ts
More file actions
35 lines (29 loc) · 947 Bytes
/
useFetchSpaces.ts
File metadata and controls
35 lines (29 loc) · 947 Bytes
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
import React from 'react';
import { Space } from '../types/space.interface';
import { Get } from 'shared/utils/request';
import { Dropdown } from 'shared/types/dropdown.interface';
export const getSpaces = async (baseUrl: string) => {
const response = await Get({ path: '/v2/spaces', baseUrl });
return response?.data as Space[];
};
export const useFetchSpaces = (baseUrl: string) => {
const [isError, setIsError] = React.useState(false);
const [spaces, setSpaces] = React.useState<Dropdown[]>([]);
React.useEffect(() => {
try {
const fetchData = async () => {
setIsError(false);
try {
const result = await getSpaces(baseUrl);
setSpaces(result.map((s) => ({ label: s.name, value: s.id })));
} catch (error) {
setIsError(true);
}
};
fetchData();
} catch (error) {
setIsError(true);
}
}, [baseUrl]);
return { isError, spaces };
};