Skip to content

Commit a0ada03

Browse files
committed
fix: double request & fetching on open
1 parent ae5b53d commit a0ada03

File tree

2 files changed

+91
-77
lines changed

2 files changed

+91
-77
lines changed

react/lib/components/PayButton/PayButton.tsx

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
113113
} = Object.assign({}, PayButton.defaultProps, props);
114114

115115
const [paymentId, setPaymentId] = useState<string | undefined>(undefined);
116-
const [fetchingPaymentId, setFetchingPaymentId] = useState<boolean | undefined>();
117116
const [addressType, setAddressType] = useState<CryptoCurrency>(
118117
getCurrencyTypeFromAddress(to),
119118
);
@@ -136,16 +135,58 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
136135
}
137136
}, 300);
138137
};
138+
139+
const getPaymentId = useCallback(async (
140+
currency: Currency,
141+
amount: number,
142+
convertedAmount: number | undefined,
143+
to: string | undefined,
144+
): Promise<string | undefined> => {
145+
if (disablePaymentId || !to) return paymentId
146+
try {
147+
const amountToUse =
148+
(isFiat(currency) || randomSatoshis) && convertedAmount
149+
? convertedAmount
150+
: amount
151+
152+
console.log('Creating payment ID with amount:', amountToUse)
153+
const responsePaymentId = await createPayment(amountToUse, to, apiBaseUrl)
154+
const intervalId = setInterval(() => { }, 1200);
155+
156+
setPaymentId(responsePaymentId)
157+
return responsePaymentId
158+
} catch (err) {
159+
console.error('Error creating payment ID:', err)
160+
return undefined
161+
}
162+
}, [disablePaymentId, apiBaseUrl, isFiat, randomSatoshis])
163+
139164
const handleButtonClick = useCallback(async (): Promise<void> => {
140-
if (onOpen !== undefined) {
165+
166+
if (onOpen) {
141167
if (isFiat(currency)) {
142-
void waitPrice(() => { onOpen(cryptoAmountRef.current, to, paymentId) })
168+
void waitPrice(() => onOpen(cryptoAmountRef.current, to, paymentId))
143169
} else {
144170
onOpen(amount, to, paymentId)
145171
}
146172
}
147-
setDialogOpen(true);
148-
}, [cryptoAmount, to, paymentId, price])
173+
174+
if (!disablePaymentId && !paymentId) {
175+
await getPaymentId(currency, Number(amount), convertedAmount, to)
176+
}
177+
178+
setDialogOpen(true)
179+
}, [
180+
onOpen,
181+
isFiat,
182+
currency,
183+
amount,
184+
to,
185+
paymentId,
186+
disablePaymentId,
187+
getPaymentId,
188+
convertedAmount,
189+
])
149190

150191
const handleCloseDialog = (success?: boolean, paymentId?: string): void => {
151192
if (onClose !== undefined) onClose(success, paymentId);
@@ -188,42 +229,42 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
188229
return
189230
}
190231
(async () => {
191-
if (txsSocket === undefined) {
192-
const expectedAmount = currencyObj ? currencyObj?.float : undefined
193-
await setupChronikWebSocket({
194-
address: to,
195-
txsSocket,
196-
apiBaseUrl,
197-
wsBaseUrl,
198-
setTxsSocket,
199-
setNewTxs,
200-
setDialogOpen,
201-
checkSuccessInfo: {
202-
currency,
203-
price,
204-
randomSatoshis: randomSatoshis ?? false,
205-
disablePaymentId,
206-
expectedAmount,
207-
expectedOpReturn: opReturn,
208-
expectedPaymentId: paymentId,
209-
currencyObj,
210-
}
211-
})
212-
}
213-
if (altpaymentSocket === undefined && useAltpayment) {
214-
await setupAltpaymentSocket({
215-
addressType,
216-
altpaymentSocket,
217-
wsBaseUrl,
218-
setAltpaymentSocket,
219-
setCoins,
220-
setCoinPair,
221-
setLoadingPair,
222-
setAltpaymentShift,
223-
setLoadingShift,
224-
setAltpaymentError,
225-
})
226-
}
232+
if (txsSocket === undefined) {
233+
const expectedAmount = currencyObj ? currencyObj?.float : undefined
234+
await setupChronikWebSocket({
235+
address: to,
236+
txsSocket,
237+
apiBaseUrl,
238+
wsBaseUrl,
239+
setTxsSocket,
240+
setNewTxs,
241+
setDialogOpen,
242+
checkSuccessInfo: {
243+
currency,
244+
price,
245+
randomSatoshis: randomSatoshis ?? false,
246+
disablePaymentId,
247+
expectedAmount,
248+
expectedOpReturn: opReturn,
249+
expectedPaymentId: paymentId,
250+
currencyObj,
251+
}
252+
})
253+
}
254+
if (altpaymentSocket === undefined && useAltpayment) {
255+
await setupAltpaymentSocket({
256+
addressType,
257+
altpaymentSocket,
258+
wsBaseUrl,
259+
setAltpaymentSocket,
260+
setCoins,
261+
setCoinPair,
262+
setLoadingPair,
263+
setAltpaymentShift,
264+
setLoadingShift,
265+
setAltpaymentError,
266+
})
267+
}
227268
})()
228269

