Skip to content

Instantly share code, notes, and snippets.

@bogdanq
Last active July 28, 2020 07:09
Show Gist options
  • Save bogdanq/f6d9612dee369d0221dde3904fb3b73d to your computer and use it in GitHub Desktop.
Save bogdanq/f6d9612dee369d0221dde3904fb3b73d to your computer and use it in GitHub Desktop.
access - компонент для проверки прав юзера
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