From 72483317421dbace5786397578c0e5511e6e485c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Mar 2023 16:04:42 -0500 Subject: [PATCH] Add useEntityRequest hook --- .../entity-store/hooks/useEntityRequest.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/soapbox/entity-store/hooks/useEntityRequest.ts diff --git a/app/soapbox/entity-store/hooks/useEntityRequest.ts b/app/soapbox/entity-store/hooks/useEntityRequest.ts new file mode 100644 index 0000000000..7d2697e61f --- /dev/null +++ b/app/soapbox/entity-store/hooks/useEntityRequest.ts @@ -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(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 }; \ No newline at end of file