import { h, type FunctionalComponent } from 'preact'
import { useEffect, useState } from 'preact/hooks'
import rek from 'rek'

interface Props {
  year: number
}

const Invoices: FunctionalComponent<Props> = ({ year }) => {
  const [invoices, setInvoices] = useState([])

  useEffect(() => {
    rek(`/api/invoices/by-year/${year}`).then(setInvoices)
  }, [year])

  return (
    <table>
      <tbody>
        {invoices.map((invoice) => (
          <tr>
            <td>{invoice.accountNumber}</td>
            <td>{invoice.description}</td>
            <td>{invoice.amount}</td>
          </tr>
        ))}
      </tbody>
    </table>
  )
}

export default Invoices
