Files
ncd-fe/packages/pl-fe/src/entity-store/hooks/useDeleteEntity.ts
marcin mikołajczak 966b04fdf0 Call it pl-fe internally
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-08-28 13:41:08 +02:00

54 lines
1.7 KiB
TypeScript

import { useAppDispatch } from 'pl-fe/hooks/useAppDispatch';
import { useGetState } from 'pl-fe/hooks/useGetState';
import { useLoading } from 'pl-fe/hooks/useLoading';
import { deleteEntities, importEntities } from '../actions';
import type { EntityCallbacks, EntityFn } from './types';
/**
* Optimistically deletes an entity from the store.
* This hook should be used to globally delete an entity from all lists.
* To remove an entity from a single list, see `useDismissEntity`.
*/
const useDeleteEntity = (entityType: string, entityFn: EntityFn<string>) => {
const dispatch = useAppDispatch();
const getState = useGetState();
const [isSubmitting, setPromise] = useLoading();
const deleteEntity = async (entityId: string, callbacks: EntityCallbacks<string> = {}): Promise<void> => {
// Get the entity before deleting, so we can reverse the action if the API request fails.
const entity = getState().entities[entityType]?.store[entityId];
// Optimistically delete the entity from the _store_ but keep the lists in tact.
dispatch(deleteEntities([entityId], entityType, { preserveLists: true }));
try {
await setPromise(entityFn(entityId));
// Success - finish deleting entity from the state.
dispatch(deleteEntities([entityId], entityType));
if (callbacks.onSuccess) {
callbacks.onSuccess(entityId);
}
} catch (e) {
if (entity) {
// If the API failed, reimport the entity.
dispatch(importEntities([entity], entityType));
}
if (callbacks.onError) {
callbacks.onError(e);
}
}
};
return {
deleteEntity,
isSubmitting,
};
};
export { useDeleteEntity };