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 {useContext, useState} from 'react'; | |
import PermissionContext from "./PermissionContext"; | |
import {Permission} from "../Types"; | |
const usePermission = (permission: Permission) => { | |
const [loading, setLoading] = useState(true); | |
const [allowed, setAllowed] = useState<boolean>(); | |
const {isAllowedTo} = useContext(PermissionContext); |
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
type Props = { | |
to: Permission; | |
fallback?: JSX.Element | string; | |
loadingComponent?: 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, loadingComponent, children}) => { | |
// We "connect" to the provider thanks to the PermissionContext |
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 = { | |
fetchPermission: (p: Permission) => Promise<boolean> | |
} | |
type PermissionCache = { | |
[key:string]: boolean; |
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 |
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 {useContext} from 'react'; | |
import PermissionContext from "./PermissionContext"; | |
import {Permission} from "../Types"; | |
const usePermission = (permission: Permission) => { | |
const {isAllowedTo} = useContext(PermissionContext); | |
return isAllowedTo(permission); | |
} | |
export default usePermission; |
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, {useContext} from 'react'; | |
import PermissionContext from "./PermissionContext"; | |
import {Permission} from "../Types"; | |
type Props = { | |
to: Permission; | |
fallback?: JSX.Element | string; | |
}; | |
// This component is meant to be used everywhere a restriction based on user permission is needed |
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
<PermissionProvider permissions={currentUser.permissions}> | |
. . . | |
<Restricted to="list.elements"> | |
<ElementList elements={elements} addElement={addElement} removeElement={removeElement}/> | |
</Restricted> | |
<div className="container"> | |
<table className="table table-sm table-hover"> | |
<thead className="thead-light"> | |
<tr> |
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, {useContext} from 'react'; | |
import PermissionContext from "./PermissionContext"; | |
import {Permission} from "../Types"; | |
type Props = { | |
to: Permission; | |
}; | |
// This component is meant to be used everywhere a restriction based on user permission is needed | |
const Restricted: React.FunctionComponent<Props> = ({to, children}) => { |
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 |
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"; | |
type PermissionContextType = { | |
isAllowedTo: (permission: Permission) => boolean; | |
} | |
// Default behaviour for the Permission Provider Context | |
// i.e. if for whatever reason the consumer is used outside of a provider. | |
// The permission will not be granted unless a provider says otherwise |