Skip to content

Commit 7f95638

Browse files
adds onMessageHover and onMessageClick props and loads modal
1 parent 036c47c commit 7f95638

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Diff for: src/zoid/buttons/component.jsx

+79
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import {
9090
getRenderedButtons,
9191
getButtonSize,
9292
getButtonExperiments,
93+
getModal,
9394
} from "./util";
9495

9596
export type ButtonsComponent = ZoidComponent<ButtonProps>;
@@ -681,6 +682,84 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
681682
},
682683
},
683684

685+
onMessageHover: {
686+
type: "function",
687+
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+
}
715+
};
716+
},
717+
},
718+
719+
onMessageClick: {
720+
type: "function",
721+
required: false,
722+
value: ({ props, state }) => {
723+
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+
}
759+
};
760+
},
761+
},
762+
684763
onShippingAddressChange: {
685764
type: "function",
686765
required: false,

Diff for: src/zoid/buttons/util.js

+43
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getFundingEligibility,
2323
getPlatform,
2424
getComponents,
25+
getEnv,
2526
} from "@paypal/sdk-client/src";
2627
import { getRefinedFundingEligibility } from "@paypal/funding-components/src";
2728

@@ -357,3 +358,45 @@ export function getButtonSize(
357358
}
358359
}
359360
}
361+
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",
374+
};
375+
const env = getEnv();
376+
377+
try {
378+
return await new Promise((resolve) => {
379+
const script = document.createElement("script");
380+
script.src = modalBundleUrl[env];
381+
script.addEventListener("load", () => {
382+
document.body.removeChild(script);
383+
resolve();
384+
});
385+
if (document.readyState === "loading") {
386+
window.addEventListener("DOMContentLoaded", () => {
387+
document.body.appendChild(script);
388+
});
389+
} else {
390+
document.body.appendChild(script);
391+
}
392+
});
393+
} catch (err) {
394+
getLogger()
395+
.info("button_message_modal_fetch_error")
396+
.track({
397+
err: err.message || "BUTTON_MESSAGE_MODAL_FETCH_ERROR",
398+
details: err.details,
399+
stack: JSON.stringify(err.stack || err),
400+
});
401+
}
402+
}

0 commit comments

Comments
 (0)