import get from 'lodash/get' ; import { useCallback , useEffect , useRef } from 'react' ; import isEqual from 'lodash/isEqual' ; import { useSafeSetState , removeEmpty } from '../../util' ; import { useGetManyReference } from '../../dataProvider' ; import { useNotify } from '../../sideEffect' ; import { Record , SortPayload , RecordMap } from '../../types' ; import { ListControllerProps } from '../useListController' ; import usePaginationState from '../usePaginationState' ; import useSelectionState from '../useSelectionState' ; import useSortState from '../useSortState' ; import { useResourceContext } from '../../core' ; interface Options { basePath ? : string ; data ? : RecordMap ; filter ? : any ; ids ? : any [ ] ; loaded ? : boolean ; page ? : number ; perPage ? : number ; record ? : Record ; reference : string ; resource : string ; sort ? : SortPayload ; source ? : string ; target : string ; total ? : number ; } const defaultFilter = { } ; example param props param { string } props . resource param { string } props . reference param { Object } props . record param { string } props . target param { Object } props . filter param { string } props . source param { string } props . basePath param { number } props . page param { number } props . perPage param { Object } props . sort returns { ReferenceManyProps } const useReferenceManyFieldController = ( props ) : ListControllerProps => { const { reference , record , target , filter = defaultFilter , source , basePath , page : initialPage , perPage : initialPerPage , sort : initialSort = { field : 'id' , order : 'DESC' } , } = props ; const resource = useResourceContext ( props ) ; const notify = useNotify ( ) ; const { page , setPage , perPage , setPerPage } = usePaginationState ( { page : initialPage , perPage : initialPerPage , } ) ; const { sort , setSort : setSortObject } = useSortState ( initialSort ) ; const setSort = useCallback ( ( field , order = 'ASC' ) => { setSortObject ( { field , order } ) ; setPage ( 1 ) ; } , [ setPage , setSortObject ] ) ; const { selectedIds , onSelect , onToggleItem , onUnselectItems , } = useSelectionState ( ) ; const filterRef = useRef ( filter ) ; const [ displayedFilters , setDisplayedFilters ] = useSafeSetState < { [ key ] : boolean ; } > ( { } ) ; const [ filterValues , setFilterValues ] = useSafeSetState < { [ key ] : any ; } > ( filter ) ; const hideFilter = useCallback ( ( filterName ) => { setDisplayedFilters ( previousState => { const { [ filterName ] : _ , ... newState } = previousState ; return newState ; } ) ; setFilterValues ( previousState => { const { [ filterName ] : _ , ... newState } = previousState ; return newState ; } ) ; } , [ setDisplayedFilters , setFilterValues ] ) ; const showFilter = useCallback ( ( filterName , defaultValue ) => { setDisplayedFilters ( previousState => ( { ... previousState , [ filterName ] : true , } ) ) ; setFilterValues ( previousState => ( { ... previousState , [ filterName ] : defaultValue , } ) ) ; } , [ setDisplayedFilters , setFilterValues ] ) ; const setFilters = useCallback ( ( filters , displayedFilters ) => { setFilterValues ( removeEmpty ( filters ) ) ; setDisplayedFilters ( displayedFilters ) ; setPage ( 1 ) ; } , [ setDisplayedFilters , setFilterValues , setPage ] ) ; useEffect ( ( ) => { if ( ! isEqual ( filter , filterRef . current ) ) { filterRef . current = filter ; setFilterValues ( filter ) ; } } ) ; const referenceId = get ( record , source ) ; const { data , ids , total , error , loading , loaded , refetch , } = useGetManyReference ( reference , target , referenceId , { page , perPage } , sort , filterValues , resource , { 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 , } , } ) , } ) ; return { basePath : basePath ? basePath . replace ( resource , reference ) : `/${ reference }` , currentSort : sort , data , defaultTitle : null , displayedFilters , error , filterValues , hasCreate : false , hideFilter , ids , loaded , loading , onSelect , onToggleItem , onUnselectItems , page , perPage , refetch , resource : reference , selectedIds , setFilters , setPage , setPerPage , setSort , showFilter , total , } ; } ; export default useReferenceManyFieldController ;