UNPKG

11.3 kBJavaScriptView Raw
1var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _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; }
6
7function _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; }
8
9/* global _ps */
10import React from 'react';
11import PropTypes from 'prop-types';
12import isRequiredIf from 'react-proptype-conditional-require';
13import uuid from 'uuid/v4';
14import PSSnippet from './PSSnippet';
15
16var PSClickWrap = function (_React$Component) {
17 _inherits(PSClickWrap, _React$Component);
18
19 function PSClickWrap(props) {
20 _classCallCheck(this, PSClickWrap);
21
22 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
23
24 _this.createClickWrap = _this.createClickWrap.bind(_this);
25 _this.state = {
26 clickwrapGroupKey: null,
27 dynamicGroup: false
28 };
29 _this.propsEventMap = {
30 onAll: 'all',
31 onSent: 'sent',
32 onRetrieved: 'retrieved',
33 onSet: 'set',
34 onSetSignerId: 'set:signer_id',
35 onValid: 'valid',
36 onInvalid: 'invalid',
37 onRendered: 'rendered',
38 onDisplayed: 'displayed',
39 onScrolledContract: 'scrolled:contract',
40 onScrolled: 'scrolled',
41 onError: 'error'
42 };
43 var _this$props = _this.props,
44 psScriptUrl = _this$props.psScriptUrl,
45 backupScriptURL = _this$props.backupScriptURL,
46 accessId = _this$props.accessId,
47 testMode = _this$props.testMode,
48 disableSending = _this$props.disableSending,
49 dynamic = _this$props.dynamic,
50 signerId = _this$props.signerId,
51 debug = _this$props.debug;
52
53 if (!PSSnippet.isSnippetLoaded(psScriptUrl, backupScriptURL)) {
54 PSSnippet.injectSnippet(psScriptUrl, backupScriptURL);
55 }
56 _ps('create', accessId, {
57 test_mode: testMode,
58 disable_sending: disableSending,
59 dynamic: dynamic,
60 signer_id: signerId
61 });
62 if (debug) {
63 _ps.debug = true;
64 }
65 return _this;
66 }
67
68 PSClickWrap.prototype.componentDidMount = function componentDidMount() {
69 this.createClickWrap();
70 };
71
72 PSClickWrap.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
73 var _props = this.props,
74 clickWrapStyle = _props.clickWrapStyle,
75 renderData = _props.renderData,
76 filter = _props.filter,
77 groupKey = _props.groupKey;
78 var _state = this.state,
79 clickwrapGroupKey = _state.clickwrapGroupKey,
80 dynamicGroup = _state.dynamicGroup;
81
82 if (clickWrapStyle !== prevProps.clickWrapStyle && !dynamicGroup) {
83 _ps.getByKey(clickwrapGroupKey).site.set('style', clickWrapStyle);
84 _ps.getByKey(clickwrapGroupKey).retrieveHTML();
85 }
86 if (renderData !== prevProps.renderData) {
87 _ps(clickwrapGroupKey + ':retrieveHTML', renderData);
88 }
89 if (clickWrapStyle !== prevProps.clickWrapStyle && dynamicGroup) {
90 this.createClickWrap();
91 }
92 if (filter !== prevProps.filter && dynamicGroup) {
93 this.createClickWrap();
94 }
95 if (groupKey !== prevProps.groupKey && !dynamicGroup) {
96 this.createClickWrap();
97 _ps.getByKey(clickwrapGroupKey).retrieveHTML();
98 }
99 };
100
101 PSClickWrap.prototype.componentWillUnmount = function componentWillUnmount() {
102 var groupKey = this.props.groupKey;
103
104 if (_ps && _ps.getByKey(groupKey) && _ps.getByKey(groupKey).rendered) {
105 _ps.getByKey(groupKey).rendered = false;
106 }
107 this.unregisterEventListeners();
108 };
109
110 PSClickWrap.prototype.registerEventListener = function registerEventListener(eventProp, groupKey) {
111 var _this2 = this;
112
113 var eventCallbackFn = function eventCallbackFn() {
114 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
115 args[_key] = arguments[_key];
116 }
117
118 var shouldFireListener = false;
119 args.forEach(function (arg) {
120 // We need to check the context variable and see if it matches the groupKey, if it does -> fire the event (context argument position varies)
121 if (arg.get && arg.get('key') && arg.get('key') === groupKey) {
122 shouldFireListener = true;
123 // Else we should check if the context is for the entire site, and as such the context variable is a Site object.
124 } else if (arg.toString() === '[object Site]') {
125 shouldFireListener = true;
126 }
127 });
128 if (shouldFireListener) {
129 var _props2;
130
131 (_props2 = _this2.props)[eventProp].apply(_props2, args);
132 }
133 };
134 // In order to handle unregistration of event listeners, we override the toString function to identify the
135 // function by a UUID rather than the default toString of a function.
136 var newEventListenerID = uuid();
137 eventCallbackFn.toString = function () {
138 return newEventListenerID;
139 };
140 _ps.on(this.propsEventMap[eventProp], eventCallbackFn);
141 return eventCallbackFn.toString();
142 };
143
144 PSClickWrap.prototype.registerEventListeners = function registerEventListeners(groupKey) {
145 var _this3 = this;
146
147 var eventListeners = {};
148 Object.keys(this.propsEventMap).forEach(function (eventProp) {
149 if (_this3.props[eventProp]) {
150 eventListeners[_this3.propsEventMap[eventProp]] = _this3.registerEventListener(eventProp, groupKey);
151 }
152 });
153 // Store event listeners in state so we can unregister them later on unmount
154 this.setState({ eventListeners: eventListeners });
155 };
156
157 PSClickWrap.prototype.unregisterEventListeners = function unregisterEventListeners() {
158 var _this4 = this;
159
160 Object.keys(this.state.eventListeners).forEach(function (event) {
161 var eventUUID = _this4.state.eventListeners[event];
162 // In order to unregister the event, we must create a fake function (typeof passed to _ps.off must be a function),
163 // that returns the UUID we want to unregister.
164 var fakeEventListener = function fakeEventListener() {
165 return eventUUID;
166 };
167 fakeEventListener.toString = function () {
168 return eventUUID;
169 };
170 _ps.off(event, fakeEventListener);
171 });
172 };
173
174 PSClickWrap.prototype.createClickWrap = function createClickWrap() {
175 var _this5 = this;
176
177 var _props3 = this.props,
178 filter = _props3.filter,
179 containerId = _props3.containerId,
180 signerIdSelector = _props3.signerIdSelector,
181 clickWrapStyle = _props3.clickWrapStyle,
182 displayAll = _props3.displayAll,
183 renderData = _props3.renderData,
184 displayImmediately = _props3.displayImmediately,
185 forceScroll = _props3.forceScroll,
186 groupKey = _props3.groupKey,
187 confirmationEmail = _props3.confirmationEmail;
188
189 var options = { filter: filter, container_selector: containerId, confirmation_email: confirmationEmail, signer_id_selector: signerIdSelector, style: clickWrapStyle, display_all: displayAll, render_data: renderData, auto_run: displayImmediately, force_scroll: forceScroll };
190
191 if (groupKey) this.setState({ clickwrapGroupKey: groupKey, dynamicGroup: false });
192 var isDynamic = !groupKey;
193
194 var eventCallback = function eventCallback(err, group) {
195 if (group) {
196 var key = groupKey || group.get('key');
197
198 var state = { clickwrapGroupKey: key };
199 if (isDynamic) state.dynamicGroup = true;
200 _this5.setState(state);
201
202 if (!isDynamic) group.render();
203 _this5.registerEventListeners(key);
204 }
205 };
206
207 if (groupKey) _ps('load', groupKey, _extends({}, options, { event_callback: eventCallback }));else _ps('load', _extends({}, options, { event_callback: eventCallback }));
208 };
209
210 PSClickWrap.prototype.render = function render() {
211 var containerId = this.props.containerId;
212
213 return React.createElement('div', { id: containerId });
214 };
215
216 return PSClickWrap;
217}(React.Component);
218
219PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE = 'PSClickWrap Error: You must provide either a groupKey or filter prop in order to use the PactSafe ClickWrap component!';
220PSClickWrap.MUST_PROVIDE_RENDER_DATA_ERROR_MESSAGE = 'PSClickWrap Error: You must provide a renderData prop when passing down the dynamic prop';
221PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR = 'PSClickWrap Error: You must provide either a signer ID or a signer ID selector';
222
223PSClickWrap.propTypes = process.env.NODE_ENV !== "production" ? {
224 accessId: PropTypes.string.isRequired,
225 clickWrapStyle: PropTypes.oneOf(['full', 'scroll', 'checkbox', 'combined', 'embedded']),
226 confirmationEmail: PropTypes.bool,
227 disableSending: PropTypes.bool,
228 displayAll: PropTypes.bool,
229 displayImmediately: PropTypes.bool,
230 dynamic: PropTypes.bool,
231 containerId: PropTypes.string,
232 filter: isRequiredIf(PropTypes.string, function (props) {
233 return !props.hasOwnProperty('groupKey');
234 }, PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE),
235 forceScroll: PropTypes.bool,
236 groupKey: isRequiredIf(PropTypes.string, function (props) {
237 return !props.hasOwnProperty('filter');
238 }, PSClickWrap.FILTER_OR_GROUPKEY_REQUIRED_ERROR_MESSAGE),
239 psScriptUrl: PropTypes.string,
240 backupScriptURL: PropTypes.string,
241 renderData: isRequiredIf(PropTypes.object, function (props) {
242 return props.hasOwnProperty('dynamic') && props.dynamic === true;
243 }, PSClickWrap.MUST_PROVIDE_RENDER_DATA_ERROR_MESSAGE),
244 signerIdSelector: isRequiredIf(PropTypes.string, function (props) {
245 return !props.hasOwnProperty('signerId');
246 }, PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR),
247 signerId: isRequiredIf(PropTypes.string, function (props) {
248 return !props.hasOwnProperty('signerIdSelector');
249 }, PSClickWrap.MUST_PROVIDE_SIGNER_ID_OR_SIGNER_ID_SELECTOR),
250 testMode: PropTypes.bool,
251 debug: PropTypes.bool,
252 onAll: PropTypes.func,
253 onSent: PropTypes.func,
254 onRetrieved: PropTypes.func,
255 onSet: PropTypes.func,
256 onSetSignerId: PropTypes.func,
257 onValid: PropTypes.func,
258 onInvalid: PropTypes.func,
259 onRendered: PropTypes.func,
260 onDisplayed: PropTypes.func,
261 onScrolledContract: PropTypes.func,
262 onScrolled: PropTypes.func,
263 onError: PropTypes.func
264} : {};
265
266PSClickWrap.defaultProps = {
267 psScriptUrl: '//vault.pactsafe.io/ps.min.js',
268 backupScriptURL: '//d3l1mqnl5xpsuc.cloudfront.net/ps.min.js',
269 containerId: 'ps-clickwrap',
270 displayImmediately: true,
271 disableSending: false,
272 displayAll: true,
273 dynamic: false,
274 testMode: false
275};
276
277export default PSClickWrap;
\No newline at end of file