Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Resource Definition is Overridable with Props #5829

Merged
merged 4 commits into from
Jan 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}