-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widgets.ts
335 lines (303 loc) 路 9.82 KB
/
Widgets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { debug } from "debug";
import WidgetApi from "../api/WidgetApi";
import {
ConfigOptions,
EngagementMedium,
WidgetConfig,
WidgetContext,
WidgetResult,
WithRequired,
} from "../types";
import {
validateConfig,
validatePasswordlessConfig,
validateWidgetConfig,
} from "../utils/validate";
import EmbedWidget from "./EmbedWidget";
import PopupWidget from "./PopupWidget";
import Widget, { Params } from "./Widget";
const _log = debug("squatch-js:widgets");
/**
* `Widgets` is a factory for creating widgets. It's possible to build your own widgets using the
* {@link WidgetApi} but most people will prefer to use these easy methods.
* @class
*/
export default class Widgets {
/**
* Instance of {@link WidgetApi}
*/
api: WidgetApi;
/**
* Tenant alias of SaaSquatch tenant
*/
tenantAlias: string;
/**
* SaaSquatch domain for API requests
* @default "https://app.referralsaasquatch.com"
*/
domain: string;
/**
* Hosted CDN for npm packages
* @default "https://fast.ssqt.io/npm"
*/
npmCdn: string;
/**
* Initialize a new {@link Widgets} instance.
*
* @param {ConfigOptions} config Config details
*
* @example <caption>Browser example</caption>
* var widgets = new squatch.Widgets({tenantAlias:'test_12b5bo1b25125'});
*
* @example <caption>Browserify/Webpack example</caption>
* var Widgets = require('@saasquatch/squatch-js').Widgets;
* var widgets = new Widgets({tenantAlias:'test_12b5bo1b25125'});
*
* @example <caption>Babel+Browserify/Webpack example</caption>
* import {Widgets} from '@saasquatch/squatch-js';
* let widgets = new Widgets({tenantAlias:'test_12b5bo1b25125'});
*/
constructor(configin: ConfigOptions) {
const config = validateConfig(configin);
this.tenantAlias = config.tenantAlias;
this.domain = config.domain;
this.npmCdn = config.npmCdn;
this.api = new WidgetApi(config);
}
/**
* This function calls the {@link WidgetApi.upsertUser} method, and it renders
* the widget if it is successful. Otherwise it shows the "error" widget.
*
* @param {Object} config Config details
* @param {Object} config.user The user details
* @param {string} config.user.id The user id
* @param {string} config.user.accountId The user account id
* @param {WidgetType} config.widgetType The content of the widget
* @param {EngagementMedium} config.engagementMedium How to display the widget
* @param {string} config.jwt the JSON Web Token (JWT) that is used to validate the data (can be disabled)
* @param {HTMLElement | string | undefined} config.container Element to load the widget into
* @param {string | undefined} config.trigger Trigger element for opening the popup widget
*
* @return {Promise<WidgetResult>} json object if true, with a Widget and user details
*/
async upsertUser(config: WithRequired<WidgetConfig, "user">) {
const raw = config as unknown;
const clean = validateWidgetConfig(raw) as WithRequired<
WidgetConfig,
"user"
>;
try {
const response = await this.api.upsertUser(clean);
return {
widget: this._renderWidget(response, clean, {
type: "upsert",
user: clean.user,
engagementMedium: config.engagementMedium,
container: config.container,
trigger: config.trigger,
}),
user: response.user,
};
} catch (err) {
_log(err);
if (err.apiErrorCode) {
this._renderErrorWidget(err, config.engagementMedium);
}
throw new Error(err);
}
}
/**
* This function calls the {@link WidgetApi.render} method, and it renders
* the widget if it is successful. Otherwise it shows the "error" widget.
*
* @param {Object} config Config details
* @param {Object} config.user The user details
* @param {string} config.user.id The user id
* @param {string} config.user.accountId The user account id
* @param {WidgetType} config.widgetType The content of the widget
* @param {EngagementMedium} config.engagementMedium How to display the widget
* @param {string} config.jwt the JSON Web Token (JWT) that is used
* to validate the data (can be disabled)
*
* @return {Promise<WidgetResult>} json object if true, with a Widget and user details
*/
async render(config: WidgetConfig): Promise<WidgetResult | undefined> {
const raw = config as unknown;
const clean = validatePasswordlessConfig(raw);
try {
const response = await this.api.render(clean);
return {
widget: this._renderWidget(response, clean, {
type: "passwordless",
engagementMedium: clean.engagementMedium,
container: clean.container,
trigger: clean.trigger,
}),
user: response.user,
};
} catch (err) {
if (err.apiErrorCode) {
this._renderErrorWidget(err, clean.engagementMedium);
}
throw new Error(err);
}
}
/**
* Autofills a referral code into an element when someone has been referred.
* Uses {@link WidgetApi.squatchReferralCookie} behind the scenes.
*
* @param selector Element class/id selector, or a callback function
* @returns
*/
async autofill(selector: string | Function): Promise<void> {
const input = selector as unknown;
if (typeof input === "function") {
try {
const response = await this.api.squatchReferralCookie();
input(response);
} catch (e) {
_log("Autofill error", e);
throw new Error(e);
}
return;
}
if (typeof input !== "string")
throw new Error("Autofill accepts a string or function");
let elems = document.querySelectorAll(input);
let elem;
if (elems.length > 0) {
// Only use the first element found
elem = elems[0];
} else {
_log("Element id/class or function missing");
throw new Error("Element id/class or function missing");
}
try {
const response = await this.api.squatchReferralCookie();
elem.value = response.codes[0];
} catch (e) {
throw new Error(e);
}
}
/**
* @hidden
* @param {Object} response The json object return from the WidgetApi
* @param {Object} config Config details
* @param {string} config.widgetType The widget type (REFERRER_WIDGET, CONVERSION_WIDGET)
* @param {string} config.engagementMedium (POPUP, EMBED)
* @returns {Widget} widget (PopupWidget or EmbedWidget)
*/
private _renderWidget(
response: any,
config: WidgetConfig,
context: WidgetContext
) {
_log("Rendering Widget...");
if (!response) throw new Error("Unable to get a response");
let widget;
let displayOnLoad = !!config.displayOnLoad;
const opts = response.jsOptions || {};
const params = {
content: response.template,
type: config.widgetType || opts.widget?.defaultWidgetType,
api: this.api,
domain: this.domain,
npmCdn: this.npmCdn,
context: context,
};
if (opts.widgetUrlMappings) {
opts.widgetUrlMappings.forEach((rule) => {
if (Widgets._matchesUrl(rule.url)) {
if (
rule.widgetType !== "CONVERSION_WIDGET" ||
response.user?.referredBy?.code
) {
displayOnLoad = rule.displayOnLoad;
_log(`Display ${rule.widgetType} on ${rule.url}`);
} else {
_log(
`Don't display ${rule.widgetType} when no referral on widget rule match ${rule.url}`
);
}
}
});
}
if (opts.fuelTankAutofillUrls) {
_log("We found a fuel tank autofill!");
opts.fuelTankAutofillUrls.forEach(({ url, formSelector }) => {
if (Widgets._matchesUrl(url)) {
_log("Fuel Tank URL matches");
if (response.user?.referredBy?.code) {
const formAutofill = document.querySelector(formSelector);
if (formAutofill) {
formAutofill.value =
response.user.referredBy.referredReward?.fuelTankCode || "";
} else {
_log(
new Error(
`Element with id/class ${formSelector} was not found.`
)
);
}
}
}
});
}
if (config.engagementMedium === "EMBED") {
widget = this._renderEmbedWidget(params);
} else {
widget = this._renderPopupWidget(params);
if (displayOnLoad) widget.open();
}
return widget;
}
private _renderPopupWidget(params: Params) {
const widget = new PopupWidget(params, params.context.trigger);
widget.load();
return widget;
}
private _renderEmbedWidget(params) {
const widget = new EmbedWidget(params, params.context.container);
widget.load();
return widget;
}
/**
* @hidden
* @param {Object} error The json object containing the error details
* @param {string} em The engagementMedium
* @returns {void}
*/
private _renderErrorWidget(
props: { apiErrorCode: string; rsCode: string; message: string },
em: EngagementMedium = "POPUP"
) {
const { apiErrorCode, rsCode, message } = props;
_log(new Error(`${apiErrorCode} (${rsCode}) ${message}`));
const params: Params = {
content: "error",
rsCode,
api: this.api,
domain: this.domain,
npmCdn: this.npmCdn,
type: "ERROR_WIDGET",
context: { type: "error" },
};
let widget: Widget;
if (em === "EMBED") {
widget = new EmbedWidget(params);
widget.load();
} else if (em === "POPUP") {
widget = new PopupWidget(params);
widget.load();
}
}
/**
* @hidden
* @param {string} rule A regular expression
* @returns {boolean} true if rule matches Url, false otherwise
*/
private static _matchesUrl(rule) {
// If there were no matches, null is returned.
return window.location.href.match(new RegExp(rule)) ? true : false;
}
}