import useVersion from '../useVersion' ; import { useCheckMinimumRequiredProps } from '../checkMinimumRequiredProps' ; import { Record , Identifier , OnFailure } from '../../types' ; import { useGetOne , Refetch } from '../../dataProvider' ; import { useTranslate } from '../../i18n' ; import { useNotify , useRedirect , useRefresh } from '../../sideEffect' ; import { CRUD_GET_ONE } from '../../actions' ; import { useResourceContext , useGetResourceLabel } from '../../core' ; export interface ShowProps { basePath ? : string ; hasCreate ? : boolean ; hasEdit ? : boolean ; hasShow ? : boolean ; hasList ? : boolean ; id ? : Identifier ; onFailure ? : OnFailure ; resource ? : string ; [ key ] : any ; } export interface ShowControllerProps < RecordType extends Record = Record > { basePath ? : string ; defaultTitle : string ; data ? : RecordType ; error ? : any ; loading : boolean ; loaded : boolean ; hasCreate ? : boolean ; hasEdit ? : boolean ; hasList ? : boolean ; hasShow ? : boolean ; resource : string ; record ? : RecordType ; refetch : Refetch ; version : number ; } param { Object } props return { Object } example export const useShowController = < RecordType extends Record = Record > ( props ) : ShowControllerProps < RecordType > => { useCheckMinimumRequiredProps ( 'Show' , [ 'basePath' , 'resource' ] , props ) ; const { basePath , hasCreate , hasEdit , hasList , hasShow , id , onFailure , } = props ; const resource = useResourceContext ( props ) ; const translate = useTranslate ( ) ; const notify = useNotify ( ) ; const redirect = useRedirect ( ) ; const refresh = useRefresh ( ) ; const version = useVersion ( ) ; const { data : record , error , loading , loaded , refetch } = useGetOne < RecordType > ( resource , id , { action : CRUD_GET_ONE , onFailure : onFailure ?? ( ( ) => { notify ( 'ra.notification.item_doesnt_exist' , { type : 'warning' , } ) ; redirect ( 'list' , basePath ) ; refresh ( ) ; } ) , } ) ; const getResourceLabel = useGetResourceLabel ( ) ; const defaultTitle = translate ( 'ra.page.show' , { name : getResourceLabel ( resource , 1 ) , id , record , } ) ; return { error , loading , loaded , defaultTitle , resource , basePath , record , refetch , hasCreate , hasEdit , hasList , hasShow , version , } ; } ;