-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathutils.js
130 lines (121 loc) · 4.16 KB
/
utils.js
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
import objectEntries from 'core-js-pure/stable/object/entries';
import arrayFrom from 'core-js-pure/stable/array/from';
import { isIosWebview, isAndroidWebview } from '@krakenjs/belter/src';
import { request, memoize, ppDebug } from '../../../../utils';
import validate from '../../../../library/zoid/message/validation';
export const getContent = memoize(
({
currency,
amount,
payerId,
clientId,
merchantId,
customerId,
buyerCountry,
language,
ignoreCache,
deviceID,
version,
env,
stageTag,
integrationType,
channel,
contextualComponents,
devTouchpoint,
disableSetCookie,
features,
buttonSessionId
}) => {
const query = objectEntries({
currency,
amount,
payer_id: payerId,
client_id: clientId,
merchant_id: merchantId,
customer_id: customerId,
buyer_country: buyerCountry,
language,
ignore_cache: ignoreCache,
deviceID,
version,
env,
stageTag,
integrationType,
channel,
contextual_components: contextualComponents,
devTouchpoint,
disableSetCookie,
features,
buttonSessionId
})
.filter(([, val]) => Boolean(val))
.reduce(
(acc, [key, val]) =>
`${acc}&${key}=${encodeURIComponent(typeof val === 'object' ? JSON.stringify(val) : val)}`,
''
)
.slice(1);
ppDebug('Updating modal with new props...', { inZoid: true });
return request('GET', `${window.location.origin}/credit-presentment/modalContent?${query}`).then(
({ data }) => data
);
}
);
/**
* Checks if target is lander. If true, lander-specific styles will be used.
* @returns boolean
*/
export const isLander = __MESSAGES__.__TARGET__ === 'LANDER';
const { userAgent } = window.navigator;
export const isIframe = window.top !== window || isIosWebview(userAgent) || isAndroidWebview(userAgent);
export function setupTabTrap() {
// Disable tab trap functionality for modal lander
if (isLander) {
return;
}
const focusableElementsString =
"a[href], button, input, textarea, select, details, [tabindex]:not([tabindex='-1'])";
function trapTabKey(e) {
// Check for TAB key press
if (e.keyCode === 9 && !document.querySelector('.modal-closed')) {
const tabArray = arrayFrom(document.querySelectorAll(focusableElementsString)).filter(
node => window.getComputedStyle(node).visibility === 'visible'
);
// SHIFT + TAB
if (e.shiftKey && document.activeElement === tabArray[0]) {
e.preventDefault();
tabArray[tabArray.length - 1].focus();
} else if (document.activeElement === tabArray[tabArray.length - 1]) {
e.preventDefault();
tabArray[0].focus();
}
// auto scroll into view for focused element
setTimeout(() => {
document.activeElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 0);
}
}
window.addEventListener('keydown', trapTabKey);
}
export function formatDateByCountry(country) {
const currentDate = new Date();
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
if (country === 'US') {
return currentDate.toLocaleDateString('en-US', options);
}
return currentDate.toLocaleDateString('en-GB', options);
}
export function validateProps(updatedProps) {
const validatedProps = {};
Object.entries(updatedProps).forEach(entry => {
const [k, v] = entry;
if (k === 'offerType') {
validatedProps.offer = validate.offer({ props: { offer: v } });
} else if (!Object.keys(validate).includes(k)) {
validatedProps[k] = v;
} else {
validatedProps[k] = validate[k]({ props: { [k]: v } });
}
});
return validatedProps;
}