Created
July 9, 2020 16:33
-
-
Save lucasdu4rte/819bb0b4dde8641df7f730f724dcc885 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { useSelector } from 'react-redux' | |
function useAccessControl(mode, page) { | |
const accessControl = useSelector((state) => state.accessControl.data) || {} | |
const user = useSelector((state) => state.user.profile) || {} | |
function can(action) { | |
console.log(`accessControl.${mode}.${page}.${action}`) | |
return Boolean(accessControl[mode][page][action]) | |
} | |
function is(role) { | |
const currentUserRole = user.hasAccess ? 'admin' : 'user' | |
return role === currentUserRole | |
} | |
return { can, is } | |
} | |
export default useAccessControl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment