sforkowany z mirror/soapbox
Add useEntityRequest hook
rodzic
ac9718e6ed
commit
7248331742
|
@ -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 };
|
Ładowanie…
Reference in New Issue