From 85162b6d33e2c37c0cbd8850e05797486c0ca317 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 22 Jul 2023 21:58:04 +0200 Subject: [PATCH] Add error handling --- lib/anonymous.js | 123 +++++++++++++++++++++++++++++++++++++++++------ lib/time.js | 2 + 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/lib/anonymous.js b/lib/anonymous.js index 559854a1..06807c1d 100644 --- a/lib/anonymous.js +++ b/lib/anonymous.js @@ -9,8 +9,54 @@ import { useMe } from '../components/me' import { msatsToSats } from './format' import FundError from '../components/fund-error' import { INVOICE } from '../fragments/wallet' +import InvoiceStatus from '../components/invoice-status' +import { sleep } from './time' +import { Button } from 'react-bootstrap' +import { CopyInput } from '../components/form' -const Invoice = ({ id, ...props }) => { +const Contacts = ({ invoiceHash }) => { + const subject = `Support request for payment hash: ${invoiceHash}` + const body = 'Hi, I successfully paid for but the action did not work.' + return ( +
+ Payment hash +
+ +
+
+ + e-mail + + \ + + sphinx + + \ + + telegram + + \ + + simplex + +
+
+ ) +} + +const Invoice = ({ id, hash, errorCount, repeat, ...props }) => { const { data, loading, error } = useQuery(INVOICE, { pollInterval: 1000, variables: { id } @@ -22,7 +68,26 @@ const Invoice = ({ id, ...props }) => { if (!data || loading) { return } - return + + let errorStatus = 'Something went wrong. Please try again.' + if (errorCount > 1) { + errorStatus = 'Something still went wrong.\nPlease contact admins for support or to request a refund.' + } + return ( + <> + + {errorCount > 0 + ? ( + <> + + {errorCount === 1 + ?
+ : } + + ) + : null} + + ) } const defaultOptions = { @@ -40,25 +105,51 @@ export const useAnonymous = (fn, options = defaultOptions) => { const showModal = useShowModal() const [fnArgs, setFnArgs] = useState() + // fix for bug where `showModal` runs the code for two modals and thus executes `onConfirmation` twice + let called = false + let errorCount = 0 + const onConfirmation = useCallback( + onClose => { + called = false + return async ({ id, satsReceived, hash }) => { + if (called) return + called = true + await sleep(2000) + const repeat = () => + fn(satsReceived, ...fnArgs, hash) + .then(onClose) + .catch((error) => { + console.error(error) + errorCount++ + onClose() + showModal(onClose => ( + + ), { keepOpen: true }) + }) + // prevents infinite loop of calling `onConfirmation` + if (errorCount === 0) await repeat() + } + }, [fn, fnArgs] + ) + 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 => + showModal(onClose => ( { - setTimeout(async () => { - if (called) return - called = true - await fn(satsReceived, ...fnArgs, invoice.hash) - onClose() - }, 2000) - } - } successVerb='received' - />, { keepOpen: true } + hash={invoice.hash} + onConfirmation={onConfirmation(onClose)} + successVerb='received' + /> + ), { keepOpen: true } ) } }, [invoice?.id]) diff --git a/lib/time.js b/lib/time.js index b1f2e4fe..891482f8 100644 --- a/lib/time.js +++ b/lib/time.js @@ -45,3 +45,5 @@ export function timeLeft (timeStamp) { return parseInt(secondsPast / (3600 * 24)) + ' days' } } + +export const sleep = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms))