-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds prop update listening to modal browser zoid polyfill #1161
base: develop
Are you sure you want to change the base?
Changes from all commits
ecf1a92
71f28c8
701bdd8
a0c0b0b
3f5908f
7da92bb
8f938f5
172e52e
7cc1fdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ 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( | ||||||||||||||||||
({ | ||||||||||||||||||
|
@@ -112,3 +113,59 @@ export function formatDateByCountry(country) { | |||||||||||||||||
} | ||||||||||||||||||
return currentDate.toLocaleDateString('en-GB', options); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export function createUUID() { | ||||||||||||||||||
// crypto.randomUUID() is only available in HTTPS secure environments and modern browsers | ||||||||||||||||||
if (typeof crypto !== 'undefined' && crypto && crypto.randomUUID instanceof Function) { | ||||||||||||||||||
return crypto.randomUUID(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
const validChars = '0123456789abcdefghijklmnopqrstuvwxyz'; | ||||||||||||||||||
const stringLength = 32; | ||||||||||||||||||
let randomId = ''; | ||||||||||||||||||
for (let index = 0; index < stringLength; index++) { | ||||||||||||||||||
const randomIndex = Math.floor(Math.random() * validChars.length); | ||||||||||||||||||
randomId += validChars.charAt(randomIndex); | ||||||||||||||||||
} | ||||||||||||||||||
return randomId; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export function validateProps(updatedProps) { | ||||||||||||||||||
const validatedProps = {}; | ||||||||||||||||||
Object.entries(updatedProps).forEach(entry => { | ||||||||||||||||||
const [k, v] = entry; | ||||||||||||||||||
if (k === 'offerTypes') { | ||||||||||||||||||
validatedProps.offer = validate.offer({ props: { offer: v } }); | ||||||||||||||||||
} else { | ||||||||||||||||||
validatedProps[k] = validate[k]({ props: { [k]: v } }); | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+136
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we guaranteed to only pass in props that we have validation for? Should we account for the scenario where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. See #1170 for this fix |
||||||||||||||||||
}); | ||||||||||||||||||
return validatedProps; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export function sendEventAck(eventId, trustedOrigin) { | ||||||||||||||||||
// skip this step if running in test env because jest's target windows don't support postMessage | ||||||||||||||||||
if (process.env.NODE_ENV === 'test') { | ||||||||||||||||||
return; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// target window selection depends on if checkout window is in popup or modal iframe | ||||||||||||||||||
let targetWindow; | ||||||||||||||||||
const popupCheck = window.parent === window; | ||||||||||||||||||
if (popupCheck) { | ||||||||||||||||||
targetWindow = window.opener; | ||||||||||||||||||
} else { | ||||||||||||||||||
targetWindow = window.parent; | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+153
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed here in other PR |
||||||||||||||||||
|
||||||||||||||||||
targetWindow.postMessage( | ||||||||||||||||||
{ | ||||||||||||||||||
// PostMessenger stops reposting an event when it receives an eventName which matches the id in the message it sent and type 'ack' | ||||||||||||||||||
eventName: eventId, | ||||||||||||||||||
type: 'ack', | ||||||||||||||||||
eventPayload: { ok: true }, | ||||||||||||||||||
id: createUUID() | ||||||||||||||||||
}, | ||||||||||||||||||
|
||||||||||||||||||
trustedOrigin | ||||||||||||||||||
); | ||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a
belter
util we can leverage that meets our needs here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
belter has
uniqueID
that returnsuid_${randomID}_${timeID}
. Considerations are that the existing postMessenger message ids have a format that is different from that (32 alphanumeric vs the above format in hex); however, there is no validation for nor true consumer of the id we will generate here. Would you like me to use this belteruniqueID
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed this in the other (more recent) PR on this line since the file was moved out of utils