Skip to content

Commit e68fd76

Browse files
committed
frontend: bitrefill move config request into its own function
1 parent e5d109f commit e68fd76

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

frontends/web/src/routes/market/bitrefill.tsx

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
9696
};
9797
}, [onResize]);
9898

99+
const handleConfiguration = useCallback(async (event: MessageEvent) => {
100+
if (
101+
!account
102+
|| !bitrefillInfo?.success
103+
) {
104+
return;
105+
}
106+
event.source?.postMessage({
107+
event: 'configuration',
108+
ref: bitrefillInfo.ref,
109+
utm_source: 'BITBOX',
110+
theme: isDarkMode ? 'dark' : 'light',
111+
hl: i18n.resolvedLanguage ? localeMapping[i18n.resolvedLanguage] : 'en',
112+
paymentMethods: account.coinCode ? coinMapping[account.coinCode] : 'bitcoin',
113+
refundAddress: bitrefillInfo.address,
114+
// Option to keep pending payment information longer in session, defaults to 'false'
115+
paymentPending: 'true',
116+
// Option to show payment information in the widget, defaults to 'true'
117+
showPaymentInfo: 'true'
118+
}, {
119+
targetOrigin: event.origin
120+
});
121+
}, [account, bitrefillInfo, isDarkMode]);
122+
99123
const handlePaymentRequest = useCallback(async (event: MessageEvent) => {
100124
if (!account || pendingPayment) {
101125
return;
@@ -164,8 +188,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
164188

165189
const handleMessage = useCallback(async (event: MessageEvent) => {
166190
if (
167-
!account
168-
|| !bitrefillInfo?.success
191+
!bitrefillInfo?.success
169192
|| ![getURLOrigin(bitrefillInfo.url), 'https://embed.bitrefill.com'].includes(event.origin)
170193
) {
171194
return;
@@ -175,21 +198,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
175198

176199
switch (data.event) {
177200
case 'request-configuration': {
178-
event.source?.postMessage({
179-
event: 'configuration',
180-
ref: bitrefillInfo.ref,
181-
utm_source: 'BITBOX',
182-
theme: isDarkMode ? 'dark' : 'light',
183-
hl: i18n.resolvedLanguage ? localeMapping[i18n.resolvedLanguage] : 'en',
184-
paymentMethods: account.coinCode ? coinMapping[account.coinCode] : 'bitcoin',
185-
refundAddress: bitrefillInfo.address,
186-
// Option to keep pending payment information longer in session, defaults to 'false'
187-
paymentPending: 'true',
188-
// Option to show payment information in the widget, defaults to 'true'
189-
showPaymentInfo: 'true'
190-
}, {
191-
targetOrigin: event.origin
192-
});
201+
handleConfiguration(event);
193202
break;
194203
}
195204
case 'payment_intent': {
@@ -200,7 +209,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
200209
break;
201210
}
202211
}
203-
}, [account, bitrefillInfo, handlePaymentRequest, isDarkMode]);
212+
}, [bitrefillInfo, handleConfiguration, handlePaymentRequest]);
204213

205214
useEffect(() => {
206215
window.addEventListener('message', handleMessage);

0 commit comments

Comments
 (0)