Add useEntityRequest hook

This commit is contained in:
Alex Gleason 2023-03-23 16:04:42 -05:00
parent ac9718e6ed
commit 7248331742
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -0,0 +1,30 @@
import { useState } from 'react';
import { useApi } from 'soapbox/hooks';
import { EntityRequest } from './types';
import { toAxiosRequest } from './utils';
function useEntityRequest() {
const api = useApi();
const [isLoading, setIsLoading] = useState<boolean>(false);
async function request(entityRequest: EntityRequest) {
setIsLoading(true);
try {
const response = await api.request(toAxiosRequest(entityRequest));
setIsLoading(false);
return response;
} catch (e) {
setIsLoading(false);
throw e;
}
}
return {
request,
isLoading,
};
}
export { useEntityRequest };