Skip to content

Commit

Permalink
✨ Include the purchase status tracker as helper in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Oct 31, 2024
1 parent 516bbff commit cfc6d8c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
3 changes: 2 additions & 1 deletion packages/sdk/src/core/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export { watchWalletStatus } from "./watchWalletStatus";
export { sendInteraction } from "./sendInteraction";
export { displayModal } from "./displayModal";
export { openSso } from "./openSso";
// Helper to track the purchase status
export { trackPurchaseStatus } from "./trackPurchaseStatus";
// Modal wrappers
export {
siweAuthenticate,
Expand All @@ -11,7 +13,6 @@ export {
sendTransaction,
type SendTransactionParams,
} from "./wrapper/sendTransaction";
export { walletStatus } from "./wrapper/walletStatus";
// Referral interaction
export { referralInteraction } from "./referral/referralInteraction";
export { processReferral } from "./referral/processReferral";
4 changes: 2 additions & 2 deletions packages/sdk/src/core/actions/referral/referralInteraction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hex } from "viem";
import { walletStatus } from "../";
import { watchWalletStatus } from "../";
import type {
DisplayModalParamsType,
ModalStepTypes,
Expand Down Expand Up @@ -29,7 +29,7 @@ export async function referralInteraction(
});

// Get the current wallet status
const currentWalletStatus = await walletStatus(client);
const currentWalletStatus = await watchWalletStatus(client);

try {
return await processReferral(client, {
Expand Down
31 changes: 31 additions & 0 deletions packages/sdk/src/core/actions/trackPurchaseStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Function used to track the status of a purchase
*/
export async function trackPurchaseStatus(args: {
customerId: string | number;
orderId: string | number;
token: string;
}) {
if (typeof window === "undefined") {
console.warn("[Frak] No window found, can't track purchase");
return;
}
const interactionToken = window.sessionStorage.getItem(
"frak-wallet-interaction-token"
);
if (!interactionToken) {
console.warn("[Frak] No frak session found, skipping purchase check");
return;
}

// Submit the listening request
await fetch("https://backend.frak.id/interactions/listenForPurchase", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"x-wallet-sdk-auth": interactionToken,
},
body: JSON.stringify(args),
});
}
38 changes: 30 additions & 8 deletions packages/sdk/src/core/actions/watchWalletStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NexusClient } from "../types/client";
import type { WalletStatusReturnType } from "../types/rpc/walletStatus";
import { Deferred } from "../utils";

/**
* Function used to watch the current nexus wallet status
Expand All @@ -8,12 +9,33 @@ import type { WalletStatusReturnType } from "../types/rpc/walletStatus";
*/
export function watchWalletStatus(
client: NexusClient,
callback: (status: WalletStatusReturnType) => void
) {
return client.listenerRequest(
{
method: "frak_listenToWalletStatus",
},
callback
);
callback?: (status: WalletStatusReturnType) => void
): Promise<WalletStatusReturnType> {
// If no callback is provided, just do a request with deferred result
if (!callback) {
return client.request({ method: "frak_listenToWalletStatus" });
}

// Otherwise, listen to the wallet status and return the first one received
const firstResult = new Deferred<WalletStatusReturnType>();
let hasResolved = false;

// Start the listening request, and return the first result
return client
.listenerRequest(
{
method: "frak_listenToWalletStatus",
},
(status) => {
// Transmit the status to the callback
callback(status);

// If the promise hasn't resolved yet, resolve it
if (!hasResolved) {
firstResult.resolve(status);
hasResolved = true;
}
}
)
.then(() => firstResult.promise);
}
26 changes: 0 additions & 26 deletions packages/sdk/src/core/actions/wrapper/walletStatus.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/sdk/src/react/hook/useWalletStatus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback } from "react";
import type { WalletStatusReturnType } from "../../core";
import { walletStatus } from "../../core/actions";
import { watchWalletStatus } from "../../core/actions";
import { ClientNotFound } from "../../core/types/rpc/error";
import { useNexusClient } from "./useNexusClient";

Expand Down Expand Up @@ -37,7 +37,7 @@ export function useWalletStatus() {
throw new ClientNotFound();
}

return walletStatus(client, newStatusUpdated);
return watchWalletStatus(client, newStatusUpdated);
},
enabled: !!client,
});
Expand Down

0 comments on commit cfc6d8c

Please sign in to comment.