import { useMutation, useSuspenseQuery } from '@apollo/client/react';
import { deleteBooksMutation, getBooksQuery } from './list.graphql';
import type { Book } from './Book';

const List: React.FC = () => {
  const { data } = useSuspenseQuery<{ book: Book[] }>(getBooksQuery);
  const [deleteBook] = useMutation(deleteBooksMutation, {
    refetchQueries: ['BooksList'],
  });

  return (
    <table>
      <thead>
        <tr>
          <th>Titel</th>
          <th>ISBN</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {data?.book.map((book) => (
          <tr key={book.id}>
            <td>{book.title}</td>
            <td>{book.isbn}</td>
            <td>
              <button
                onClick={() => deleteBook({ variables: { id: book.id } })}
              >
                löschen
              </button>
            </td>
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default List;
