Make EntityCallbacks a generic function
This commit is contained in:
parent
7248331742
commit
45c12e9b65
|
@ -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<Value, Error = unknown> {
|
||||
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,
|
||||
};
|
|
@ -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<TEntity extends Entity = Entity> {
|
||||
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>(
|
||||
expandedPath: ExpandedEntitiesPath,
|
||||
request: EntityRequest,
|
||||
|
@ -30,7 +25,7 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
|
|||
|
||||
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);
|
||||
|
||||
try {
|
||||
|
|
|
@ -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<boolean>(false);
|
||||
|
||||
async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise<void> {
|
||||
async function deleteEntity(entityId: string, callbacks: EntityCallbacks<string> = {}): Promise<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue