import get from 'lodash/get' ; import { Record , SortPayload } from '../../types' ; import { useGetMany } from '../../dataProvider' ; import { ListControllerProps } from '../useListController' ; import { useNotify } from '../../sideEffect' ; import { useResourceContext } from '../../core' ; import { useList } from '../useList' ; interface Option { basePath ? : string ; filter ? : any ; page ? : number ; perPage ? : number ; record ? : Record ; reference : string ; resource : string ; sort ? : SortPayload ; source : string ; } const emptyArray = [ ] ; const defaultFilter = { } ; const defaultSort = { field : null , order : null } ; example param props param { string } props . basePath param { Object } props . record param { string } props . reference param { string } props . resource param { string } props . source param { Props } props returns { ReferenceArrayProps } const useReferenceArrayFieldController = ( props ) : ListControllerProps => { const { basePath , filter = defaultFilter , page = 1 , perPage = 1000 , record , reference , sort = defaultSort , source , } = props ; const resource = useResourceContext ( props ) ; const notify = useNotify ( ) ; const ids = get ( record , source ) || emptyArray ; const { data , error , loading , loaded , refetch } = useGetMany ( reference , ids , { onFailure : error => notify ( typeof error === 'string' ? error : error . message || 'ra.notification.http_error' , { type : 'warning' , messageArgs : { _ : typeof error === 'string' ? error : error && error . message ? error . message : undefined , } , } ) , } ) ; const listProps = useList ( { data , error , filter , ids , loaded , loading , page , perPage , sort , } ) ; return { basePath : basePath ? basePath . replace ( resource , reference ) : `/${ reference }` , ... listProps , defaultTitle : null , hasCreate : false , refetch , resource : reference , } ; } ; export default useReferenceArrayFieldController ;