Skip to content

Instantly share code, notes, and snippets.

import { generateDoorsContent } from "./utils";
const generateDoors = () => {
const initialContent = generateDoorsContent();
return [
{ behind: initialContent[0], isOpen: false },
{ behind: initialContent[1], isOpen: false },
{ behind: initialContent[2], isOpen: false }
];
};
<PermissionsGate
errorProps={{ disabled: true }}
scopes={[SCOPES.canEdit]}
>
<Input />
</PermissionsGate>
export default function PermissionsGate({
children,
RenderError = () => <></>,
errorProps = null,
scopes = []
}) {
const { role } = useGetRole();
const permissions = PERMISSIONS[role];
const permissionGranted = hasPermission({ permissions, scopes });
<PermissionsGate
RenderError={() => <p>You shall not pass!</p>}
scopes={[SCOPES.canCreate]}
>
<img alt="" className="vault-image" src={VAULT} />
<h1>Private content</h1>
<p>Must be an editor to view</p>
</PermissionsGate>
export default function PermissionsGate({
children,
RenderError = () => <></>,
errorProps = null,
scopes = []
}) {
const { role } = useGetRole();
const permissions = PERMISSIONS[role];
const permissionGranted = hasPermission({ permissions, scopes });
<PermissionsGate
scopes={[SCOPES.canEdit]}
>
<img alt="" className="vault-image" src={VAULT} />
<h1>Private content</h1>
<p>Must be an editor to view</p>
</PermissionsGate>
export const ROLES = {
viewer: "VIEWER",
editor: "EDITOR",
owner: "OWNER"
};
export const SCOPES = {
canCreate: "can-create",
canEdit: "can-edit",
canDelete: "can-delete",
import { cloneElement } from "react";
import { PERMISSIONS } from "./permission-maps";
import { useGetRole } from "./useGetRole";
const hasPermission = ({ permissions, scopes }) => {
const scopesMap = {};
scopes.forEach((scope) => {
scopesMap[scope] = true;
});
processInventory = (action, widget) => {
displayLoader();
return this.updateInventories(action, widget);
};
updateInventories = (action, widget) => {
switch (action) {
case "REQUEST": {
const { first, inventoryItems } = this.requestInventory(
this.inventoryItems
processInventory = (widget) => {
displayLoader();
switch (this.action) {
case "REQUEST": {
const { first, inventoryItems } = this.requestInventory(
this.inventoryItems
);
this.inventoryItems = inventoryItems;
return first;
}