|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import isEqual from 'lodash/isEqual'; |
| 3 | + |
| 4 | +import useGetPermissions from './useGetPermissions'; |
| 5 | +import { useSafeSetState } from '../util/hooks'; |
| 6 | + |
| 7 | +interface State { |
| 8 | + permissions?: any; |
| 9 | + error?: any; |
| 10 | +} |
| 11 | + |
| 12 | +const emptyParams = {}; |
| 13 | + |
| 14 | +// keep a cache of already fetched permissions to initialize state for new |
| 15 | +// components and avoid a useless rerender if the permissions haven't changed |
| 16 | +const alreadyFetchedPermissions = { '{}': [] }; |
| 17 | + |
| 18 | +/** |
| 19 | + * Hook for getting user permissions without the loading state. |
| 20 | + * |
| 21 | + * When compared to usePermissions, this hook doesn't cause a re-render |
| 22 | + * when the permissions haven't changed since the last call. |
| 23 | + * |
| 24 | + * This hook doesn't handle the loading state. |
| 25 | + * |
| 26 | + * @see usePermissions |
| 27 | + * |
| 28 | + * Calls the authProvider.getPermissions() method asynchronously. |
| 29 | + * If the authProvider returns a rejected promise, returns empty permissions. |
| 30 | + * |
| 31 | + * The return value updates according to the request state: |
| 32 | + * |
| 33 | + * - start: { permissions: [previously fetched permissions for these params] } |
| 34 | + * - success: { permissions: [permissions returned by the authProvider (usually the same as on start)] } |
| 35 | + * - error: { error: [error from provider] } |
| 36 | + * |
| 37 | + * Useful to enable features based on user permissions |
| 38 | + * |
| 39 | + * @param {Object} params Any params you want to pass to the authProvider |
| 40 | + * |
| 41 | + * @returns The current auth check state. Destructure as { permissions, error }. |
| 42 | + * |
| 43 | + * @example |
| 44 | + * import { usePermissionsOptimized } from 'react-admin'; |
| 45 | + * |
| 46 | + * const PostDetail = props => { |
| 47 | + * const { permissions } = usePermissionsOptimized(); |
| 48 | + * if (permissions !== 'editor') { |
| 49 | + * return <Redirect to={`posts/${props.id}/show`} /> |
| 50 | + * } else { |
| 51 | + * return <PostEdit {...props} /> |
| 52 | + * } |
| 53 | + * }; |
| 54 | + */ |
| 55 | +const usePermissionsOptimized = (params = emptyParams) => { |
| 56 | + const [state, setState] = useSafeSetState<State>({ |
| 57 | + permissions: alreadyFetchedPermissions[JSON.stringify(params)], |
| 58 | + }); |
| 59 | + const getPermissions = useGetPermissions(); |
| 60 | + useEffect(() => { |
| 61 | + getPermissions(params) |
| 62 | + .then(permissions => { |
| 63 | + const key = JSON.stringify(params); |
| 64 | + if (!isEqual(permissions, alreadyFetchedPermissions[key])) { |
| 65 | + alreadyFetchedPermissions[key] = permissions; |
| 66 | + setState({ permissions }); |
| 67 | + } |
| 68 | + }) |
| 69 | + .catch(error => { |
| 70 | + setState({ |
| 71 | + error, |
| 72 | + }); |
| 73 | + }); |
| 74 | + }, [getPermissions, params, setState]); |
| 75 | + |
| 76 | + return state; |
| 77 | +}; |
| 78 | + |
| 79 | +export default usePermissionsOptimized; |
0 commit comments