bigbuffet-rw/app/soapbox/entity-store/hooks/useEntityRequest.ts
2023-03-23 16:04:42 -05:00

30 lines
No EOL
637 B
TypeScript

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 };