bigbuffet-rw/packages/pl-fe/src/entity-store/hooks/useDismissEntity.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

33 lines
959 B
TypeScript

import { useAppDispatch } from 'pl-fe/hooks/useAppDispatch';
import { useLoading } from 'pl-fe/hooks/useLoading';
import { dismissEntities } from '../actions';
import { parseEntitiesPath } from './utils';
import type { EntityFn, ExpandedEntitiesPath } from './types';
/**
* Removes an entity from a specific list.
* To remove an entity globally from all lists, see `useDeleteEntity`.
*/
const useDismissEntity = (expandedPath: ExpandedEntitiesPath, entityFn: EntityFn<string>) => {
const dispatch = useAppDispatch();
const [isLoading, setPromise] = useLoading();
const { entityType, listKey } = parseEntitiesPath(expandedPath);
// TODO: optimistic dismissing
const dismissEntity = async (entityId: string) => {
const result = await setPromise(entityFn(entityId));
dispatch(dismissEntities([entityId], entityType, listKey));
return result;
};
return {
dismissEntity,
isLoading,
};
};
export { useDismissEntity };