Skip to content
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

v1.1: events cleanup #268

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 12 additions & 11 deletions packages/core/base/src/services/embed/crossmintIFrameService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
CheckoutEvents,
CrossmintCheckoutEventUnion,
CrossmintEmbeddedCheckoutProps,
InternalEvents,
InternalEventsUnion,
} from "../../types";
CrossmintEvent,
CrossmintEvents,
CrossmintInternalEvent,
CrossmintInternalEvents,
} from "@/types";

import { getEnvironmentBaseUrl, isFiatEmbeddedCheckoutProps } from "../../utils";

// TODO: Emit updatable parameters
Expand Down Expand Up @@ -44,10 +45,10 @@ function getUrl(props: CrossmintEmbeddedCheckoutProps) {
return `${baseUrl}${path}?${queryParams.toString()}`;
}

function _listenToEvents<EP = CrossmintCheckoutEventUnion | InternalEventsUnion>(
function _listenToEvents<EP = CrossmintEvent | CrossmintInternalEvent>(
callback: (event: MessageEvent<EP>) => void,
validEventTypes: {
[key: string]: CheckoutEvents | InternalEvents;
[key: string]: CrossmintEvents | CrossmintInternalEvents;
}
) {
function _onEvent(event: MessageEvent) {
Expand All @@ -65,7 +66,7 @@ function _listenToEvents<EP = CrossmintCheckoutEventUnion | InternalEventsUnion>
};
}

const listenToEvents = (callback: (event: MessageEvent<CrossmintCheckoutEventUnion>) => void) =>
_listenToEvents(callback, CheckoutEvents);
const listenToInternalEvents = (callback: (event: MessageEvent<InternalEventsUnion>) => void) =>
_listenToEvents(callback, InternalEvents);
const listenToEvents = (callback: (event: MessageEvent<CrossmintEvent>) => void) =>
_listenToEvents(callback, CrossmintEvents);
const listenToInternalEvents = (callback: (event: MessageEvent<CrossmintInternalEvent>) => void) =>
_listenToEvents(callback, CrossmintInternalEvents);
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { FiatEmbeddedCheckoutProps } from "../../types";
import { CrossmintCheckoutEventUnion, ParamsUpdatePayload, PaymentElementSDKEvents } from "../../types/events";
import { CheckoutEvents } from "../../types/events";
import {
CrossmintEvent,
CrossmintEvents,
CrossmintInternalEvents,
FiatEmbeddedCheckoutProps,
ParamsUpdatePayload,
} from "../../types";
import { getEnvironmentBaseUrl } from "../../utils";

// TODO: Accept both fiat and crypto
Expand Down Expand Up @@ -69,13 +73,13 @@ export function crossmintPaymentService_OLD(props: FiatEmbeddedCheckoutProps) {
return `${baseUrl}/sdk/paymentElement?${params.toString()}`;
}

function listenToEvents(cb: (event: MessageEvent<CrossmintCheckoutEventUnion>) => void): () => void {
function listenToEvents(cb: (event: MessageEvent<CrossmintEvent>) => void): () => void {
function _internalOnEvent(event: MessageEvent<any>) {
if (event.origin !== baseUrl) {
return;
}

if (Object.values(CheckoutEvents).includes(event.data.type)) {
if (Object.values(CrossmintEvents).includes(event.data.type)) {
cb(event);
}
}
Expand All @@ -93,7 +97,7 @@ export function crossmintPaymentService_OLD(props: FiatEmbeddedCheckoutProps) {
console.error("[Crossmint] Failed to find crossmint-embedded-checkout.iframe");
}
try {
iframe?.contentWindow?.postMessage({ type: PaymentElementSDKEvents.PARAMS_UPDATE, payload }, baseUrl);
iframe?.contentWindow?.postMessage({ type: CrossmintInternalEvents.PARAMS_UPDATE, payload }, baseUrl);
} catch (e) {
console.log("[Crossmint] Failed to emit query params", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InternalEvents } from "@/types/events";
import { CrossmintInternalEvents } from "@/types/events";
import { getEnvironmentBaseUrl } from "@/utils";

export function crossmintUiService_OLD({ environment }: { environment?: string } = {}) {
Expand All @@ -10,7 +10,7 @@ export function crossmintUiService_OLD({ environment }: { environment?: string }
return;
}

if (Object.values(InternalEvents).includes(event.data.type)) {
if (Object.values(CrossmintInternalEvents).includes(event.data.type)) {
cb(event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface CrossmintModalServiceParams {
failureCallbackURL?: string;
loginEmail?: string;
// TODO: Enable when events are ready in crossbit-main and docs are updated
// onEvent?: (event: CheckoutEvents, metadata?: any) => void;
// onEvent?: (event: CrossmintEvents, metadata?: any) => void;
}

export interface CrossmintModalServiceReturn {
Expand Down
29 changes: 16 additions & 13 deletions packages/core/base/src/services/useCrossmintEvents.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { backOff } from "exponential-backoff";

import { CheckoutEvents, CrossmintCheckoutEventUnion, ListenToMintingEventsProps, ListenerType } from "../types/events";
import { CrossmintEvent, CrossmintEvents } from "..";
import { getEnvironmentBaseUrl } from "../utils";

export function useCrossmintEvents({ environment }: { environment?: string } = {}) {
function listenToMintingEvents({ orderIdentifier }: ListenToMintingEventsProps, cb: ListenerType) {
const emittedEvents: CheckoutEvents[] = [];
function listenToMintingEvents(
{
orderIdentifier,
}: {
orderIdentifier: string;
},
cb: (event: CrossmintEvent) => any
) {
const emittedEvents: CrossmintEvents[] = [];
const succeededTransactionIdentifiers: string[] = [];
const failedTransactionIdentifiers: string[] = [];

function emitEvent(event: CrossmintCheckoutEventUnion) {
function emitEvent(event: CrossmintEvent) {
cb(event);
emittedEvents.push(event.type);
if (event.type === CheckoutEvents.ORDER_PROCESS_FINISHED) {
if (event.type === CrossmintEvents.ORDER_PROCESS_FINISHED) {
clearInterval(timer);
}
}

function handleDuplicateEvent(event: CrossmintCheckoutEventUnion) {
function handleDuplicateEvent(event: CrossmintEvent) {
if (!("transactionIdentifier" in event.payload)) {
return;
}

if (event.type === CheckoutEvents.TRANSACTION_FULFILLMENT_SUCCEEDED) {
if (event.type === CrossmintEvents.TRANSACTION_FULFILLMENT_SUCCEEDED) {
if (succeededTransactionIdentifiers.includes(event.payload.transactionIdentifier)) {
return;
}
succeededTransactionIdentifiers.push(event.payload.transactionIdentifier);
emitEvent(event);
} else if (event.type === CheckoutEvents.TRANSACTION_FULFILLMENT_FAILED) {
} else if (event.type === CrossmintEvents.TRANSACTION_FULFILLMENT_FAILED) {
if (failedTransactionIdentifiers.includes(event.payload.transactionIdentifier)) {
return;
}
Expand Down Expand Up @@ -59,11 +66,7 @@ export function useCrossmintEvents({ environment }: { environment?: string } = {
};
}

async function fetchOrderStatus({
orderIdentifier,
}: {
orderIdentifier: string;
}): Promise<CrossmintCheckoutEventUnion[]> {
async function fetchOrderStatus({ orderIdentifier }: { orderIdentifier: string }): Promise<CrossmintEvent[]> {
return await backOff(
async () => {
const res = await fetch(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/base/src/types/embed/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommonEmbeddedCheckoutProps } from ".";
import { CryptoPaymentMethod } from "..";

export type CryptoEmbeddedCheckoutProps<
PM extends keyof CryptoPaymentMethodSignerMap = keyof CryptoPaymentMethodSignerMap
PM extends keyof CryptoPaymentMethodSignerMap = keyof CryptoPaymentMethodSignerMap,
> = CommonEmbeddedCheckoutProps<PM> & {
signer?: CryptoPaymentMethodSignerMap[PM];
};
Expand Down
1 change: 0 additions & 1 deletion packages/core/base/src/types/embed/fiat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CommonEmbeddedCheckoutProps } from ".";
import { Recipient } from "..";
import { CrossmintCheckoutEventUnion } from "../events";

export type FiatEmbeddedCheckoutProps = CommonEmbeddedCheckoutProps<"fiat"> & {
// TODO: Audit old params
Expand Down
4 changes: 2 additions & 2 deletions packages/core/base/src/types/embed/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CaseInsensitive,
CollectionOrClientId,
CrossmintCheckoutEventUnion,
CrossmintEvent,
Currency,
Locale,
MintConfigs,
Expand All @@ -23,7 +23,7 @@ export type CommonEmbeddedCheckoutProps<PM extends PaymentMethod = PaymentMethod
uiConfig?: UIConfig;
whPassThroughArgs?: any;
projectId?: string;
onEvent?(event: CrossmintCheckoutEventUnion): any;
onEvent?(event: CrossmintEvent): any;
} & CollectionOrClientId;

export type CrossmintEmbeddedCheckoutProps = FiatEmbeddedCheckoutProps | CryptoEmbeddedCheckoutProps;
Loading