diff --git a/my-library/src/App.jsx b/my-library/src/App.jsx index ec7c2b3..4873915 100644 --- a/my-library/src/App.jsx +++ b/my-library/src/App.jsx @@ -5,6 +5,7 @@ import Books from './pages/Books'; import Orders from './pages/Orders'; import Profile from './pages/Profile'; import NotFound from './pages/NotFound'; +import AddBook from './pages/AddBook'; export default function App() { return ( @@ -15,6 +16,7 @@ export default function App() { } /> } /> } /> + } /> ); diff --git a/my-library/src/api/books.jsx b/my-library/src/api/books.jsx index 60a4c3a..c9ba492 100644 --- a/my-library/src/api/books.jsx +++ b/my-library/src/api/books.jsx @@ -2,4 +2,8 @@ import client from './client'; export function getBooks(page = 0, size = 20) { return client.get('/api/books', { params: { page, size } }); +} + +export function registerBook(book) { + return client.post('/api/books', book); } \ No newline at end of file diff --git a/my-library/src/pages/AddBook.jsx b/my-library/src/pages/AddBook.jsx new file mode 100644 index 0000000..5266ab9 --- /dev/null +++ b/my-library/src/pages/AddBook.jsx @@ -0,0 +1,105 @@ +import { useState } from 'react'; +import { registerBook } from '../api/books'; + +const CATEGORIES = ['FICTION', 'NON_FICTION', 'SCIENCE_FICTION', 'FANTASY', 'MYSTERY', 'THRILLER', 'ROMANCE', 'BIOGRAPHY', 'HISTORY', 'POETRY', 'CHILDRENS', 'YOUNG_ADULT', 'SCIENCE', 'PHILOSOPHY', 'SELF_HELP', 'TRAVEL', 'COOKING', 'ART', 'RELIGION', 'REFERENCE']; + +const initialForm = { + isbn: '', title: '', author: '', publisher: '', publicationDate: '', price: '', quantity: '', categories: [], description: '', language: '', +}; + +export default function AddBook() { + const [form, setForm] = useState(initialForm); + const [message, setMessage] = useState(null); + const [submitting, setSubmitting] = useState(false); + + function handleChange(e) { + const { name, value } = e.target; + setForm((prev) => ({ ...prev, [name]: value })); + } + + function handleCategoryChange(e) { + setForm((prev) => ({ ...prev, categories: e.target.value ? [e.target.value] : [] })); + } + + function handleSubmit(e) { + e.preventDefault(); + setSubmitting(true); + setMessage(null); + + const payload = { + ...form, + isbn: Number(form.isbn), + price: Number(form.price), + quantity: Number(form.quantity), + }; + + registerBook(payload) + .then((response) => { + setMessage({ type: 'success', text: `Livre créé (id : ${response.data}) ✅` }); + setForm(initialForm); + }) + .catch((error) => { + console.error(error); + const status = error.response?.status; + if (status === 406) setMessage({ type: 'error', text: 'Ce livre existe déjà.' }); + else if (status === 400) setMessage({ type: 'error', text: 'Données invalides, vérifie les champs.' }); + else setMessage({ type: 'error', text: 'Erreur lors de la création.' }); + }) + .finally(() => setSubmitting(false)); + } + + return ( + + Ajouter un livre + + + + ISBN + + + + Titre + + + + Auteur + + + + Éditeur + + + + Date de publication + + + + Prix + + + + Quantité + + + + Langue + + + + Catégorie + choisir {CATEGORIES.map((c) => {c})} + + + Description + + + + + {submitting ? 'Envoi…' : 'Ajouter le livre'} + + + + {message && {message.text}} + + ); +} \ No newline at end of file diff --git a/my-library/src/pages/Books.jsx b/my-library/src/pages/Books.jsx index 21b2ebb..393db4e 100644 --- a/my-library/src/pages/Books.jsx +++ b/my-library/src/pages/Books.jsx @@ -1,5 +1,6 @@ import { useState, useEffect } from 'react'; import { getBooks } from '../api/books'; +import { Link } from 'react-router-dom'; export default function Books() { const [books, setBooks] = useState([]); @@ -27,6 +28,7 @@ export default function Books() { return ( Catalogue + + Ajouter un livre {books.map((book) => (
{message.text}