Improve API of parseEntitiesPath
This commit is contained in:
parent
8f67d2c76f
commit
61fb434a54
5 changed files with 12 additions and 11 deletions
|
@ -35,8 +35,7 @@ function useCreateEntity<TEntity extends Entity = Entity, Params = any, Result =
|
|||
createFn: CreateFn<Params, Result>,
|
||||
opts: UseCreateEntityOpts<TEntity> = {},
|
||||
) {
|
||||
const path = parseEntitiesPath(expandedPath);
|
||||
const [entityType, listKey] = path;
|
||||
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
|
|
|
@ -13,8 +13,7 @@ type DismissFn<T> = (entityId: string) => Promise<T> | T;
|
|||
* To remove an entity globally from all lists, see `useDeleteEntity`.
|
||||
*/
|
||||
function useDismissEntity<T = unknown>(expandedPath: ExpandedEntitiesPath, dismissFn: DismissFn<T>) {
|
||||
const path = parseEntitiesPath(expandedPath);
|
||||
const [entityType, listKey] = path;
|
||||
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
|
|
|
@ -39,9 +39,7 @@ function useEntities<TEntity extends Entity>(
|
|||
const dispatch = useAppDispatch();
|
||||
const getState = useGetState();
|
||||
|
||||
const path = parseEntitiesPath(expandedPath);
|
||||
const [entityType, listKey] = path;
|
||||
|
||||
const { entityType, listKey, path } = parseEntitiesPath(expandedPath);
|
||||
const entities = useAppSelector(state => selectEntities<TEntity>(state, path));
|
||||
|
||||
const isEnabled = opts.enabled ?? true;
|
||||
|
|
|
@ -22,8 +22,7 @@ function useEntityActions<TEntity extends Entity = Entity, Params = any>(
|
|||
opts: UseEntityActionsOpts<TEntity> = {},
|
||||
) {
|
||||
const api = useApi();
|
||||
const path = parseEntitiesPath(expandedPath);
|
||||
const [entityType] = path;
|
||||
const { entityType, path } = parseEntitiesPath(expandedPath);
|
||||
|
||||
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
|
||||
if (!endpoints.delete) return Promise.reject(endpoints);
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
import type { EntitiesPath, ExpandedEntitiesPath } from './types';
|
||||
|
||||
function parseEntitiesPath(expandedPath: ExpandedEntitiesPath): EntitiesPath {
|
||||
function parseEntitiesPath(expandedPath: ExpandedEntitiesPath) {
|
||||
const [entityType, ...listKeys] = expandedPath;
|
||||
const listKey = (listKeys || []).join(':');
|
||||
return [entityType, listKey];
|
||||
const path: EntitiesPath = [entityType, listKey];
|
||||
|
||||
return {
|
||||
entityType,
|
||||
listKey,
|
||||
path,
|
||||
};
|
||||
}
|
||||
|
||||
export { parseEntitiesPath };
|
Loading…
Reference in a new issue