import { useEffect, useState } from 'react';
import type { Book } from './Book';

const List: React.FC = () => {
  const [books, setBooks] = useState<Book[]>([]);

  async function fetchData() {
    const response = await fetch('http://localhost:3001/books');
    const data = await response.json();
    setBooks(data);
  }

  useEffect(() => {
    fetchData();
  }, []);

  return (
    <table>
      <thead>
        <tr>
          <th>Titel</th>
          <th>ISBN</th>
        </tr>
      </thead>
      <tbody>
        {books.map((book) => (
          <tr key={book.id}>
            <td>{book.title}</td>
            <td>{book.isbn}</td>
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default List;