229270
return () => {
@@ -258,9 +299,9 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
258299

259300
useEffect(() => {
260301
(async () => {
261-
if (isFiat(currency) && price === 0) {
262-
await getPrice();
263-
}
302+
if (isFiat(currency) && price === 0) {
303+
await getPrice();
304+
}
264305
})()
265306
}, [currency, getPrice, to, price]);
266307

@@ -282,44 +323,14 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
282323
amount as number,
283324
addressType,
284325
randomSatoshis,
285-
);
286-
setCryptoAmount(convertedObj.string);
326+
); setCryptoAmount(convertedObj.string);
287327
setConvertedAmount(convertedObj.float);
288328
setConvertedCurrencyObj(convertedObj);
289329
} else if (!isFiat(currency) && !randomSatoshis) {
290330
setCryptoAmount(amount?.toString());
291331
}
292332
}, [price, currencyObj, amount, currency, randomSatoshis, to]);
293333

294-
useEffect(() => {
295-
if (fetchingPaymentId === true) {
296-
return;
297-
}
298-
if ((isFiat(currency) && !convertedAmount) || (randomSatoshis && !convertedAmount)) {
299-
return
300-
}
301-
302-
setFetchingPaymentId(true);
303-
const initializePaymentId = async () => {
304-
if (!disablePaymentId && to) {
305-
try {
306-
const amountToUse = (isFiat(currency) || randomSatoshis) ? convertedAmount : amount;
307-
console.log('Creating payment ID with amount:', amountToUse, isFiat(currency));
308-
const responsePaymentId = await createPayment(amountToUse, to, apiBaseUrl);
309-
setPaymentId(responsePaymentId);
310-
setFetchingPaymentId(false);
311-
} catch (error) {
312-
console.error('Error creating payment ID:', error);
313-
setFetchingPaymentId(false);
314-
}
315-
} else {
316-
setFetchingPaymentId(false);
317-
}
318-
};
319-
320-
initializePaymentId();
321-
}, [disablePaymentId, amount, convertedAmount, to, apiBaseUrl]);
322-
323334
const theme = useTheme(props.theme, isValidXecAddress(to ?? ''));
324335

325336
const ButtonComponent: React.FC<ButtonProps> = (

react/lib/components/Widget/WidgetContainer.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
144144
const [thisPrice, setThisPrice] = useState(0);
145145
const [usdPrice, setUsdPrice] = useState(0);
146146
useEffect(() => {
147-
if (fetchingPaymentId !== undefined) {
147+
if (
148+
fetchingPaymentId !== undefined ||
149+
thisPaymentId !== undefined
150+
) {
148151
return
149152
}
150153
setFetchingPaymentId(true)
151154
const initializePaymentId = async () => {
152-
if ((paymentId === undefined || paymentId === '') && !disablePaymentId) {
155+
if (paymentId === undefined && !disablePaymentId) {
153156
if (to) {
154157
try {
155158
const responsePaymentId = await createPayment(amount, to, apiBaseUrl);

0 commit comments

Comments
 (0)