@pactsafe/pactsafe-react-sdk
Version:
PactSafe React SDK - React SDK for easy Clickwrap/Browsewrap implementation leveraging PactSafe's JavaScript API
105 lines (84 loc) • 3.96 kB
JavaScript
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 */
import React from 'react';
import PropTypes from 'prop-types';
import isRequiredIf from 'react-proptype-conditional-require';
import PSSnippet from './PSSnippet';
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 (!PSSnippet.isSnippetLoaded(psScriptUrl)) {
PSSnippet.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 React.createElement(
'a',
{ href: link, id: this.targetSelector },
linkText
);
};
return PSBrowseWrap;
}(React.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: PropTypes.string.isRequired,
alwaysVisible: PropTypes.bool,
badgeText: PropTypes.string,
groupKey: PropTypes.string.isRequired,
link: isRequiredIf(PropTypes.string, function (props) {
return props.hasOwnProperty('openLegalCenter') && props.openLegalCenter === false;
}, PSBrowseWrap.MUST_PROVIDE_LINK_IF_OPEN_LEGAL_CENTER_FALSE),
linkText: PropTypes.string.isRequired,
openLegalCenter: PropTypes.bool,
position: PropTypes.oneOf(['middle', 'left', 'right', 'auto']),
psScriptUrl: PropTypes.string
} : {};
PSBrowseWrap.defaultProps = {
psScriptUrl: '//vault.pactsafe.io/ps.min.js',
position: 'auto',
link: '#',
openLegalCenter: true
};
export default PSBrowseWrap;