Last active
July 28, 2020 07:09
-
-
Save bogdanq/f6d9612dee369d0221dde3904fb3b73d to your computer and use it in GitHub Desktop.
access - компонент для проверки прав юзера
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
export const Access = ({ guards = [], children, name = "" }) => { | |
const { checkPermissions } = usePermissions(); | |
const { loading } = useUser(); | |
const { hiddenPages, hiddenComponents } = React.useContext( | |
configContext, | |
); | |
const hasCompletedGuards = React.useMemo(() => checkPermissions(guards), [ | |
guards, | |
checkPermissions, | |
]); | |
if ( | |
loading || | |
hiddenPages.includes(name) || | |
hiddenComponents.includes(name) | |
) { | |
return null; | |
} | |
return hasCompletedGuards ? children : null; | |
}; | |
const usePermissions = () => { | |
const { user } = useUser(); | |
const checkPermissions = React.useCallback( | |
guards => { | |
return guards.every(item => item({ user })); | |
}, | |
[user], | |
); | |
return { checkPermissions }; | |
}; | |
// <Access name="sendmail" guards={[onlyAuth]}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment