2026-06-11 04:32:20 -04:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
|
import { getBooks } from '../api/books';
|
2026-06-11 05:15:30 -04:00
|
|
|
import { Link } from 'react-router-dom';
|
2026-06-11 04:32:20 -04:00
|
|
|
|
2026-06-10 16:24:37 -04:00
|
|
|
export default function Books() {
|
2026-06-11 04:32:20 -04:00
|
|
|
const [books, setBooks] = useState([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getBooks()
|
|
|
|
|
.then((response) => {
|
|
|
|
|
console.log(response.data);
|
|
|
|
|
setBooks(response.data.content);
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
setError("Impossible de charger les livres.");
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
if (loading) return <main><p>Chargement…</p></main>;
|
|
|
|
|
if (error) return <main><p>{error}</p></main>;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<main>
|
|
|
|
|
<h1>Catalogue</h1>
|
2026-06-11 05:15:30 -04:00
|
|
|
<Link to="/books/new">+ Ajouter un livre</Link>
|
2026-06-11 04:32:20 -04:00
|
|
|
<ul>
|
|
|
|
|
{books.map((book) => (
|
|
|
|
|
<li key={book.isbn}>
|
2026-06-11 05:52:26 -04:00
|
|
|
<Link to={`/books/${book.isbn}`}>
|
|
|
|
|
<strong>{book.title}</strong> - {book.author}
|
|
|
|
|
</Link>
|
2026-06-11 04:32:20 -04:00
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
2026-06-10 16:24:37 -04:00
|
|
|
}
|