Skip to content

Instantly share code, notes, and snippets.

@francois-roget
Created May 25, 2021 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francois-roget/699c9cf0c34b384df292f4e5e25dcb0f to your computer and use it in GitHub Desktop.
Save francois-roget/699c9cf0c34b384df292f4e5e25dcb0f to your computer and use it in GitHub Desktop.
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