github.com/minio/console@v1.4.1/web-app/src/screens/Console/Common/Hooks/useApi.tsx (about) 1 import { useState } from "react"; 2 import api from "../../../../common/api"; 3 import { ErrorResponseHandler } from "../../../../common/types"; 4 5 type NoReturnFunction = (param?: any) => void; 6 type ApiMethodToInvoke = (method: string, url: string, data?: any) => void; 7 type IsApiInProgress = boolean; 8 9 const useApi = ( 10 onSuccess: NoReturnFunction, 11 onError: NoReturnFunction, 12 ): [IsApiInProgress, ApiMethodToInvoke] => { 13 const [isLoading, setIsLoading] = useState<boolean>(false); 14 15 const callApi = (method: string, url: string, data?: any, headers?: any) => { 16 setIsLoading(true); 17 api 18 .invoke(method, url, data, headers) 19 .then((res: any) => { 20 setIsLoading(false); 21 onSuccess(res); 22 }) 23 .catch((err: ErrorResponseHandler) => { 24 setIsLoading(false); 25 onError(err); 26 }); 27 }; 28 29 return [isLoading, callApi]; 30 }; 31 32 export default useApi;