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>,
|
createFn: CreateFn<Params, Result>,
|
||||||
opts: UseCreateEntityOpts<TEntity> = {},
|
opts: UseCreateEntityOpts<TEntity> = {},
|
||||||
) {
|
) {
|
||||||
const path = parseEntitiesPath(expandedPath);
|
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||||
const [entityType, listKey] = path;
|
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
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`.
|
* To remove an entity globally from all lists, see `useDeleteEntity`.
|
||||||
*/
|
*/
|
||||||
function useDismissEntity<T = unknown>(expandedPath: ExpandedEntitiesPath, dismissFn: DismissFn<T>) {
|
function useDismissEntity<T = unknown>(expandedPath: ExpandedEntitiesPath, dismissFn: DismissFn<T>) {
|
||||||
const path = parseEntitiesPath(expandedPath);
|
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||||
const [entityType, listKey] = path;
|
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
|
|
@ -39,9 +39,7 @@ function useEntities<TEntity extends Entity>(
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const getState = useGetState();
|
const getState = useGetState();
|
||||||
|
|
||||||
const path = parseEntitiesPath(expandedPath);
|
const { entityType, listKey, path } = parseEntitiesPath(expandedPath);
|
||||||
const [entityType, listKey] = path;
|
|
||||||
|
|
||||||
const entities = useAppSelector(state => selectEntities<TEntity>(state, path));
|
const entities = useAppSelector(state => selectEntities<TEntity>(state, path));
|
||||||
|
|
||||||
const isEnabled = opts.enabled ?? true;
|
const isEnabled = opts.enabled ?? true;
|
||||||
|
|
|
@ -22,8 +22,7 @@ function useEntityActions<TEntity extends Entity = Entity, Params = any>(
|
||||||
opts: UseEntityActionsOpts<TEntity> = {},
|
opts: UseEntityActionsOpts<TEntity> = {},
|
||||||
) {
|
) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const path = parseEntitiesPath(expandedPath);
|
const { entityType, path } = parseEntitiesPath(expandedPath);
|
||||||
const [entityType] = path;
|
|
||||||
|
|
||||||
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
|
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
|
||||||
if (!endpoints.delete) return Promise.reject(endpoints);
|
if (!endpoints.delete) return Promise.reject(endpoints);
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
import type { EntitiesPath, ExpandedEntitiesPath } from './types';
|
import type { EntitiesPath, ExpandedEntitiesPath } from './types';
|
||||||
|
|
||||||
function parseEntitiesPath(expandedPath: ExpandedEntitiesPath): EntitiesPath {
|
function parseEntitiesPath(expandedPath: ExpandedEntitiesPath) {
|
||||||
const [entityType, ...listKeys] = expandedPath;
|
const [entityType, ...listKeys] = expandedPath;
|
||||||
const listKey = (listKeys || []).join(':');
|
const listKey = (listKeys || []).join(':');
|
||||||
return [entityType, listKey];
|
const path: EntitiesPath = [entityType, listKey];
|
||||||
|
|
||||||
|
return {
|
||||||
|
entityType,
|
||||||
|
listKey,
|
||||||
|
path,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export { parseEntitiesPath };
|
export { parseEntitiesPath };
|
Loading…
Reference in a new issue