Skip to content

Commit f7d8c25

Browse files
modal renders
1 parent 7f95638 commit f7d8c25

File tree

3 files changed

+72
-92
lines changed

3 files changed

+72
-92
lines changed

src/ui/buttons/props.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ export function normalizeButtonMessage(
760760
}
761761
}
762762

763-
if (offer || typeof offer === "string") {
763+
if (typeof offer !== "undefined") {
764764
if (!Array.isArray(offer)) {
765765
throw new TypeError(
766766
`Expected message.offer to be an array of strings, got: ${String(
@@ -776,18 +776,18 @@ export function normalizeButtonMessage(
776776
}
777777
}
778778

779-
if (color === "" || (color && !values(MESSAGE_COLOR).includes(color))) {
779+
if (typeof color !== "undefined" && !values(MESSAGE_COLOR).includes(color)) {
780780
throw new Error(`Invalid color: ${color}`);
781781
}
782782

783783
if (
784-
position === "" ||
785-
(position && !values(MESSAGE_POSITION).includes(position))
784+
typeof position !== "undefined" &&
785+
!values(MESSAGE_POSITION).includes(position)
786786
) {
787787
throw new Error(`Invalid position: ${position}`);
788788
}
789789

790-
if (align === "" || (align && !values(MESSAGE_ALIGN).includes(align))) {
790+
if (typeof align !== "undefined" && !values(MESSAGE_ALIGN).includes(align)) {
791791
throw new Error(`Invalid align: ${align}`);
792792
}
793793

src/zoid/buttons/component.jsx

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -685,77 +685,55 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
685685
onMessageHover: {
686686
type: "function",
687687
required: false,
688-
value: ({ props, state }) => {
689-
return async ({ offerType }) => {
690-
if (!window.paypal.MessagesModal.show && !state.isModalFetching) {
691-
state.isModalFetching = true;
692-
await getModal();
693-
state.isModalFetching = false;
694-
695-
if (state.isAwaitingShow === true) {
696-
const { message, clientID, merchantID } = props;
697-
const amount = message?.amount;
698-
699-
window.paypal
700-
.MessagesModal({
701-
onReady: ({ show }) => {
702-
show({
703-
amount,
704-
offer: offerType,
705-
account: clientID,
706-
merchantId: merchantID,
707-
});
708-
},
709-
})
710-
.render("body");
711-
712-
state.isAwaitingShow = false;
713-
}
714-
}
688+
value: () => {
689+
return () => {
690+
// lazy loads the modal, to be memoized and executed onMessageClick
691+
getModal();
715692
};
716693
},
717694
},
718695

719696
onMessageClick: {
720697
type: "function",
721698
required: false,
722-
value: ({ props, state }) => {
699+
value: ({ props }) => {
723700
return async ({ offerType, messageType }) => {
724-
if (window.paypal.MessagesModal) {
725-
const { message, clientID, merchantID, buttonSessionID } = props;
726-
const amount = message?.amount || undefined;
727-
728-
window.paypal
729-
.MessagesModal({
730-
onReady: ({ show }) => {
731-
show({
732-
amount,
733-
offer: offerType,
734-
account: clientID,
735-
merchantId: merchantID,
736-
});
737-
},
738-
})
739-
.render("body");
740-
741-
getLogger()
742-
.info("button_message_clicked")
743-
.track({
744-
[FPTI_KEY.MESSAGE_TYPE]: messageType,
745-
[FPTI_KEY.MESSAGE_STYLE_POSITION]: message.position,
746-
[FPTI_KEY.MESSAGE_STYLE_TEXT_ALIGN]: message.align,
747-
[FPTI_KEY.MESSAGE_STYLE_COLOR]: message.color,
748-
[FPTI_KEY.MESSAGE_OFFER]: offerType,
749-
[FPTI_KEY.AMOUNT]: amount,
750-
[FPTI_KEY.BUTTON_MESSAGE_ID]: buttonSessionID,
751-
});
752-
} else if (!state.isModalFetching) {
753-
state.isModalFetching = true;
754-
await getModal();
755-
state.isModalFetching = false;
756-
} else {
757-
state.isAwaitingShow = true;
758-
}
701+
const {
702+
message,
703+
clientID,
704+
merchantID,
705+
currency,
706+
locale: { lang, country },
707+
buttonSessionID,
708+
} = props;
709+
const amount = message?.amount || undefined;
710+
711+
const modal = await getModal();
712+
const modalInstance = modal({
713+
account: `client-id:${clientID}`,
714+
});
715+
modalInstance.show({
716+
amount,
717+
offer: offerType?.join(",") || undefined,
718+
account: `client-id:${clientID}`,
719+
merchantId: merchantID?.join(",") || undefined,
720+
language: `${lang}-${country}`,
721+
buyerCountry: getBuyerCountry(),
722+
currency,
723+
});
724+
725+
getLogger()
726+
.info("button_message_clicked")
727+
.track({
728+
[FPTI_KEY.EVENT_NAME]: "message_click",
729+
// [FPTI_KEY.BUTTON_MESSAGE_OFFER_TYPE]: offerType,
730+
// [FPTI_KEY.BUTTON_MESSAGE_TYPE]: messageType,
731+
// [FPTI_KEY.BUTTON_MESSAGE_POSITION]: message.position,
732+
// [FPTI_KEY.BUTTON_MESSAGE_ALIGN]: message.align,
733+
// [FPTI_KEY.BUTTON_MESSAGE_COLOR]: message.color,
734+
// [FPTI_KEY.AMOUNT]: amount,
735+
[FPTI_KEY.BUTTON_SESSION_UID]: buttonSessionID,
736+
});
759737
};
760738
},
761739
},

src/zoid/buttons/util.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable compat/compat */
2+
/* eslint-disable promise/no-native */
13
/* @flow */
24
import {
35
supportsPopups as userAgentSupportsPopups,
@@ -13,6 +15,7 @@ import {
1315
getElement,
1416
isStandAlone,
1517
once,
18+
memoize,
1619
} from "@krakenjs/belter/src";
1720
import { FUNDING } from "@paypal/sdk-constants/src";
1821
import {
@@ -359,36 +362,35 @@ export function getButtonSize(
359362
}
360363
}
361364

362-
// eslint-disable-next-line promise/no-native, no-restricted-globals
363-
export async function getModal(): string | Promise<Object> | void {
364-
const modalBundleUrl = {
365-
local:
366-
"https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
367-
test: "https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
368-
stage:
369-
"https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
370-
sandbox:
371-
"https://www.paypalobjects.com/upstream/bizcomponents/sandbox/modal.js",
372-
production:
373-
"https://www.paypalobjects.com/upstream/bizcomponents/js/modal.js",
365+
export const getModal: () => Object = memoize(() => {
366+
const modalBundleUrl = () => {
367+
let envPiece;
368+
switch (getEnv()) {
369+
case "local":
370+
case "test":
371+
case "stage":
372+
envPiece = "stage";
373+
break;
374+
case "sandbox":
375+
envPiece = "sandbox";
376+
break;
377+
case "production":
378+
default:
379+
envPiece = "js";
380+
}
381+
return `https://www.paypalobjects.com/upstream/bizcomponents/${envPiece}/modal.js`;
374382
};
375-
const env = getEnv();
376383

377384
try {
378-
return await new Promise((resolve) => {
385+
// eslint-disable-next-line no-restricted-globals
386+
return new Promise((resolve) => {
379387
const script = document.createElement("script");
380-
script.src = modalBundleUrl[env];
388+
script.src = modalBundleUrl();
389+
document.body.appendChild(script);
381390
script.addEventListener("load", () => {
382391
document.body.removeChild(script);
383-
resolve();
392+
resolve(window.paypal.MessagesModal);
384393
});
385-
if (document.readyState === "loading") {
386-
window.addEventListener("DOMContentLoaded", () => {
387-
document.body.appendChild(script);
388-
});
389-
} else {
390-
document.body.appendChild(script);
391-
}
392394
});
393395
} catch (err) {
394396
getLogger()
@@ -399,4 +401,4 @@ export async function getModal(): string | Promise<Object> | void {
399401
stack: JSON.stringify(err.stack || err),
400402
});
401403
}
402-
}
404+
});

0 commit comments

Comments
 (0)