From 896e83806ec91b775f4209d4b74b9247ee28e232 Mon Sep 17 00:00:00 2001 From: Sam Samskies Date: Mon, 15 May 2023 20:27:47 -0500 Subject: [PATCH] handle case where user denies permission to sign event --- README.md | 2 +- src/view.js | 60 +++++++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 94af687..6b5e2ab 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ you can specify relays that you'd like the zap receipt published to using a `dat Add this script tag right before the bottom closing body tag. ```js - + ``` Example Sandbox: https://codesandbox.io/s/nostr-zap-from-anywhere-poc-wiyzgm diff --git a/src/view.js b/src/view.js index 4c3c6be..2debae7 100644 --- a/src/view.js +++ b/src/view.js @@ -142,19 +142,20 @@ const renderAmountDialog = async (npub, relays) => { const dialogHeaderContainer = amountDialog.querySelector( ".dialog-header-container" ); + const handleError = (error) => { + amountDialog.close(); + + const errorDialog = renderErrorDialog(error, npub); + + errorDialog.showModal(); + }; getDialogHeader() .then((htmlString) => { dialogHeaderContainer.innerHTML = htmlString; zapButtton.disabled = false; }) - .catch((error) => { - amountDialog.close(); - - const errorDialog = renderErrorDialog(error, npub); - - errorDialog.showModal(); - }); + .catch(handleError); const setZapButtonToLoadingState = () => { zapButtton.disabled = true; @@ -194,27 +195,32 @@ const renderAmountDialog = async (npub, relays) => { const amount = Number(amountInput.value) * 1000; const comment = commentInput.value; - const zapEvent = await makeZapEvent({ - profile: authorId, - amount, - relays: normalizedRelays, - comment, - }); - const invoice = await fetchInvoice({ - zapEvent, - zapEndpoint: await zapEndpoint, - amount, - comment, - }); - const invoiceDialog = renderInvoiceDialog({ - dialogHeader: await getDialogHeader(), - invoice, - }); - const openWalletButton = invoiceDialog.querySelector(".cta-button"); - amountDialog.close(); - invoiceDialog.showModal(); - openWalletButton.focus(); + try { + const zapEvent = await makeZapEvent({ + profile: authorId, + amount, + relays: normalizedRelays, + comment, + }); + const invoice = await fetchInvoice({ + zapEvent, + zapEndpoint: await zapEndpoint, + amount, + comment, + }); + const invoiceDialog = renderInvoiceDialog({ + dialogHeader: await getDialogHeader(), + invoice, + }); + const openWalletButton = invoiceDialog.querySelector(".cta-button"); + + amountDialog.close(); + invoiceDialog.showModal(); + openWalletButton.focus(); + } catch (error) { + handleError(error); + } }); return amountDialog;