Skip to content

Instantly share code, notes, and snippets.

View francois-roget's full-sized avatar

François Roget francois-roget

  • Worldline Financial Solutions
  • Brussels, Belgium
View GitHub Profile
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);
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
import React from 'react';
import {Permission} from "../Types";
import PermissionContext from "./PermissionContext";
type Props = {
fetchPermission: (p: Permission) => Promise<boolean>
}
type PermissionCache = {
[key:string]: boolean;
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
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;
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
<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>
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}) => {
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
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