diff --git a/components/dont-link-this.js b/components/dont-link-this.js index 90464e32..ae3c11d8 100644 --- a/components/dont-link-this.js +++ b/components/dont-link-this.js @@ -2,8 +2,6 @@ import { gql, useMutation } from '@apollo/client' import Dropdown from 'react-bootstrap/Dropdown' import { useShowModal } from './modal' import { useToast } from './toast' -import { InvoiceModal, payOrLoginError } from './invoice' -import { DONT_LIKE_THIS_COST } from '../lib/constants' import ItemAct from './item-act' export default function DontLikeThisDropdownItem ({ id }) { @@ -40,22 +38,7 @@ export default function DontLikeThisDropdownItem ({ id }) { }} itemId={id} act={dontLikeThis} down />) } catch (error) { - console.error(error) - if (payOrLoginError(error)) { - showModal(onClose => { - return ( - { - await dontLikeThis({ variables: { id, hash, hmac } }) - toaster.success('item flagged') - }} - /> - ) - }) - } else { - toaster.danger('failed to flag item') - } + toaster.danger('failed to flag item') } }} > diff --git a/components/invoice.js b/components/invoice.js index cb9e7218..04c2283e 100644 --- a/components/invoice.js +++ b/components/invoice.js @@ -149,6 +149,9 @@ const defaultOptions = { callback: null, // (formValues) => void replaceModal: false } +// TODO: refactor this so it can be easily understood +// there's lots of state cascading paired with logic +// independent of the state, and it's hard to follow export const useInvoiceable = (onSubmit, options = defaultOptions) => { const me = useMe() const [createInvoice, { data }] = useMutation(gql` @@ -247,17 +250,14 @@ export const useInvoiceable = (onSubmit, options = defaultOptions) => { // tell onSubmit handler that we want to keep local storage // even though the submit handler was "successful" return { keepLocalStorage: true } - }, [onSubmit, setFormValues, setSubmitArgs, createInvoice]) + }, [onSubmit, setFormValues, setSubmitArgs, createInvoice, !!me]) return onSubmitWrapper } -export const InvoiceModal = ({ onPayment, amount }) => { - const createInvoice = useInvoiceable(onPayment, { replaceModal: true }) - - useEffect(() => { - createInvoice({ amount }) - }, []) +export const useInvoiceModal = (onPayment, deps) => { + const onPaymentMemo = useCallback(onPayment, deps) + return useInvoiceable(onPaymentMemo, { replaceModal: true }) } export const payOrLoginError = (error) => { diff --git a/components/item-act.js b/components/item-act.js index ef50241b..d249074d 100644 --- a/components/item-act.js +++ b/components/item-act.js @@ -67,7 +67,7 @@ export default function ItemAct ({ onClose, itemId, act, down, strike }) { return (
{ + await act({ variables: { ...variables, hash, hmac } }) + }, [act]) const handlePayBounty = async onComplete => { try { @@ -74,16 +77,7 @@ export default function PayBounty ({ children, item }) { onComplete() } catch (error) { if (payOrLoginError(error)) { - showModal(onClose => { - return ( - { - await act({ variables: { id: item.id, sats: root.bounty, hash, hmac } }) - }} - /> - ) - }) + showInvoiceModal({ amount: root.bounty }, { variables: { id: item.id, sats: root.bounty } }) return } throw new Error({ message: error.toString() }) diff --git a/components/poll.js b/components/poll.js index 1a0b8fc6..987f96ee 100644 --- a/components/poll.js +++ b/components/poll.js @@ -7,13 +7,11 @@ import styles from './poll.module.css' import Check from '../svgs/checkbox-circle-fill.svg' import { signIn } from 'next-auth/react' import ActionTooltip from './action-tooltip' -import { useShowModal } from './modal' import { POLL_COST } from '../lib/constants' -import { InvoiceModal } from './invoice' +import { payOrLoginError, useInvoiceModal } from './invoice' export default function Poll ({ item }) { const me = useMe() - const showModal = useShowModal() const [pollVote] = useMutation( gql` mutation pollVote($id: ID!, $hash: String, $hmac: String) { @@ -47,6 +45,12 @@ export default function Poll ({ item }) { ) const PollButton = ({ v }) => { + const showInvoiceModal = useInvoiceModal(async ({ hash, hmac }, { variables }) => { + await pollVote({ variables: { ...variables, hash, hmac } }) + }, [pollVote]) + + const variables = { id: v.id } + return (