Repository URL to install this package:
|
Version:
0.4.16 ▾
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useEffect, useState } from 'react';
import { Button, Col } from 'react-bootstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSync } from '@fortawesome/free-solid-svg-icons';
import withAuth from './withAuth';
import { isFunction } from 'lodash';
import './SecureActionButton.css';
var SecureActionButton = function SecureActionButton(_ref) {
var isLoading = _ref.isLoading,
text = _ref.text,
loadingText = _ref.loadingText,
allowed = _ref.allowed,
_ref$show = _ref.show,
show = _ref$show === void 0 ? true : _ref$show,
_ref$asCol = _ref.asCol,
asCol = _ref$asCol === void 0 ? true : _ref$asCol,
_ref$disabled = _ref.disabled,
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
props = _objectWithoutProperties(_ref, ["isLoading", "text", "loadingText", "allowed", "show", "asCol", "disabled"]);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
authorized = _useState2[0],
setAuthorized = _useState2[1];
useEffect(function () {
function checkAuthorization() {
return _checkAuthorization.apply(this, arguments);
}
function _checkAuthorization() {
_checkAuthorization = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee() {
var authenticated, _authorized;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return props.isAuthenticated();
case 2:
authenticated = _context.sent;
if (!authenticated) {
_context.next = 10;
break;
}
_context.next = 6;
return props.isAuthorizedFor(allowed, "action-".concat(text));
case 6:
_authorized = _context.sent;
setAuthorized(_authorized);
_context.next = 11;
break;
case 10:
setAuthorized(false);
case 11:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _checkAuthorization.apply(this, arguments);
}
checkAuthorization();
}, [allowed, props, text]);
var shouldShow = function shouldShow(show) {
if (isFunction(show)) {
return show();
} else {
return show;
}
};
if (authorized && shouldShow(show)) {
if (asCol) {
return React.createElement(Col, {
className: "secureActionButton"
}, React.createElement(Button, Object.assign({
disabled: disabled || isLoading
}, props), isLoading && React.createElement(FontAwesomeIcon, {
icon: faSync,
spin: true
}), !isLoading ? text : loadingText));
} else {
return React.createElement("div", {
className: "secureActionButton"
}, React.createElement(Button, Object.assign({
disabled: disabled || isLoading
}, props), isLoading && React.createElement(FontAwesomeIcon, {
icon: faSync,
spin: true
}), !isLoading ? text : loadingText));
}
} else {
return null;
}
};
export default withAuth(SecureActionButton);
//# sourceMappingURL=SecureActionButton.js.map