Skip to content

Commit f0c9d89

Browse files
authored
refactor(widget): improve overall widget codebase, flow (#696)
1 parent a49d43b commit f0c9d89

3 files changed

Lines changed: 35 additions & 37 deletions

File tree

components/src/widget/views/confirmation/confirmation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { property, state } from 'lit/decorators.js'
33
import type { WalletAddressInfo } from 'publisher-tools-api'
44
import { CloseBtn } from '@c/shared/components/close-btn'
55
import { DotsLoader } from '@c/shared/components/dots-loader'
6-
import { getCurrencySymbol, getFormattedAmount } from '@c/utils'
6+
import { getFormattedAmount } from '@c/utils'
77
import {
88
NO_OP_CONTROLLER,
99
type Controller,
@@ -13,7 +13,7 @@ import { toAmount } from '@shared/utils'
1313
import confirmationCss from './confirmation.css?raw'
1414
import { type AmountChangeEventDetail, PaymentAmount } from '../amount/amount'
1515

16-
export class PaymentConfirmation extends LitElement {
16+
export class PaymentInitiate extends LitElement {
1717
@property({ type: Object }) configController!: WidgetController
1818
@property({ type: String }) note = ''
1919

@@ -183,13 +183,12 @@ export class PaymentConfirmation extends LitElement {
183183
walletAddress: sender,
184184
receiver,
185185
amount,
186-
note = '',
187186
} = this.configController.state
188187
const res = await this.#controller.initiatePayment({
189188
sender,
190189
receiver,
191190
amount,
192-
note,
191+
note: this.note,
193192
})
194193

195194
this.configController.updateState({

components/src/widget/views/interaction/interaction.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import { property, state } from 'lit/decorators.js'
33
import failedIcon from '@c/assets/interaction/authorization_failed.svg'
44
import loadingIcon from '@c/assets/interaction/authorization_loading.svg'
55
import successIcon from '@c/assets/interaction/authorization_success.svg'
6-
import {
7-
NO_OP_CONTROLLER,
8-
type Controller,
9-
type WidgetController,
10-
} from '@c/widget/controller'
11-
import interactionStyles from './interaction.css?raw'
6+
import { NO_OP_CONTROLLER, type Controller } from '@c/widget/controller'
7+
import styles from './interaction.css?raw'
128

139
type ButtonAction = {
1410
label: string
@@ -27,36 +23,38 @@ type InteractionView = {
2723
description?: string
2824
}
2925

30-
export class PaymentInteraction extends LitElement {
26+
export class PaymentWaiting extends LitElement {
3127
#pollingAbortController: AbortController | null = null
3228
#interactionCompleted = false
3329

34-
@property({ type: Object }) configController!: WidgetController
3530
@state() private currentView:
3631
| 'authorizing'
3732
| 'processing'
3833
| 'success'
3934
| 'failed' = 'authorizing'
4035
@state() private errorMessage = ''
4136

42-
static styles = unsafeCSS(interactionStyles)
37+
@property({ type: String, attribute: false }) private paymentId = ''
38+
@property({ type: String, attribute: false }) private grantRedirectUrl = ''
39+
40+
static styles = unsafeCSS(styles)
4341

4442
connectedCallback() {
4543
super.connectedCallback()
46-
const { grantRedirectUrl, paymentId } = this.configController.state
47-
if (!grantRedirectUrl) {
44+
45+
if (!this.grantRedirectUrl) {
4846
throw new Error('Grant redirect URL not found')
4947
}
5048
if (!this.#controller.isPreviewMode) {
51-
window.open(grantRedirectUrl, '_blank')
49+
window.open(this.grantRedirectUrl, '_blank')
5250
}
5351

5452
const POLLING_TIMEOUT = 25_000 * 5
5553
this.#pollingAbortController = new AbortController()
5654
AbortSignal.timeout(POLLING_TIMEOUT).addEventListener('abort', (ev) => {
5755
this.#pollingAbortController?.abort(ev)
5856
})
59-
void this.waitForCompletion(paymentId)
57+
void this.waitForCompletion(this.paymentId)
6058
}
6159

6260
#controller = NO_OP_CONTROLLER
@@ -75,7 +73,6 @@ export class PaymentInteraction extends LitElement {
7573
}
7674

7775
private async waitForCompletion(paymentId: string) {
78-
console.log(this.#controller?.getStatus)
7976
try {
8077
for await (const status of this.#controller.getStatus(
8178
paymentId,

components/src/widget/widget.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import {
99
WidgetController,
1010
} from './controller'
1111
import type { WidgetConfig } from './types'
12-
import { PaymentConfirmation } from './views/confirmation/confirmation'
12+
import { PaymentInitiate } from './views/confirmation/confirmation'
1313
import { HomeView, type SubmitEventDetail } from './views/home/home'
14-
import { PaymentInteraction } from './views/interaction/interaction'
14+
import { PaymentWaiting } from './views/interaction/interaction'
1515
import styles from './widget.css?raw'
1616

1717
const COMPONENTS = {
1818
'wm-payment-home': HomeView,
19-
'wm-payment-confirmation': PaymentConfirmation,
20-
'wm-payment-interaction': PaymentInteraction,
19+
'wm-payment-initiate': PaymentInitiate,
20+
'wm-payment-waiting': PaymentWaiting,
2121
}
2222

2323
export class PaymentWidget extends LitElement {
@@ -38,7 +38,7 @@ export class PaymentWidget extends LitElement {
3838

3939
@property({ type: Boolean }) isOpen = false
4040

41-
@state() private currentView: 'home' | 'confirmation' | 'interact' = 'home'
41+
@state() private currentView: 'home' | 'initiate' | 'waiting' = 'home'
4242

4343
static styles = unsafeCSS(styles)
4444

@@ -70,7 +70,7 @@ export class PaymentWidget extends LitElement {
7070
walletAddress: walletInfo,
7171
receiver: await this.#receiver,
7272
})
73-
this.currentView = 'confirmation'
73+
this.currentView = 'initiate'
7474
} catch (error) {
7575
return error instanceof Error
7676
? error.message
@@ -97,24 +97,24 @@ export class PaymentWidget extends LitElement {
9797
}
9898

9999
private handleInteractionCancelled() {
100-
this.currentView = 'confirmation'
100+
this.currentView = 'initiate'
101101
}
102102

103103
private renderCurrentView() {
104104
switch (this.currentView) {
105105
case 'home':
106106
return this.renderHomeView()
107-
case 'confirmation':
108-
return this.renderConfirmationView()
109-
case 'interact':
110-
return this.renderInteractionView()
107+
case 'initiate':
108+
return this.renderInitiateView()
109+
case 'waiting':
110+
return this.renderWaitingView()
111111
default:
112112
return this.renderHomeView()
113113
}
114114
}
115115

116116
private navigateToInteraction() {
117-
this.currentView = 'interact'
117+
this.currentView = 'waiting'
118118
}
119119

120120
private navigateToHome() {
@@ -136,27 +136,29 @@ export class PaymentWidget extends LitElement {
136136
`
137137
}
138138

139-
private renderConfirmationView() {
139+
private renderInitiateView() {
140140
return html`
141-
<wm-payment-confirmation
141+
<wm-payment-initiate
142142
.configController=${this.configController}
143143
.controller=${this.#controller}
144144
.note=${this.config.note || ''}
145145
@back=${this.navigateToHome}
146146
@close=${this.toggleWidget}
147147
@payment-confirmed=${this.navigateToInteraction}
148-
></wm-payment-confirmation>
148+
></wm-payment-initiate>
149149
`
150150
}
151151

152-
private renderInteractionView() {
152+
private renderWaitingView() {
153+
const { paymentId, grantRedirectUrl } = this.configController.state
153154
return html`
154-
<wm-payment-interaction
155-
.configController=${this.configController}
155+
<wm-payment-waiting
156+
.paymentId=${paymentId}
157+
.grantRedirectUrl=${grantRedirectUrl}
156158
.controller=${this.#controller}
157159
@interaction-cancelled=${this.handleInteractionCancelled}
158160
@back=${this.navigateToHome}
159-
></wm-payment-interaction>
161+
></wm-payment-waiting>
160162
`
161163
}
162164

0 commit comments

Comments
 (0)