Skip to content

feat: message hover and click behavior and modal #2352

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

Merged
merged 17 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ui/buttons/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ export function normalizeButtonMessage(
}
}

if (offer) {
if (offer || typeof offer === "string") {
if (!Array.isArray(offer)) {
throw new TypeError(
`Expected message.offer to be an array of strings, got: ${String(
Expand All @@ -776,15 +776,18 @@ export function normalizeButtonMessage(
}
}

if (color && !values(MESSAGE_COLOR).includes(color)) {
if (color === "" || (color && !values(MESSAGE_COLOR).includes(color))) {
throw new Error(`Invalid color: ${color}`);
}

if (position && !values(MESSAGE_POSITION).includes(position)) {
if (
position === "" ||
(position && !values(MESSAGE_POSITION).includes(position))
) {
throw new Error(`Invalid position: ${position}`);
}

if (align && !values(MESSAGE_ALIGN).includes(align)) {
if (align === "" || (align && !values(MESSAGE_ALIGN).includes(align))) {
throw new Error(`Invalid align: ${align}`);
}

Expand Down
79 changes: 79 additions & 0 deletions src/zoid/buttons/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
getRenderedButtons,
getButtonSize,
getButtonExperiments,
getModal,
} from "./util";

export type ButtonsComponent = ZoidComponent<ButtonProps>;
Expand Down Expand Up @@ -681,6 +682,84 @@
},
},

onMessageHover: {
type: "function",
required: false,
value: ({ props, state }) => {
return async ({ offerType }) => {
if (!window.paypal.MessagesModal.show && !state.isModalFetching) {
state.isModalFetching = true;
await getModal();
state.isModalFetching = false;

Check failure on line 693 in src/zoid/buttons/component.jsx

View workflow job for this annotation

GitHub Actions / main

Possible race condition: `state.isModalFetching` might be assigned based on an outdated state of `state`

if (state.isAwaitingShow === true) {
const { message, clientID, merchantID } = props;
const amount = message?.amount;

window.paypal
.MessagesModal({
onReady: ({ show }) => {
show({
amount,
offer: offerType,
account: clientID,
merchantId: merchantID,
});
},
})
.render("body");

state.isAwaitingShow = false;
}
}
};
},
},

onMessageClick: {
type: "function",
required: false,
value: ({ props, state }) => {
return async ({ offerType, messageType }) => {
if (window.paypal.MessagesModal) {
const { message, clientID, merchantID, buttonSessionID } = props;
const amount = message?.amount || undefined;

window.paypal
.MessagesModal({
onReady: ({ show }) => {
show({
amount,
offer: offerType,
account: clientID,
merchantId: merchantID,
});
},
})
.render("body");

getLogger()
.info("button_message_clicked")
.track({
[FPTI_KEY.MESSAGE_TYPE]: messageType,
[FPTI_KEY.MESSAGE_STYLE_POSITION]: message.position,
[FPTI_KEY.MESSAGE_STYLE_TEXT_ALIGN]: message.align,
[FPTI_KEY.MESSAGE_STYLE_COLOR]: message.color,
[FPTI_KEY.MESSAGE_OFFER]: offerType,
[FPTI_KEY.AMOUNT]: amount,
[FPTI_KEY.BUTTON_MESSAGE_ID]: buttonSessionID,
});
} else if (!state.isModalFetching) {
state.isModalFetching = true;
await getModal();
state.isModalFetching = false;

Check failure on line 755 in src/zoid/buttons/component.jsx

View workflow job for this annotation

GitHub Actions / main

Possible race condition: `state.isModalFetching` might be assigned based on an outdated state of `state`
} else {
state.isAwaitingShow = true;
}
};
},
},

onShippingAddressChange: {
type: "function",
required: false,
Expand Down
43 changes: 43 additions & 0 deletions src/zoid/buttons/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
getFundingEligibility,
getPlatform,
getComponents,
getEnv,
} from "@paypal/sdk-client/src";
import { getRefinedFundingEligibility } from "@paypal/funding-components/src";

Expand Down Expand Up @@ -357,3 +358,45 @@
}
}
}

// eslint-disable-next-line promise/no-native, no-restricted-globals
export async function getModal(): string | Promise<Object> | void {
const modalBundleUrl = {
local:
"https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
test: "https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
stage:
"https://www.paypalobjects.com/upstream/bizcomponents/stage/modal.js",
sandbox:
"https://www.paypalobjects.com/upstream/bizcomponents/sandbox/modal.js",
production:
"https://www.paypalobjects.com/upstream/bizcomponents/js/modal.js",
};
const env = getEnv();

try {
return await new Promise((resolve) => {

Check failure on line 378 in src/zoid/buttons/util.js

View workflow job for this annotation

GitHub Actions / main

Promise is not supported in Safari 5, IE 9, Chrome 27

Check failure on line 378 in src/zoid/buttons/util.js

View workflow job for this annotation

GitHub Actions / main

Unexpected use of 'Promise'

Check failure on line 378 in src/zoid/buttons/util.js

View workflow job for this annotation

GitHub Actions / main

"Promise" is not defined
const script = document.createElement("script");
script.src = modalBundleUrl[env];
script.addEventListener("load", () => {
document.body.removeChild(script);

Check failure on line 382 in src/zoid/buttons/util.js

View workflow job for this annotation

GitHub Actions / main

document.body() is not supported in Firefox 30
resolve();
});
if (document.readyState === "loading") {
window.addEventListener("DOMContentLoaded", () => {
document.body.appendChild(script);
});
} else {
document.body.appendChild(script);
}
});
} catch (err) {
getLogger()
.info("button_message_modal_fetch_error")
.track({
err: err.message || "BUTTON_MESSAGE_MODAL_FETCH_ERROR",
details: err.details,
stack: JSON.stringify(err.stack || err),
});
}
}
Loading