-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8521 from marmelab/deprecate-usePermissionsOptimised
Deprecate usePermissionsOptimised
- Loading branch information
Showing
3 changed files
with
5 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 0 additions & 111 deletions
111
packages/ra-core/src/auth/usePermissionsOptimized.spec.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,14 @@ | ||
import { useEffect } from 'react'; | ||
import isEqual from 'lodash/isEqual'; | ||
|
||
import useGetPermissions from './useGetPermissions'; | ||
import { useSafeSetState } from '../util/hooks'; | ||
|
||
interface State { | ||
permissions?: any; | ||
error?: any; | ||
} | ||
import usePermissions from './usePermissions'; | ||
|
||
const emptyParams = {}; | ||
|
||
// keep a cache of already fetched permissions to initialize state for new | ||
// components and avoid a useless rerender if the permissions haven't changed | ||
const alreadyFetchedPermissions = { '{}': undefined }; | ||
|
||
/** | ||
* Hook for getting user permissions without the loading state. | ||
* | ||
* When compared to usePermissions, this hook doesn't cause a re-render | ||
* when the permissions haven't changed since the last call. | ||
* | ||
* This hook doesn't handle the loading state. | ||
* @deprecated use usePermissions instead | ||
* | ||
* @see usePermissions | ||
* | ||
* Calls the authProvider.getPermissions() method asynchronously. | ||
* If the authProvider returns a rejected promise, returns empty permissions. | ||
* | ||
* The return value updates according to the request state: | ||
* | ||
* - start: { permissions: [previously fetched permissions for these params] } | ||
* - success: { permissions: [permissions returned by the authProvider (usually the same as on start)] } | ||
* - error: { error: [error from provider] } | ||
* | ||
* Useful to enable features based on user permissions | ||
* | ||
* @param {Object} params Any params you want to pass to the authProvider | ||
* | ||
* @returns The current auth check state. Destructure as { permissions, error }. | ||
* | ||
* @example | ||
* import { usePermissionsOptimized } from 'react-admin'; | ||
* | ||
* const PostDetail = props => { | ||
* const { permissions } = usePermissionsOptimized(); | ||
* if (permissions !== 'editor') { | ||
* return <Redirect to={`posts/${props.id}/show`} /> | ||
* } else { | ||
* return <PostEdit {...props} /> | ||
* } | ||
* }; | ||
*/ | ||
const usePermissionsOptimized = (params = emptyParams) => { | ||
const key = JSON.stringify(params); | ||
const [state, setState] = useSafeSetState<State>({ | ||
permissions: alreadyFetchedPermissions[key], | ||
}); | ||
const getPermissions = useGetPermissions(); | ||
useEffect(() => { | ||
getPermissions(params) | ||
.then(permissions => { | ||
if (!isEqual(permissions, state.permissions)) { | ||
alreadyFetchedPermissions[key] = permissions; | ||
setState({ permissions }); | ||
} | ||
}) | ||
.catch(error => { | ||
setState({ | ||
error, | ||
}); | ||
}); | ||
}, [getPermissions, key]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
return state; | ||
return usePermissions(params); | ||
}; | ||
|
||
export default usePermissionsOptimized; |