Skip to content

Commit

Permalink
Merge pull request #5829 from marmelab/ensure-resource-definition-ove…
Browse files Browse the repository at this point in the history
…rridable-with-props

Ensure Resource Definition is Overridable with Props
  • Loading branch information
fzaninotto committed Jan 28, 2021
2 parents 2ea0259 + 6f892bf commit 9b06f92
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/ra-core/src/core/useResourceDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import { useSelector } from 'react-redux';
import merge from 'lodash/merge';
import { getResources } from '../reducer';
import { ResourceDefinition } from '../types';
import { useResourceContext } from './useResourceContext';
import { useMemo } from 'react';

/**
* Hook which returns the definition of the requested resource
*/
export const useResourceDefinition = (props): ResourceDefinition => {
export const useResourceDefinition = (
props: UseResourceDefinitionOptions
): ResourceDefinition => {
const resource = useResourceContext(props);
const resources = useSelector(getResources);
const { hasCreate, hasEdit, hasList, hasShow } = props;

return resources.find(r => r?.name === resource) || props;
const definition = useMemo(() => {
const definitionFromRedux = resources.find(r => r?.name === resource);
return merge({}, definitionFromRedux, {
hasCreate,
hasEdit,
hasList,
hasShow,
});
}, [resource, resources, hasCreate, hasEdit, hasList, hasShow]);

return definition;
};

export interface UseResourceDefinitionOptions {
readonly resource?: string;
readonly options?: any;
readonly hasList?: boolean;
readonly hasEdit?: boolean;
readonly hasShow?: boolean;
readonly hasCreate?: boolean;
readonly icon?: any;
}

0 comments on commit 9b06f92

Please sign in to comment.