Skip to content

Commit

Permalink
feat: allow to bypass redemption steps for Redeem or Cancel + fix iss…
Browse files Browse the repository at this point in the history
…ues in js script
  • Loading branch information
levalleux-ludo committed Aug 14, 2023
1 parent f64d397 commit 22e9197
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@bosonprotocol/react-kit": "^0.18.0",
"@bosonprotocol/react-kit": "^0.19.0-alpha.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
70 changes: 44 additions & 26 deletions public/scripts/boson-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ const constants = {
showRedeemId: "boson-redeem",
showFinanceId: "boson-finance",
exchangeIdTag: "data-exchange-id",
bypassModeTag: "data-bypass-mode",
sellerIdTag: "data-seller-id",
hideModalId: "boson-hide-modal",
hideModalMessage: "boson-close-iframe",
financeUrl: (widgetsHost, sellerId) =>
`${widgetsHost}/#/finance?sellerId=${sellerId}`,
redeemUrl: (widgetsHost, exchangeId) =>
`${widgetsHost}/#/redeem?exchangeId=${exchangeId}`
redeemUrl: (widgetsHost, exchangeId, bypassMode) =>
`${widgetsHost}/#/redeem?exchangeId=${exchangeId || ""}&bypassMode=${
bypassMode || ""
}`
};
var scripts = document.getElementsByTagName("script");
var widgetsHost = null;
if (scripts) {
for (var i = 0; i < scripts.length; i++) {
if (
scripts[i].attributes["src"]?.value &&
scripts[i].attributes["src"].value.endsWith(`/${constants.scriptName}`)
) {
widgetsHost = scripts[i].attributes["src"].value.replace(
Expand All @@ -31,6 +35,9 @@ if (scripts) {
break;
}
}
if (!widgetsHost) {
console.error("Unable to find widgetsHost from script tags");
}
} else {
console.error("Unable to find <scripts> tag");
}
Expand Down Expand Up @@ -66,32 +73,43 @@ const hideIFrame = () => {
el.remove();
}
};
const showFinanceId = document.getElementById(constants.showFinanceId);
if (showFinanceId) {
showFinanceId.onclick = function () {
showLoading(constants.loadingDurationMSec);
hideIFrame();
var sellerId = showFinanceId.attributes[constants.sellerIdTag]?.value;
createIFrame(constants.financeUrl(widgetsHost, sellerId));
};
}
const showRedeemId = document.getElementById(constants.showRedeemId);
if (showRedeemId) {
showRedeemId.onclick = function () {
showLoading(constants.loadingDurationMSec);
hideIFrame();
var exchangeId = showRedeemId.attributes[constants.exchangeIdTag]?.value;
createIFrame(constants.redeemUrl(widgetsHost, exchangeId));
};
}
const hideModalId = document.getElementById(constants.hideModalId);
if (hideModalId) {
hideModalId.onclick = function () {
hideIFrame();
};
}
window.addEventListener("message", (event) => {
if (event.data === constants.hideModalMessage) {
hideIFrame();
}
});
function bosonWidgetReload() {
const showFinanceId = document.getElementById(constants.showFinanceId);
if (showFinanceId) {
showFinanceId.onclick = function () {
showLoading(constants.loadingDurationMSec);
hideIFrame();
var sellerId = showFinanceId.attributes[constants.sellerIdTag]?.value;
createIFrame(constants.financeUrl(widgetsHost, sellerId));
};
}
const showRedeemEls = document.querySelectorAll(
`[id^="${constants.showRedeemId}"]`
);
for (let i = 0; i < showRedeemEls.length; i++) {
const showRedeemId = showRedeemEls[i];
console.log(
"Boson Widget - add onClick handle on element",
showRedeemId.id
);
showRedeemId.onclick = function () {
showLoading(constants.loadingDurationMSec);
hideIFrame();
var exchangeId = showRedeemId.attributes[constants.exchangeIdTag]?.value;
var bypassMode = showRedeemId.attributes[constants.bypassModeTag]?.value;
createIFrame(constants.redeemUrl(widgetsHost, exchangeId, bypassMode));
};
}
const hideModalId = document.getElementById(constants.hideModalId);
if (hideModalId) {
hideModalId.onclick = function () {
hideIFrame();
};
}
}
bosonWidgetReload();
25 changes: 24 additions & 1 deletion src/components/widgets/redeem/Redeem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RedemptionWidget } from "@bosonprotocol/react-kit";
import {
RedemptionBypassMode,
RedemptionWidget
} from "@bosonprotocol/react-kit";
import { useSearchParams } from "react-router-dom";

import { CONFIG } from "../../../config";
Expand All @@ -7,6 +10,9 @@ export const redeemPath = "/redeem";
export function Redeem() {
const [searchParams] = useSearchParams();
const exchangeId = searchParams.get("exchangeId") || undefined;
const bypassMode: RedemptionBypassMode = checkBypassMode(
searchParams.get("bypassMode") || undefined
);

return (
<RedemptionWidget
Expand Down Expand Up @@ -45,6 +51,23 @@ export function Redeem() {
}
}}
modalMargin="2%"
bypassMode={bypassMode}
></RedemptionWidget>
);
}

function checkBypassMode(
bypassModeStr: string | undefined
): RedemptionBypassMode {
switch (bypassModeStr) {
case RedemptionBypassMode.REDEEM: {
return RedemptionBypassMode.REDEEM;
}
case RedemptionBypassMode.CANCEL: {
return RedemptionBypassMode.CANCEL;
}
default: {
return RedemptionBypassMode.NORMAL;
}
}
}

0 comments on commit 22e9197

Please sign in to comment.