Skip to content

Commit

Permalink
✨ Update vanilla example to use the modal step builder
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Oct 31, 2024
1 parent 0581ab8 commit 8fe1ebc
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-cars-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frak-labs/nexus-sdk": patch
---

Add a `modalBuilder` to ease modal creation
30 changes: 16 additions & 14 deletions example/vanilla-js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { setupFrakClient } from "./module/setupClient";
import { displayWalletStatus } from "./module/walletStatus";

// Export the setup function and config for use in other files
window.FrakSetup = { frakConfig, frakClient: null, modalShare };
window.FrakSetup = {
frakConfig,
frakClient: null,
modalShare,
modalBuilder: null,
};

document.addEventListener("DOMContentLoaded", () => {
console.log("NexusSDK", window.NexusSDK);
Expand All @@ -16,22 +21,19 @@ document.addEventListener("DOMContentLoaded", () => {
return;
}

const modalStepBuilder = window.NexusSDK.modalBuilder(frakClient, {
metadata: {
lang: "fr",
isDismissible: true,
},
login: loginModalStep,
});

window.FrakSetup.frakClient = frakClient;
window.FrakSetup.modalBuilder = modalStepBuilder;

window.NexusSDK.referralInteraction(frakClient, {
modalConfig: {
steps: {
login: loginModalStep,
openSession: {},
final: {
action: { key: "reward" },
},
},
metadata: {
lang: "fr",
isDismissible: true,
},
},
modalConfig: modalStepBuilder.reward().params,
options: {
alwaysAppendUrl: true,
},
Expand Down
15 changes: 2 additions & 13 deletions example/vanilla-js/src/module/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { loginModalStep } from "./config";

export function bindLoginButton() {
const loginButton = document.getElementById("login-button");
loginButton?.addEventListener("click", handleLogin);
Expand All @@ -19,20 +17,11 @@ async function handleLogin() {
loginButton.textContent = "Logging in...";

try {
if (!window.FrakSetup.frakClient) {
if (!window.FrakSetup.modalBuilder) {
console.error("Frak client not initialized");
return;
}
await window.NexusSDK.displayModal(window.FrakSetup.frakClient, {
metadata: {
lang: "fr",
isDismissible: true,
},
steps: {
login: loginModalStep,
openSession: {},
},
});
await window.FrakSetup.modalBuilder.display();
loginButton.textContent = "Logged In";
} catch (error) {
console.error("Login error:", error);
Expand Down
32 changes: 8 additions & 24 deletions example/vanilla-js/src/module/modalShare.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import { loginModalStep } from "./config";

export function modalShare() {
const finalAction = {
key: "sharing",
options: {
popupTitle: "Share this article with your friends",
text: "Discover this awesome article",
link: typeof window !== "undefined" ? window.location.href : "",
},
} as const;
if (!window.FrakSetup.frakClient) {
if (!window.FrakSetup.modalBuilder) {
console.error("Frak client not initialized");
return;
}
window.NexusSDK.displayModal(window.FrakSetup.frakClient, {
metadata: {
lang: "fr",
isDismissible: true,
},
steps: {
login: loginModalStep,
openSession: {},
final: {
action: finalAction,
},
},
});
window.FrakSetup.modalBuilder
.sharing({
popupTitle: "Share this article with your friends",
text: "Discover this awesome article",
link: typeof window !== "undefined" ? window.location.href : "",
})
.display();
}
4 changes: 4 additions & 0 deletions example/vanilla-js/src/types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {
ModalBuilder,
displayModal,
modalBuilder,
referralInteraction,
watchWalletStatus,
} from "@frak-labs/nexus-sdk/actions";
Expand All @@ -18,11 +20,13 @@ declare global {
displayModal: typeof displayModal;
referralInteraction: typeof referralInteraction;
watchWalletStatus: typeof watchWalletStatus;
modalBuilder: typeof modalBuilder;
};
FrakSetup: {
frakConfig: NexusWalletSdkConfig;
frakClient: NexusClient | null;
modalShare: () => void;
modalBuilder: ModalBuilder | null;
};
}
}
6 changes: 5 additions & 1 deletion packages/sdk/src/core/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ export {
sendTransaction,
type SendTransactionParams,
} from "./wrapper/sendTransaction";
export { modalBuilder } from "./wrapper/modalBuilder";
export {
modalBuilder,
type ModalStepBuilder,
type ModalBuilder,
} from "./wrapper/modalBuilder";
// Referral interaction
export { referralInteraction } from "./referral/referralInteraction";
export { processReferral } from "./referral/processReferral";
53 changes: 31 additions & 22 deletions packages/sdk/src/core/actions/wrapper/modalBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ import type {
} from "../../types";
import { displayModal } from "../displayModal";

/**
* Represent the type of the modal step builder
*/
export type ModalStepBuilder<
Steps extends ModalStepTypes[] = ModalStepTypes[],
> = {
params: DisplayModalParamsType<Steps>;
sendTx: (
options: SendTransactionModalStepType["params"]
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
reward: (
options?: Omit<FinalModalStepType["params"], "action">
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
sharing: (
sharingOptions?: Extract<
FinalActionType,
{ key: "sharing" }
>["options"],
options?: Omit<FinalModalStepType["params"], "action">
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
display: () => Promise<ModalRpcStepsResultType<Steps>>;
};

/**
* Represent the output type of the modal builder
*/
export type ModalBuilder = ModalStepBuilder<
[LoginModalStepType, OpenInteractionSessionModalStepType]
>;

/**
* Simple modal builder params builder
* @param client
Expand All @@ -30,7 +60,7 @@ export function modalBuilder(
login?: LoginModalStepType["params"];
openSession?: OpenInteractionSessionModalStepType["params"];
}
): ModalStepBuilder<[LoginModalStepType, OpenInteractionSessionModalStepType]> {
): ModalBuilder {
// Build the initial modal params
const baseParams: DisplayModalParamsType<
[LoginModalStepType, OpenInteractionSessionModalStepType]
Expand All @@ -46,27 +76,6 @@ export function modalBuilder(
return modalStepsBuilder(client, baseParams);
}

/**
* Represent the type of the modal step builder
*/
type ModalStepBuilder<Steps extends ModalStepTypes[]> = {
params: DisplayModalParamsType<Steps>;
sendTx: (
options: SendTransactionModalStepType["params"]
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
reward: (
options?: Omit<FinalModalStepType["params"], "action">
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
sharing: (
sharingOptions?: Extract<
FinalActionType,
{ key: "sharing" }
>["options"],
options?: Omit<FinalModalStepType["params"], "action">
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
display: () => Promise<ModalRpcStepsResultType<Steps>>;
};

/**
* Build builder helping to add steps to the modal
* @param client
Expand Down

0 comments on commit 8fe1ebc

Please sign in to comment.