-
-
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 #10257 from marmelab/optional-authprovider-getperm…
…issions Make authProvider.getPermissions optional
- Loading branch information
Showing
5 changed files
with
184 additions
and
84 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
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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as React from 'react'; | ||
import usePermissions, { UsePermissionsResult } from './usePermissions'; | ||
import { QueryClient } from '@tanstack/react-query'; | ||
import { AuthProvider, CoreAdminContext, TestMemoryRouter } from '..'; | ||
|
||
export default { | ||
title: 'ra-core/auth/usePermissions', | ||
}; | ||
|
||
export const NoAuthProvider = () => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext> | ||
<UsePermissions>{state => inspectState(state)}</UsePermissions> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
export const NoAuthProviderGetPermissions = () => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext | ||
authProvider={{ | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: () => Promise.reject('bad method'), | ||
checkError: () => Promise.reject('bad method'), | ||
}} | ||
> | ||
<UsePermissions>{state => inspectState(state)}</UsePermissions> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
export const WithAuthProviderAndGetPermissions = ({ | ||
authProvider = { | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: () => Promise.reject('bad method'), | ||
checkError: () => Promise.reject('bad method'), | ||
getPermissions: () => | ||
new Promise(resolve => setTimeout(resolve, 300, 'admin')), | ||
}, | ||
queryClient, | ||
}: { | ||
authProvider?: AuthProvider; | ||
queryClient?: QueryClient; | ||
}) => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext authProvider={authProvider} queryClient={queryClient}> | ||
<UsePermissions>{state => inspectState(state)}</UsePermissions> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
const UsePermissions = ({ | ||
children, | ||
}: { | ||
children: (state: UsePermissionsResult<any, Error>) => React.ReactNode; | ||
}) => { | ||
const permissionQueryParams = { | ||
retry: false, | ||
}; | ||
const res = usePermissions({}, permissionQueryParams); | ||
return children(res); | ||
}; | ||
|
||
const inspectState = (state: UsePermissionsResult<any, Error>) => ( | ||
<div> | ||
{state.isPending ? <span>LOADING</span> : null} | ||
{state.permissions ? ( | ||
<span>PERMISSIONS: {state.permissions}</span> | ||
) : null} | ||
{state.error ? <span>ERROR</span> : null} | ||
</div> | ||
); |
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
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