-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathuseResourceDefinition.ts
43 lines (39 loc) · 1.27 KB
/
useResourceDefinition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useSelector } from 'react-redux';
import defaults from 'lodash/defaults';
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: UseResourceDefinitionOptions
): ResourceDefinition => {
const resource = useResourceContext(props);
const resources = useSelector(getResources);
const { hasCreate, hasEdit, hasList, hasShow } = props;
const definition = useMemo(() => {
const definitionFromRedux = resources.find(r => r?.name === resource);
return defaults(
{},
{
hasCreate,
hasEdit,
hasList,
hasShow,
},
definitionFromRedux
);
}, [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;
}