import { useState , useCallback , ReactEventHandler , SyntheticEvent , } from 'react' ; import { useDelete } from '../../dataProvider' ; import { CRUD_DELETE } from '../../actions' ; import { useRefresh , useNotify , useRedirect , RedirectionSideEffect , } from '../../sideEffect' ; import { Record , MutationMode , OnFailure , OnSuccess } from '../../types' ; import { useResourceContext } from '../../core' ; example const useDeleteWithConfirmController = ( props ) : UseDeleteWithConfirmControllerReturn => { const { record , redirect : redirectTo , basePath , mutationMode , onClick , onSuccess , onFailure , } = props ; const resource = useResourceContext ( props ) ; const [ open , setOpen ] = useState ( false ) ; const notify = useNotify ( ) ; const redirect = useRedirect ( ) ; const refresh = useRefresh ( ) ; const [ deleteOne , { loading } ] = useDelete ( resource , null , null , { action : CRUD_DELETE , onSuccess : response => { setOpen ( false ) ; if ( onSuccess === undefined ) { notify ( 'ra.notification.deleted' , { type : 'info' , messageArgs : { smart_count : 1 } , undoable : mutationMode === 'undoable' , } ) ; redirect ( redirectTo , basePath || `/${ resource }` ) ; refresh ( ) ; } else { onSuccess ( response ) ; } } , onFailure : error => { setOpen ( false ) ; if ( onFailure === undefined ) { notify ( typeof error === 'string' ? error : error . message || 'ra.notification.http_error' , { type : 'warning' , messageArgs : { _ : typeof error === 'string' ? error : error && error . message ? error . message : undefined , } , } ) ; refresh ( ) ; } else { onFailure ( error ) ; } } , mutationMode , } ) ; const handleDialogOpen = e => { setOpen ( true ) ; e . stopPropagation ( ) ; } ; const handleDialogClose = e => { setOpen ( false ) ; e . stopPropagation ( ) ; } ; const handleDelete = useCallback ( event => { event . stopPropagation ( ) ; deleteOne ( { payload : { id : record . id , previousData : record } , } ) ; if ( typeof onClick === 'function' ) { onClick ( event ) ; } } , [ deleteOne , onClick , record ] ) ; return { open , loading , handleDialogOpen , handleDialogClose , handleDelete } ; } ; export interface UseDeleteWithConfirmControllerParams { basePath ? : string ; mutationMode ? : MutationMode ; record ? : Record ; redirect ? : RedirectionSideEffect ; resource ? : string ; onClick ? : ReactEventHandler < any > ; onSuccess ? : OnSuccess ; onFailure ? : OnFailure ; } export interface UseDeleteWithConfirmControllerReturn { open : boolean ; loading : boolean ; handleDialogOpen : ( e : SyntheticEvent ) => void ; handleDialogClose : ( e : SyntheticEvent ) => void ; handleDelete : ReactEventHandler < any > ; } export default useDeleteWithConfirmController ;