fix invoicable modal close race condition

This commit is contained in:
keyan 2023-08-31 12:26:42 -05:00
parent b7d0a373f4
commit 29d17dce21

View File

@ -153,19 +153,18 @@ export const useInvoiceable = (onSubmit, options = defaultOptions) => {
(onClose, hmac) => { (onClose, hmac) => {
return async ({ id, satsReceived, expiresAt, hash }) => { return async ({ id, satsReceived, expiresAt, hash }) => {
await sleep(500) await sleep(500)
const repeat = () => const repeat = () => {
onClose()
// call onSubmit handler and pass invoice data // call onSubmit handler and pass invoice data
onSubmit({ satsReceived, hash, hmac, ...formValues }, ...submitArgs) onSubmit({ satsReceived, hash, hmac, ...formValues }, ...submitArgs)
.then(() => { .then(() => {
onClose()
options?.callback?.(formValues) options?.callback?.(formValues)
}) })
.catch((error) => { .catch((error) => {
// if error happened after payment, show repeat and cancel options // if error happened after payment, show repeat and cancel options
// by passing `errorCount` and `repeat` // by passing `errorCount` and `repeat`
console.error(error) console.error(error)
errorCount++ errorCount++
onClose()
showModal(onClose => ( showModal(onClose => (
<MutationInvoice <MutationInvoice
id={id} id={id}
@ -180,6 +179,7 @@ export const useInvoiceable = (onSubmit, options = defaultOptions) => {
/> />
), { keepOpen: true }) ), { keepOpen: true })
}) })
}
// prevents infinite loop of calling `onPayment` // prevents infinite loop of calling `onPayment`
if (errorCount === 0) await repeat() if (errorCount === 0) await repeat()
} }