From 2c0f30f0a22716b97f7cab1db3d270e4354aeb57 Mon Sep 17 00:00:00 2001 From: dick Date: Thu, 11 Jun 2026 05:15:30 -0400 Subject: [PATCH] :sparkles: Enregistrer un livre --- my-library/src/App.jsx | 2 + my-library/src/api/books.jsx | 4 ++ my-library/src/pages/AddBook.jsx | 105 +++++++++++++++++++++++++++++++ my-library/src/pages/Books.jsx | 2 + 4 files changed, 113 insertions(+) create mode 100644 my-library/src/pages/AddBook.jsx 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
Description