React Reference

useHandleInvoice

useHandleInvoice is a hook that allows you to handle (accept) a single invoice. It returns an object with the following properties:

NameDescription
acceptInvoiceA function that accepts the invoice.
isLoadingBoolean indicating whether the invoice is being accepted.
errorThe error explaining why the last attempt to accept the invoice failed.
isAcceptedBoolean indicating whether the invoice has been accepted.
resetA function that resets the state of the hook (sets isAccepted to false and error to undefined).

Parameters

This hook accepts two parameters:

NameDescription
invoice (required)The id of the invoice to handle (you can get it from the useInvoices hook).
options (optional)see below

Options

NameDescription
onAccepted (optional)A function that will be called when the invoice is accepted. This is a good place to display a success message to the user.
onRejected (optional)A function that will be called when the invoice is rejected. This is a good place to display an error message to the user.
allocationTimeoutMs (optional)The timeout for the allocation in milliseconds. Defaults to 60 seconds.

Example

function MyComponent({ id, status }) {
  const { acceptInvoice, isAccepted, isLoading } = useHandleInvoice(id, {
    onAccepted: () => {
      showToast(`Invoice accepted! 💸`, { type: 'success' })
    },
    onRejected: () => {
      showToast(`There was an error accepting the invoice 😥`, {
        type: 'error',
      })
    },
  })
  return (
    <div>
      <p>{id}</p>
      <p>{status}</p>
      <button
        onClick={acceptInvoice}
        disabled={status !== InvoiceStatus.Received || isAccepted}
      >
        {isLoading ? 'Accepting...' : isAccepted ? 'Accepted!' : 'Accept'}
      </button>
    </div>
  )
}

Was this helpful?