Add useEntityRequest hook
This commit is contained in:
parent
ac9718e6ed
commit
7248331742
1 changed files with 30 additions and 0 deletions
30
app/soapbox/entity-store/hooks/useEntityRequest.ts
Normal file
30
app/soapbox/entity-store/hooks/useEntityRequest.ts
Normal 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 };
|
Loading…
Reference in a new issue