Make EntityCallbacks a generic function

This commit is contained in:
Alex Gleason 2023-03-23 16:09:04 -05:00
parent 7248331742
commit 45c12e9b65
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 13 additions and 16 deletions

View File

@ -25,6 +25,12 @@ type EntitiesPath = [entityType: string, listKey: string]
/** Used to look up a single entity by its ID. */ /** Used to look up a single entity by its ID. */
type EntityPath = [entityType: string, entityId: string] type EntityPath = [entityType: string, entityId: string]
/** Callback functions for entity actions. */
interface EntityCallbacks<Value, Error = unknown> {
onSuccess?(value: Value): void
onError?(error: Error): void
}
/** /**
* Passed into hooks to make requests. * Passed into hooks to make requests.
* Can be a URL for GET requests, or a request object. * Can be a URL for GET requests, or a request object.
@ -36,5 +42,6 @@ export type {
ExpandedEntitiesPath, ExpandedEntitiesPath,
EntitiesPath, EntitiesPath,
EntityPath, EntityPath,
EntityCallbacks,
EntityRequest, EntityRequest,
}; };

View File

@ -8,17 +8,12 @@ import { importEntities } from '../actions';
import { parseEntitiesPath, toAxiosRequest } from './utils'; import { parseEntitiesPath, toAxiosRequest } from './utils';
import type { Entity } from '../types'; import type { Entity } from '../types';
import type { EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types'; import type { EntityCallbacks, EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types';
interface UseCreateEntityOpts<TEntity extends Entity = Entity> { interface UseCreateEntityOpts<TEntity extends Entity = Entity> {
schema?: EntitySchema<TEntity> schema?: EntitySchema<TEntity>
} }
interface CreateEntityCallbacks<TEntity extends Entity = Entity, Error = unknown> {
onSuccess?(entity: TEntity): void
onError?(error: Error): void
}
function useCreateEntity<TEntity extends Entity = Entity, Data = any>( function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
expandedPath: ExpandedEntitiesPath, expandedPath: ExpandedEntitiesPath,
request: EntityRequest, request: EntityRequest,
@ -30,7 +25,7 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
const { entityType, listKey } = parseEntitiesPath(expandedPath); const { entityType, listKey } = parseEntitiesPath(expandedPath);
async function createEntity(data: Data, callbacks: CreateEntityCallbacks = {}): Promise<void> { async function createEntity(data: Data, callbacks: EntityCallbacks<TEntity> = {}): Promise<void> {
setIsLoading(true); setIsLoading(true);
try { try {

View File

@ -6,12 +6,7 @@ import { deleteEntities, importEntities } from '../actions';
import { toAxiosRequest } from './utils'; import { toAxiosRequest } from './utils';
import type { EntityRequest } from './types'; import type { EntityCallbacks, EntityRequest } from './types';
interface DeleteEntityCallbacks {
onSuccess?(): void
onError?(): void
}
/** /**
* Optimistically deletes an entity from the store. * Optimistically deletes an entity from the store.
@ -27,7 +22,7 @@ function useDeleteEntity(
const getState = useGetState(); const getState = useGetState();
const [isLoading, setIsLoading] = useState<boolean>(false); const [isLoading, setIsLoading] = useState<boolean>(false);
async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise<void> { async function deleteEntity(entityId: string, callbacks: EntityCallbacks<string> = {}): Promise<void> {
setIsLoading(true); setIsLoading(true);
// Get the entity before deleting, so we can reverse the action if the API request fails. // 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)); dispatch(deleteEntities([entityId], entityType));
if (callbacks.onSuccess) { if (callbacks.onSuccess) {
callbacks.onSuccess(); callbacks.onSuccess(entityId);
} }
} catch (e) { } catch (e) {
if (entity) { if (entity) {
@ -56,7 +51,7 @@ function useDeleteEntity(
} }
if (callbacks.onError) { if (callbacks.onError) {
callbacks.onError(); callbacks.onError(e);
} }
} }