Fix onSuccess called twice

For some reason, when calling `showModal`, `useMemo` in modal.js and the code for the modal component (here: <Invoice>) is called twice.

This leads to the `onSuccess` callback being called twice and one failing since the first one deletes the invoice.
This commit is contained in:
ekzyis 2023-07-22 12:49:01 +02:00
parent 853a389b65
commit 7dda8a1e01
1 changed files with 4 additions and 0 deletions

View File

@ -43,12 +43,16 @@ export const useAnonymous = (fn, options = defaultOptions) => {
const invoice = data?.createInvoice
useEffect(() => {
if (invoice) {
// fix for bug where `showModal` runs the code for two modals and thus executes `onSuccess` twice
let called = false
showModal(onClose =>
<Invoice
id={invoice.id}
onConfirmation={
async ({ satsReceived }) => {
setTimeout(async () => {
if (called) return
called = true
await fn(satsReceived, ...fnArgs, invoice.hash)
onClose()
}, 2000)