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
| export const useFetchAPI = <T extends unknown>(
url: string,
method: 'POST' | 'GET',
body?: string | JSONObject
): [string, T | null] => {
const [fetchStatus, setFetchStatus] = useState('error');
const [fetchResult, setFetchResult] = useState<T | null>(null);
useEffect(() => {
const apiMockFetch: () => Promise<{
status: string;
requestId: string;
result: T | null;
}> = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(someData);
}, 1000);
});
};
const fetchData = async () => {
const { status, result } = await apiMockFetch();
setFetchStatus(status);
if (result !== undefined) {
setFetchResult(result);
}
};
if (fetchStatus !== 'success') fetchData();
}, [url, method, body, fetchStatus]);
return [fetchStatus, fetchResult];
};
|