From ccafc24f72ef3523172bcb23bbf5230df92973fe Mon Sep 17 00:00:00 2001 From: dick Date: Thu, 11 Jun 2026 07:27:37 -0400 Subject: [PATCH 1/2] :sparkles: Ajouter un client --- my-library/src/App.jsx | 2 + my-library/src/api/{books.jsx => books.js} | 0 my-library/src/api/{client.jsx => client.js} | 0 my-library/src/api/customers.js | 9 ++ my-library/src/pages/AddBook.jsx | 2 +- my-library/src/pages/Customers.jsx | 90 ++++++++++++++++++++ 6 files changed, 102 insertions(+), 1 deletion(-) rename my-library/src/api/{books.jsx => books.js} (100%) rename my-library/src/api/{client.jsx => client.js} (100%) create mode 100644 my-library/src/api/customers.js create mode 100644 my-library/src/pages/Customers.jsx diff --git a/my-library/src/App.jsx b/my-library/src/App.jsx index 0b72acb..ff39111 100644 --- a/my-library/src/App.jsx +++ b/my-library/src/App.jsx @@ -7,6 +7,7 @@ import Profile from './pages/Profile'; import NotFound from './pages/NotFound'; import AddBook from './pages/AddBook'; import BookDetail from './pages/BookDetail'; +import Customers from './pages/Customers'; export default function App() { return ( @@ -19,6 +20,7 @@ export default function App() { } /> } /> } /> + } /> ); diff --git a/my-library/src/api/books.jsx b/my-library/src/api/books.js similarity index 100% rename from my-library/src/api/books.jsx rename to my-library/src/api/books.js diff --git a/my-library/src/api/client.jsx b/my-library/src/api/client.js similarity index 100% rename from my-library/src/api/client.jsx rename to my-library/src/api/client.js diff --git a/my-library/src/api/customers.js b/my-library/src/api/customers.js new file mode 100644 index 0000000..114d6ad --- /dev/null +++ b/my-library/src/api/customers.js @@ -0,0 +1,9 @@ +import client from './client'; + +export function registerCustomer(customer) { + return client.post('/api/customers', customer); +} + +export function findCustomerByPhone(phoneNumber) { + return client.get(`/api/customers/phone/${encodeURIComponent(phoneNumber)}`); +} \ No newline at end of file diff --git a/my-library/src/pages/AddBook.jsx b/my-library/src/pages/AddBook.jsx index 5266ab9..5f97471 100644 --- a/my-library/src/pages/AddBook.jsx +++ b/my-library/src/pages/AddBook.jsx @@ -35,7 +35,7 @@ export default function AddBook() { registerBook(payload) .then((response) => { - setMessage({ type: 'success', text: `Livre créé (id : ${response.data}) ✅` }); + setMessage({ type: 'success', text: `Livre créé (id : ${response.data})` }); setForm(initialForm); }) .catch((error) => { diff --git a/my-library/src/pages/Customers.jsx b/my-library/src/pages/Customers.jsx new file mode 100644 index 0000000..0475a08 --- /dev/null +++ b/my-library/src/pages/Customers.jsx @@ -0,0 +1,90 @@ +import { useState } from 'react'; +import { registerCustomer, findCustomerByPhone } from '../api/customers'; + +const initialForm = { firstName: '', lastName: '', phoneNumber: '' }; + +export default function Customers() { + const [form, setForm] = useState(initialForm); + const [registerMsg, setRegisterMsg] = useState(null); + const [submitting, setSubmitting] = useState(false); + + function handleChange(e) { + const { name, value } = e.target; + setForm((prev) => ({ ...prev, [name]: value })); + } + + function handleRegister(e) { + e.preventDefault(); + setSubmitting(true); + setRegisterMsg(null); + registerCustomer(form) + .then((response) => { + setRegisterMsg(`Client créé (id : ${response.data})`); + setForm(initialForm); + }) + .catch((error) => { + console.error(error); + setRegisterMsg('Erreur lors de la création du client.'); + }) + .finally(() => setSubmitting(false)); + } + + const [phone, setPhone] = useState(''); + const [customer, setCustomer] = useState(null); + const [searchError, setSearchError] = useState(null); + + function handleSearch(e) { + e.preventDefault(); + setSearchError(null); + setCustomer(null); + findCustomerByPhone(phone) + .then((response) => setCustomer(response.data)) + .catch((error) => { + console.error(error); + setSearchError('Aucun client trouvé avec ce numéro.'); + }); + } + + return ( +
+

Gestion des clients

+ +
+

Enregistrer un client

+
+ + + + +
+ {registerMsg &&

{registerMsg}

} +
+ +
+

Rechercher un client

+
+ + +
+ {searchError &&

{searchError}

} + {customer && ( +
+

{customer.firstName} {customer.lastName}

+

Téléphone : {customer.phoneNumber}

+

Points de fidélité : {customer.loyaltyPoints}

+
+ )} +
+
+ ); +} \ No newline at end of file -- 2.54.0 From e33acdf151186771c6821903301d3104c6a5cee1 Mon Sep 17 00:00:00 2001 From: dick Date: Thu, 11 Jun 2026 07:59:03 -0400 Subject: [PATCH 2/2] =?UTF-8?q?:sparkles:=20Ajouter=20et=20Retirer=20des?= =?UTF-8?q?=20points=20de=20fid=C3=A9lit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- my-library/src/api/customers.js | 8 ++++++++ my-library/src/pages/Customers.jsx | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/my-library/src/api/customers.js b/my-library/src/api/customers.js index 114d6ad..37483fe 100644 --- a/my-library/src/api/customers.js +++ b/my-library/src/api/customers.js @@ -6,4 +6,12 @@ export function registerCustomer(customer) { export function findCustomerByPhone(phoneNumber) { return client.get(`/api/customers/phone/${encodeURIComponent(phoneNumber)}`); +} + +export function addLoyaltyPoints(customerId, points) { + return client.post(`/api/customers/${customerId}/loyalty/add`, null, { params: { points } }); +} + +export function subtractLoyaltyPoints(customerId, points) { + return client.post(`/api/customers/${customerId}/loyalty/subtract`, null, { params: { points } }); } \ No newline at end of file diff --git a/my-library/src/pages/Customers.jsx b/my-library/src/pages/Customers.jsx index 0475a08..514961b 100644 --- a/my-library/src/pages/Customers.jsx +++ b/my-library/src/pages/Customers.jsx @@ -1,5 +1,5 @@ import { useState } from 'react'; -import { registerCustomer, findCustomerByPhone } from '../api/customers'; +import { registerCustomer, findCustomerByPhone, addLoyaltyPoints, subtractLoyaltyPoints } from '../api/customers'; const initialForm = { firstName: '', lastName: '', phoneNumber: '' }; @@ -7,6 +7,7 @@ export default function Customers() { const [form, setForm] = useState(initialForm); const [registerMsg, setRegisterMsg] = useState(null); const [submitting, setSubmitting] = useState(false); + const [pointsInput, setPointsInput] = useState(''); function handleChange(e) { const { name, value } = e.target; @@ -44,6 +45,21 @@ export default function Customers() { setSearchError('Aucun client trouvé avec ce numéro.'); }); } + function applyLoyalty(operation) { + const points = Number(pointsInput); + const action = operation === 'add' ? addLoyaltyPoints : subtractLoyaltyPoints; + + action(customer.id, points) + .then((response) => { + setCustomer((prev) => ({ ...prev, loyaltyPoints: response.data })); + setPointsInput(''); + }) + .catch((error) => { + console.error(error); + if (error.response?.status === 400) setSearchError('Pas assez de points pour ce retrait.'); + else setSearchError('Erreur lors de la mise à jour des points.'); + }); + } return (
@@ -82,6 +98,9 @@ export default function Customers() {

{customer.firstName} {customer.lastName}

Téléphone : {customer.phoneNumber}

Points de fidélité : {customer.loyaltyPoints}

+ setPointsInput(e.target.value)}placeholder="Nombre de points"/> + + )} -- 2.54.0