From 1b569b6c826d00cdbb25e54bfea982533fa78f9a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Mar 2023 15:15:04 -0500 Subject: [PATCH] useEntity: accept an EntityRequest object --- app/soapbox/entity-store/hooks/useEntity.ts | 23 +++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/soapbox/entity-store/hooks/useEntity.ts b/app/soapbox/entity-store/hooks/useEntity.ts index aa7b40b5d..cb71b86a9 100644 --- a/app/soapbox/entity-store/hooks/useEntity.ts +++ b/app/soapbox/entity-store/hooks/useEntity.ts @@ -5,8 +5,10 @@ import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks'; import { importEntities } from '../actions'; +import { toAxiosRequest } from './utils'; + import type { Entity } from '../types'; -import type { EntitySchema, EntityPath } from './types'; +import type { EntitySchema, EntityPath, EntityRequest } from './types'; /** Additional options for the hook. */ interface UseEntityOpts { @@ -18,7 +20,7 @@ interface UseEntityOpts { function useEntity( path: EntityPath, - endpoint: string, + request: EntityRequest, opts: UseEntityOpts = {}, ) { const api = useApi(); @@ -34,15 +36,18 @@ function useEntity( const [isFetching, setIsFetching] = useState(false); const isLoading = isFetching && !entity; - const fetchEntity = () => { + const fetchEntity = async () => { setIsFetching(true); - api.get(endpoint).then(({ data }) => { - const entity = schema.parse(data); + + try { + const response = await api.request(toAxiosRequest(request)); + const entity = schema.parse(response.data); dispatch(importEntities([entity], entityType)); - setIsFetching(false); - }).catch(() => { - setIsFetching(false); - }); + } catch (e) { + // do nothing + } + + setIsFetching(false); }; useEffect(() => {