import { useEffect } from 'react';
import { useBooksContext } from './BooksContext';
import BooksListItem from './BooksListItem';

const BooksList: React.FC = () => {
  const [books, setBooks] = useBooksContext();

  useEffect(() => {
    fetch(`${import.meta.env.VITE_API_SERVER}/books`)
      .then((response) => response.json())
      .then((data) => {
        setBooks(data);
      });
  }, []);

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

export default BooksList;
