Skip to content

Commit

Permalink
Merge pull request #7642 from marmelab/fix-use-configure-admin-router…
Browse files Browse the repository at this point in the history
…-from-children-permissions

Simplify useConfigureAdminRouterFromChildren permissions
  • Loading branch information
fzaninotto authored May 5, 2022
2 parents 77b526a + 572014d commit dc6bce6
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions packages/ra-core/src/core/useConfigureAdminRouterFromChildren.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useEffect,
useState,
} from 'react';
import { useLogout, useGetPermissions, useAuthState } from '../auth';
import { useLogout, usePermissions, useAuthState } from '../auth';
import { useSafeSetState } from '../util';
import {
AdminChildren,
Expand Down Expand Up @@ -43,7 +43,7 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext';
export const useConfigureAdminRouterFromChildren = (
children: AdminChildren
): RoutesAndResources & { status: AdminRouterStatus } => {
const permissions = usePermissionsWithLogout();
const { permissions } = usePermissions();

// Whenever children are updated, update our custom routes and resources
const [routesAndResources, status] = useRoutesAndResourcesFromChildren(
Expand All @@ -62,25 +62,6 @@ export const useConfigureAdminRouterFromChildren = (
};
};

const usePermissionsWithLogout = () => {
const getPermissions = useGetPermissions();
const doLogout = useLogout();
const [permissions, setPermissions] = useState<any>();

useEffect(() => {
getPermissions()
.then(newPermissions => {
setPermissions(newPermissions);
})
.catch(error => {
console.error(error);
doLogout();
});
}, [doLogout, getPermissions]);

return permissions;
};

/**
* A hook that determine the routes and resources from React nodes and permissions.
* Returns a tuple with the routes and resources as a single object, and the status.
Expand Down

0 comments on commit dc6bce6

Please sign in to comment.