Created
May 25, 2021 06:12
-
-
Save francois-roget/2ab66c7ceb38c5717686f5cd53d725f8 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 usePermission from "./usePermission"; | |
type Props = { | |
to: Permission; | |
fallback?: JSX.Element | string; | |
}; | |
// This component is meant to be used everywhere a restriction based on user permission is needed | |
const Restricted: React.FunctionComponent<Props> = ({to, fallback, children}) => { | |
// We "connect" to the provider thanks to the permission hook | |
const allowed = usePermission(to); | |
// If the user has that permission, render the children | |
if(allowed){ | |
return <>{children}</>; | |
} | |
// Otherwise, render the fallback | |
return <>{fallback}</>; | |
}; | |
export default Restricted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment