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

fix: button and modal listeners #212

Merged
merged 3 commits into from
Mar 7, 2024
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
33 changes: 14 additions & 19 deletions packages/dapp-kit-ui/src/components/buttons/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ let dappKitConfiguredListener: () => void;
export class Button extends LitElement {
constructor() {
super();

if (DAppKitUI.initialized) {
this.setAddressFromState();
this.configureButtonUI();
this.initAddressListener();
}
}

// this subscribeToCustomEvent need to be done if the DappKitUI button is reconfigured and so recreated after the initial configuration
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
} else {
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
connectedCallback(): void {
super.connectedCallback();
if (DAppKitUI.initialized) {
this.initAddressListener();
}
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
}

disconnectedCallback(): void {
Expand All @@ -49,6 +43,7 @@ export class Button extends LitElement {
this.mode = DAppKitUI.configuration?.themeMode ?? 'LIGHT';
this.i18n = DAppKitUI.configuration?.i18n ?? defaultI18n;
this.language = DAppKitUI.configuration?.language ?? 'en';
this.address = DAppKitUI.wallet.state.address ?? '';
this.requestUpdate();
}

Expand Down
47 changes: 28 additions & 19 deletions packages/dapp-kit-ui/src/components/modals/modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { html, LitElement, type TemplateResult, nothing } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { type WalletManager } from '@vechain/dapp-kit';
import { DAppKitUI } from '../../client';
import {
defaultI18n,
Expand All @@ -17,28 +16,42 @@ export class Modal extends LitElement {
constructor() {
super();
if (DAppKitUI.initialized) {
this.init();
} else {
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.init();
},
);
this.setAddressFromState();
}
}

connectedCallback(): void {
super.connectedCallback();
if (DAppKitUI.initialized) {
this.initAddressListener();
}
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
}

disconnectedCallback(): void {
super.disconnectedCallback();
dappKitConfiguredListener?.();
}

private init(): void {
private setAddressFromState(): void {
this.address = DAppKitUI.wallet.state.address ?? '';
this.wallet.subscribeToKey('address', (addr) => {
this.address = addr ?? '';
this.requestUpdate();
});
this.requestUpdate();
}

private initAddressListener(): void {
DAppKitUI.wallet.subscribeToKey(
'address',
(_address: string | null) => {
this.address = _address ?? '';
this.requestUpdate();
},
);
}

@property()
Expand All @@ -53,16 +66,12 @@ export class Modal extends LitElement {
@property()
language = 'en';

private get wallet(): WalletManager {
return DAppKitUI.wallet;
}

@property({ type: Function })
onSourceClick?: (source?: SourceInfo) => void;

@property({ type: Function })
onDisconnectClick = (): void => {
this.wallet.disconnect();
DAppKitUI.wallet.disconnect();
};

override render(): TemplateResult {
Expand Down
Loading