reference

useInvoices

useInvoices is a hook that fetches all invoices known to the connected yagna instance. It paginates data by default. Under the hood it uses SWR so you get all the benefits of caching and revalidation. It's also possible to configure that behavior by passing the swrConfig parameter. It returns an object with the following properties:

NameDescription
invoicesArray of Invoices (see below)
isLoadingBoolean indicating whether the invoices are being fetched.
errorError object containing information about the error that occurred while fetching the invoices.
isValidatingBoolean indicating whether the invoices are being revalidated.
refetchA function that can be used to manually trigger a refetch.

Invoice object type

NameType
invoiceIdString
issuerIdString
recipientIdString
payeeAddrString
payerAddrString
paymentPlatformString
timestampString
agreementIdString
activityIdsArray of Strings
amountString
paymentDueDateString
statusOne of ISSUED RECEIVED ACCEPTED REJECTED FAILED SETTLED CANCELLED

Parameters

This hook accepts a single configuration object with the following properties:

NameDescription
after (optional)A string representing the timestamp to start fetching invoices from. If not provided, the hook will fetch invoices starting from the earliest invoice known to the connected yagna instance.
limit (optional)The maximum number of invoices to fetch. Defaults to 10.
swrConfig (optional)SWR configuration object

To achieve pagination, pass the timestamp of the last invoice in the after parameter of the next call.

Example

function MyComponent() {
  const { invoices, isLoading, error, refetch } = useInvoices()
  if (isLoading) {
    return <div>Loading...</div>
  }
  if (error) {
    return <div>Error: {error.toString()}</div>
  }
  return (
    <div>
      <ul>
        {invoices.map((invoice) => (
          <li key={invoice.invoiceId}>
            {invoice.invoiceId} - {invoice.status}
          </li>
        ))}
      </ul>
      <button onClick={refetch}> Refresh </button>
    </div>
  )
}

With pagination:

function MyComponent() {
  const [after, setAfter] = useState()
  const { invoices, isLoading, error, refetch } = useInvoices({ after })
  if (isLoading) {
    return <div>Loading...</div>
  }
  if (error) {
    return <div>Error: {error.toString()}</div>
  }
  return (
    <div>
      <ul>
        {invoices.map((invoice) => (
          <li key={invoice.invoiceId}>
            {invoice.invoiceId} - {invoice.status}
          </li>
        ))}
      </ul>
      <button onClick={() => setAfter(invoices[invoices.length - 1].timestamp)}>
        Next
      </button>
      <button onClick={refetch}> Refresh </button>
    </div>
  )
}

Was this helpful?