From 45c12e9b65f73b98353b49e7ad5371a051fd72d1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Mar 2023 16:09:04 -0500 Subject: [PATCH] Make EntityCallbacks a generic function --- app/soapbox/entity-store/hooks/types.ts | 7 +++++++ app/soapbox/entity-store/hooks/useCreateEntity.ts | 9 ++------- app/soapbox/entity-store/hooks/useDeleteEntity.ts | 13 ++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/soapbox/entity-store/hooks/types.ts b/app/soapbox/entity-store/hooks/types.ts index 8bd788d93..e9df8c18b 100644 --- a/app/soapbox/entity-store/hooks/types.ts +++ b/app/soapbox/entity-store/hooks/types.ts @@ -25,6 +25,12 @@ type EntitiesPath = [entityType: string, listKey: string] /** Used to look up a single entity by its ID. */ type EntityPath = [entityType: string, entityId: string] +/** Callback functions for entity actions. */ +interface EntityCallbacks { + onSuccess?(value: Value): void + onError?(error: Error): void +} + /** * Passed into hooks to make requests. * Can be a URL for GET requests, or a request object. @@ -36,5 +42,6 @@ export type { ExpandedEntitiesPath, EntitiesPath, EntityPath, + EntityCallbacks, EntityRequest, }; \ No newline at end of file diff --git a/app/soapbox/entity-store/hooks/useCreateEntity.ts b/app/soapbox/entity-store/hooks/useCreateEntity.ts index 8c4aac94a..87625f942 100644 --- a/app/soapbox/entity-store/hooks/useCreateEntity.ts +++ b/app/soapbox/entity-store/hooks/useCreateEntity.ts @@ -8,17 +8,12 @@ import { importEntities } from '../actions'; import { parseEntitiesPath, toAxiosRequest } from './utils'; import type { Entity } from '../types'; -import type { EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types'; +import type { EntityCallbacks, EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types'; interface UseCreateEntityOpts { schema?: EntitySchema } -interface CreateEntityCallbacks { - onSuccess?(entity: TEntity): void - onError?(error: Error): void -} - function useCreateEntity( expandedPath: ExpandedEntitiesPath, request: EntityRequest, @@ -30,7 +25,7 @@ function useCreateEntity( const { entityType, listKey } = parseEntitiesPath(expandedPath); - async function createEntity(data: Data, callbacks: CreateEntityCallbacks = {}): Promise { + async function createEntity(data: Data, callbacks: EntityCallbacks = {}): Promise { setIsLoading(true); try { diff --git a/app/soapbox/entity-store/hooks/useDeleteEntity.ts b/app/soapbox/entity-store/hooks/useDeleteEntity.ts index e7220e746..718ca5e11 100644 --- a/app/soapbox/entity-store/hooks/useDeleteEntity.ts +++ b/app/soapbox/entity-store/hooks/useDeleteEntity.ts @@ -6,12 +6,7 @@ import { deleteEntities, importEntities } from '../actions'; import { toAxiosRequest } from './utils'; -import type { EntityRequest } from './types'; - -interface DeleteEntityCallbacks { - onSuccess?(): void - onError?(): void -} +import type { EntityCallbacks, EntityRequest } from './types'; /** * Optimistically deletes an entity from the store. @@ -27,7 +22,7 @@ function useDeleteEntity( const getState = useGetState(); const [isLoading, setIsLoading] = useState(false); - async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise { + async function deleteEntity(entityId: string, callbacks: EntityCallbacks = {}): Promise { setIsLoading(true); // Get the entity before deleting, so we can reverse the action if the API request fails. @@ -47,7 +42,7 @@ function useDeleteEntity( dispatch(deleteEntities([entityId], entityType)); if (callbacks.onSuccess) { - callbacks.onSuccess(); + callbacks.onSuccess(entityId); } } catch (e) { if (entity) { @@ -56,7 +51,7 @@ function useDeleteEntity( } if (callbacks.onError) { - callbacks.onError(); + callbacks.onError(e); } }