Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate usePermissionsOptimised #8521

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ra-core/src/auth/WithPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Location } from 'react-router-dom';

import warning from '../util/warning';
import { useAuthenticated } from './useAuthenticated';
import usePermissionsOptimized from './usePermissionsOptimized';
import usePermissions from './usePermissions';

export interface WithPermissionsChildrenParams {
permissions: any;
Expand Down Expand Up @@ -76,7 +76,7 @@ const WithPermissions = (props: WithPermissionsProps) => {
);

useAuthenticated(authParams);
const { permissions } = usePermissionsOptimized(authParams);
const { permissions } = usePermissions(authParams);
// render even though the usePermissions() call isn't finished (optimistic rendering)
if (component) {
return createElement(component, { permissions, ...rest });
Expand Down
111 changes: 0 additions & 111 deletions packages/ra-core/src/auth/usePermissionsOptimized.spec.tsx

This file was deleted.

71 changes: 3 additions & 68 deletions packages/ra-core/src/auth/usePermissionsOptimized.ts
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;