import React, { Component, useRef, useState, version } from "react";
import { render } from "react-dom";
import squatch from "../dist/squatch";
import {
popup,
embed,
embedNew,
embedReferred,
popupNew,
popupReferred,
script,
toURL,
users,
href,
} from "./sandbox";
import { getVersions } from "./versions";
import { delay } from "./util";
import { widgets, worker } from "./generate";
import { rest } from "msw";
// 2. Define request handlers and response resolvers.
const modes = ["POPUP", "EMBED"];
const widgetTypes = [
"REFERRER_WIDGET",
"CONVERSION_WIDGET",
"p/tuesday-test/w/referrerWidget",
];
const staticVersions = ["HEAD", "latest", "alpha", "next", "local"];
/**
* Use the addUrlProps higher-order component to hook-in react-url-query.
*/
class App extends Component {
constructor(props) {
super(props);
worker.start({
findWorker: (scriptURL, _mockServiceWorkerUrl) =>
scriptURL.includes("mockServiceWorker"),
onUnhandledRequest(req) {
console.error(
"Found an unhandled %s request to %s",
req.method,
req.url.href
);
},
});
}
state = {
versions: staticVersions,
toolbarOpen: true,
};
async componentWillMount() {
const apiVersions = await getVersions();
const versions = [...staticVersions, ...apiVersions];
this.setState({ versions });
}
render() {
return (
Quick pick variables
Tenant / Program
);
}
}
function ParamArea() {
return (
Squatch.js Config
);
}
async function recordPurchase() {
//@ts-ignore
const { squatch, sandbox } = window;
const {
jwt,
user: { id, accountId },
} = sandbox.initObj;
const fields = {
// Optional
total: 10.0,
revenue: 10.0,
tax: 5.0,
currency: "USD",
};
await squatch.events().track(
{
userId: id,
accountId: accountId,
events: [
{
key: "purchase",
fields: fields, // Optional
// id: "kjv12kbwktb13t3", // Optional id
// dateTriggered: 1535136384753 // Optional date
},
],
},
{
jwt,
}
);
// TODO: Eventually we'd like an API like this:
// squatch.events().track("purchase", { ...fields });
}
async function runEventBomb() {
while (true) {
await recordPurchase();
await delay(100);
}
}
function WidgetType(props) {
return (
{widgetTypes.map((widgetType, i) => (
{widgetType}
))}
);
}
function ModeList(props) {
return (
Engagement Medium
{modes.map((mode, i) => (
{mode}
))}
);
}
function UserList(props) {
return (
User
{users.map((user, i) => (
{user["firstName"] || "Empty"}
))}
);
}
function VersionList(props) {
const { versions } = props;
return (
Version
{versions.map((v, i) => (
))}
);
}
async function getCustomWidget(engagementMedium) {
window["sandbox"].initObj = {
...window["sandbox"].initObj,
engagementMedium,
};
const value = document.getElementById("custom-widget")?.value;
worker.use(
rest.put(
"https://staging.referralsaasquatch.com/api/*",
(req, res, ctx) => {
return res(
ctx.delay(500),
ctx.status(202, "Mocked status"),
ctx.json({ jsOptions: {}, user: {}, template: value })
);
}
)
);
document.getElementById("squatchembed").innerHTML = "";
window["squatch"].widgets().upsertUser({
...window["sandbox"].initObj,
});
}
function MockedWidgets(props) {
const { versions } = props;
const [engagementMedium, setEngagementMedium] = useState("EMBED");
const [usePreload, setUsePreload] = useState(false);
const [showWidget, setShowWidget] = useState(false);
const [widget, setWidget] = useState(undefined);
const container = usePreload && document.getElementById("squatchembed");
const [popupTrigger, setPopupTrigger] = useState(".squatchpop");
async function getMockWidget(
widget,
containerOverride: string | undefined = undefined
) {
window["mockWidget"] = widget;
window["sandbox"].initObj = {
...window["sandbox"].initObj,
engagementMedium,
};
worker.use(
rest.put(
"https://staging.referralsaasquatch.com/api/*",
(req, res, ctx) => {
return res(
ctx.delay(500),
ctx.status(202, "Mocked status"),
ctx.json(widgets[window["mockWidget"]])
);
}
)
);
const defaultElement = document.getElementById(
"squatchembed"
) as HTMLElement;
defaultElement.innerHTML = "";
document.getElementById("test-selector").innerHTML = "";
if (!usePreload) defaultElement.setAttribute("style", "");
const { widget: embedWidget } = await window["squatch"]
.widgets()
.upsertUser({
...window["sandbox"].initObj,
container: (usePreload && containerOverride) || container,
trigger: popupTrigger,
});
if (showWidget) embedWidget.open();
setWidget(embedWidget);
}
return (
Mocked Widgets
Engagement Medium
{engagementMedium === "POPUP" ? (
) : (
""
)}
);
}
function CustomMockedWidget(props) {
const { versions } = props;
const [engagementMedium, setEngagementMedium] = useState("EMBED");
return (
Custom Mocked Widget
setEngagementMedium("EMBED")}
>
setEngagementMedium("POPUP")}
>
);
}
const root = document.getElementById("app");
render(, root);