-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default Unauthorized component to ra-ui-materialui
- Loading branch information
Showing
10 changed files
with
146 additions
and
9 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,6 +1,3 @@ | ||
import { createContext } from 'react'; | ||
import { UnauthorizedComponent } from '../types'; | ||
import { ComponentType, createContext } from 'react'; | ||
|
||
export const UnauthorizedContext = createContext<UnauthorizedComponent>( | ||
() => null | ||
); | ||
export const UnauthorizedContext = createContext<ComponentType>(() => null); |
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
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
74 changes: 74 additions & 0 deletions
74
packages/ra-ui-materialui/src/layout/Unauthorized.stories.tsx
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 polyglotI18nProvider from 'ra-i18n-polyglot'; | ||
import englishMessages from 'ra-language-english'; | ||
import frenchMessages from 'ra-language-french'; | ||
import { | ||
I18nContextProvider, | ||
Resource, | ||
testDataProvider, | ||
TestMemoryRouter, | ||
} from 'ra-core'; | ||
|
||
import { AdminContext } from '../AdminContext'; | ||
import { AdminUI } from '../AdminUI'; | ||
import { Unauthorized } from './Unauthorized'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
export default { | ||
title: 'ra-ui-materialui/layout/Unauthorized', | ||
}; | ||
|
||
const i18nProvider = polyglotI18nProvider( | ||
locale => (locale === 'fr' ? frenchMessages : englishMessages), | ||
'en', | ||
[ | ||
{ locale: 'en', name: 'English' }, | ||
{ locale: 'fr', name: 'Français' }, | ||
] | ||
); | ||
|
||
export const Basic = () => <Unauthorized />; | ||
|
||
export const I18N = () => { | ||
return ( | ||
<TestMemoryRouter> | ||
<I18nContextProvider value={i18nProvider}> | ||
<Unauthorized /> | ||
</I18nContextProvider> | ||
</TestMemoryRouter> | ||
); | ||
}; | ||
|
||
const authProvider = { | ||
login: () => Promise.resolve(), | ||
logout: () => Promise.resolve(), | ||
checkAuth: () => Promise.resolve(), | ||
checkError: () => Promise.resolve(), | ||
getPermissions: () => Promise.resolve(), | ||
canAccess: ({ resource }) => Promise.resolve(resource === 'posts'), | ||
}; | ||
|
||
export const FullApp = () => ( | ||
<TestMemoryRouter> | ||
<AdminContext | ||
dataProvider={testDataProvider()} | ||
authProvider={authProvider} | ||
i18nProvider={i18nProvider} | ||
> | ||
<AdminUI> | ||
<Resource name="users" list={UserList} /> | ||
<Resource name="posts" list={PostList} /> | ||
</AdminUI> | ||
</AdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
const UserList = () => <div style={{ marginTop: 10 }}>User list</div>; | ||
const PostList = () => ( | ||
<div style={{ marginTop: 10 }}> | ||
<div>Post list</div> | ||
<div> | ||
<Link to="/users">User list</Link> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { Typography, SxProps } from '@mui/material'; | ||
import { useTranslate } from 'ra-core'; | ||
|
||
export const Unauthorized = (props: UnauthorizedProps) => { | ||
const { | ||
className, | ||
unauthorizedPrimary = 'ra.page.unauthorized', | ||
unauthorizedSecondary = 'ra.message.unauthorized', | ||
...rest | ||
} = props; | ||
const translate = useTranslate(); | ||
return ( | ||
<Root className={className} {...rest}> | ||
<div className={UnauthorizedClasses.message}> | ||
<Typography variant="h5" mt={3} color="text.secondary"> | ||
{translate(unauthorizedPrimary, { _: unauthorizedPrimary })} | ||
</Typography> | ||
<Typography variant="body2"> | ||
{translate(unauthorizedSecondary, { | ||
_: unauthorizedSecondary, | ||
})} | ||
</Typography> | ||
</div> | ||
</Root> | ||
); | ||
}; | ||
|
||
export interface UnauthorizedProps { | ||
className?: string; | ||
unauthorizedPrimary?: string; | ||
unauthorizedSecondary?: string; | ||
sx?: SxProps; | ||
} | ||
|
||
const PREFIX = 'RaUnauthorized'; | ||
|
||
export const UnauthorizedClasses = { | ||
root: `${PREFIX}-root`, | ||
message: `${PREFIX}-message`, | ||
}; | ||
|
||
const Root = styled('div', { | ||
name: PREFIX, | ||
overridesResolver: (props, styles) => styles.root, | ||
})(({ theme }) => ({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
height: '100%', | ||
[`& .${UnauthorizedClasses.message}`]: { | ||
textAlign: 'center', | ||
fontFamily: 'Roboto, sans-serif', | ||
color: theme.palette.text.disabled, | ||
paddingTop: '1em', | ||
paddingBottom: '1em', | ||
}, | ||
})); |