Created
May 25, 2021 06:06
-
-
Save francois-roget/699c9cf0c34b384df292f4e5e25dcb0f to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import {Permission} from "../Types"; | |
import PermissionContext from "./PermissionContext"; | |
type Props = { | |
permissions: Permission[] | |
} | |
// This provider is intended to be surrounding the whole application. | |
// It should receive the users permissions as parameter | |
const PermissionProvider: React.FunctionComponent<Props> = ({permissions, children}) => { | |
// Creates a method that returns whether the requested permission is available in the list of permissions | |
// passed as parameter | |
const isAllowedTo = (permission: Permission) => permissions.includes(permission); | |
// This component will render its children wrapped around a PermissionContext's provider whose | |
// value is set to the method defined above | |
return <PermissionContext.Provider value={{isAllowedTo}}>{children}</PermissionContext.Provider>; | |
}; | |
export default PermissionProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment