Skip to content

Commit

Permalink
Update createModalElement func
Browse files Browse the repository at this point in the history
  • Loading branch information
mgavrila committed Dec 19, 2024
1 parent a5a44e1 commit 6601d3e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export class CrossWindowProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down Expand Up @@ -138,10 +138,10 @@ export class CrossWindowProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class ExtensionProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down Expand Up @@ -117,10 +117,10 @@ export class ExtensionProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export class IFrameProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down Expand Up @@ -129,10 +129,10 @@ export class IFrameProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,27 @@ export class LedgerProviderStrategy {
return;
}

const { eventBus: modalEventBus } =
await createModalElement<LedgerConnectModal>({
name: 'ledger-connect-modal',
withEventBus: true
});
const modalElement = await createModalElement<LedgerConnectModal>(
'ledger-connect-modal'
);
const eventBus = await modalElement.getEventBus();

if (!modalEventBus) {
throw new Error('Event bus not provided for Ledger provider');
if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
}

this.eventBus = modalEventBus;
return modalEventBus;
this.eventBus = eventBus;
return eventBus;
};

private signTransactions = async (transactions: Transaction[]) => {
const { modalElement: signModalElement, eventBus } =
await createModalElement<SignTransactionsModal>({
name: 'sign-transactions-modal',
withEventBus: true
});
const signModalElement = await createModalElement<SignTransactionsModal>(
'sign-transactions-modal'
);
const eventBus = await signModalElement.getEventBus();

if (!eventBus) {
throw new Error('Event bus not provided for Ledger provider');
throw new Error(ProviderErrorsEnum.eventBusError);
}

const manager = SignTransactionsStateManager.getInstance(eventBus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ export class WalletConnectProviderStrategy {
return;
}

const { eventBus } = await createModalElement<WalletConnectModal>({
name: 'wallet-connect-modal',
withEventBus: true
});
const modalElement = await createModalElement<WalletConnectModal>(
'wallet-connect-modal'
);

const eventBus = await modalElement.getEventBus();
return eventBus;
};

Expand Down Expand Up @@ -305,10 +305,10 @@ export class WalletConnectProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down Expand Up @@ -356,10 +356,10 @@ export class WalletConnectProviderStrategy {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const { eventBus } = await createModalElement<PendingTransactionsModal>({
name: 'pending-transactions-modal',
withEventBus: true
});
const modalElement = await createModalElement<PendingTransactionsModal>(
'pending-transactions-modal'
);
const eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(ProviderErrorsEnum.eventBusError);
Expand Down
28 changes: 11 additions & 17 deletions src/utils/createModalElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ import { IEventBus } from '@multiversx/sdk-dapp-core-ui/loader';
import { safeWindow } from 'constants/index';
import { defineCustomElements } from 'lib/sdkDappCoreUi';

type CreateModalElementType = {
name: string;
withEventBus?: boolean;
};

export const createModalElement = async <
T extends HTMLElement & { getEventBus: () => Promise<IEventBus | undefined> }
>({
name,
withEventBus = false
}: CreateModalElementType) => {
let eventBus: IEventBus | undefined;
>(
name: string
) => {
// let eventBus: IEventBus | undefined;

await defineCustomElements(safeWindow);

const modalElement = document.createElement(name) as T;
document.body.appendChild(modalElement);
await customElements.whenDefined(name);

if (withEventBus) {
eventBus = await modalElement.getEventBus();
// if (withEventBus) {
// eventBus = await modalElement.getEventBus();

if (!eventBus) {
throw new Error(`Event bus not provided for ${name}.`);
}
}
// if (!eventBus) {
// throw new Error(`Event bus not provided for ${name}.`);
// }
// }

return { modalElement, eventBus };
return modalElement;
};

0 comments on commit 6601d3e

Please sign in to comment.