import { useEffect } from 'react' ; import useGetPermissions from './useGetPermissions' ; import { useSafeSetState } from '../util/hooks' ; interface State < Permissions , Error > { loading : boolean ; loaded : boolean ; permissions ? : Permissions ; error ? : Error ; } const emptyParams = { } ; param { Object } params returns example const usePermissions = < Permissions = any , Error = any > ( params = emptyParams ) : State < Permissions , Error > => { const [ state , setState ] = useSafeSetState < State < Permissions , Error > > ( { loading : true , loaded : false , } ) ; const getPermissions = useGetPermissions ( ) ; useEffect ( ( ) => { getPermissions ( params ) . then ( permissions => { setState ( { loading : false , loaded : true , permissions } ) ; } ) . catch ( error => { setState ( { loading : false , loaded : true , error , } ) ; } ) ; } , [ getPermissions , params , setState ] ) ; return state ; } ; export default usePermissions ;