UNPKG

react-permissions-dynamic

Version:
25 lines (24 loc) 1.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PermissionCheck = void 0; const __1 = require(".."); const createPayload_1 = require("./createPayload"); const checkIfAllowed_1 = require("./checkIfAllowed"); const defaultAllowLogic_1 = require("./defaultAllowLogic"); const PermissionCheck = ({ children, action, onDenied, loading = null, fallback = null, isAllowed = defaultAllowLogic_1.defaultAllowLogic, }) => { const onActionDenied = (status) => { if (!status.checked || status.allowed) return; return onDenied === null || onDenied === void 0 ? void 0 : onDenied(status.action); }; const payload = (0, createPayload_1.createPayload)(action); const actionsStatus = (0, __1.useCheckPermissions)(payload, onActionDenied); const checked = actionsStatus.every(s => !!s.checked); if (!checked) return loading; const allowed = (0, checkIfAllowed_1.checkIfAllowed)(actionsStatus, isAllowed, payload); if (!allowed) return fallback; return children; }; exports.PermissionCheck = PermissionCheck;