UNPKG

@pactsafe/pactsafe-react-sdk

Version:

PactSafe React SDK - React SDK for easy Clickwrap/Browsewrap implementation leveraging PactSafe's JavaScript API

123 lines (91 loc) 4.6 kB
'use strict'; exports.__esModule = true; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactProptypeConditionalRequire = require('react-proptype-conditional-require'); var _reactProptypeConditionalRequire2 = _interopRequireDefault(_reactProptypeConditionalRequire); var _PSSnippet = require('./PSSnippet'); var _PSSnippet2 = _interopRequireDefault(_PSSnippet); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global _ps */ var PSBrowseWrap = function (_React$Component) { _inherits(PSBrowseWrap, _React$Component); function PSBrowseWrap(props) { _classCallCheck(this, PSBrowseWrap); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); var _this$props = _this.props, psScriptUrl = _this$props.psScriptUrl, groupKey = _this$props.groupKey, accessId = _this$props.accessId; if (!_PSSnippet2.default.isSnippetLoaded(psScriptUrl)) { _PSSnippet2.default.injectSnippet(psScriptUrl); } _this.targetSelector = 'psbw-' + groupKey; _ps('create', accessId); return _this; } PSBrowseWrap.prototype.componentDidMount = function componentDidMount() { var _props = this.props, groupKey = _props.groupKey, position = _props.position, badgeText = _props.badgeText, alwaysVisible = _props.alwaysVisible, openLegalCenter = _props.openLegalCenter; _ps('load', groupKey, { target_selector: this.targetSelector, position: position, badge_text: badgeText, always_visible: alwaysVisible, open_legal_center: openLegalCenter }); }; PSBrowseWrap.prototype.componentWillUnmount = function componentWillUnmount() { var groupKey = this.props.groupKey; _ps.getByKey(groupKey).rendered = false; }; PSBrowseWrap.prototype.isSnippetLoaded = function isSnippetLoaded() { var psScriptUrl = this.props.psScriptUrl; var scripts = document.getElementsByTagName('script'); for (var i = 0; i < scripts.length; i += 1) { if (scripts[i].src.indexOf(psScriptUrl) !== -1) return true; } return false; }; PSBrowseWrap.prototype.render = function render() { var _props2 = this.props, link = _props2.link, linkText = _props2.linkText; return _react2.default.createElement( 'a', { href: link, id: this.targetSelector }, linkText ); }; return PSBrowseWrap; }(_react2.default.Component); PSBrowseWrap.MUST_PROVIDE_LINK_IF_OPEN_LEGAL_CENTER_FALSE = 'PSBrowseWrap Error: You must provide a link prop if openLegalCenter is passed false'; PSBrowseWrap.propTypes = process.env.NODE_ENV !== "production" ? { accessId: _propTypes2.default.string.isRequired, alwaysVisible: _propTypes2.default.bool, badgeText: _propTypes2.default.string, groupKey: _propTypes2.default.string.isRequired, link: (0, _reactProptypeConditionalRequire2.default)(_propTypes2.default.string, function (props) { return props.hasOwnProperty('openLegalCenter') && props.openLegalCenter === false; }, PSBrowseWrap.MUST_PROVIDE_LINK_IF_OPEN_LEGAL_CENTER_FALSE), linkText: _propTypes2.default.string.isRequired, openLegalCenter: _propTypes2.default.bool, position: _propTypes2.default.oneOf(['middle', 'left', 'right', 'auto']), psScriptUrl: _propTypes2.default.string } : {}; PSBrowseWrap.defaultProps = { psScriptUrl: '//vault.pactsafe.io/ps.min.js', position: 'auto', link: '#', openLegalCenter: true }; exports.default = PSBrowseWrap; module.exports = exports['default'];