From d7dfec49fddacf696e11744ee0f5ade4340b505e Mon Sep 17 00:00:00 2001 From: RKaraMos_EVO Date: Sun, 14 Jun 2026 16:44:55 +0200 Subject: [PATCH 1/2] feat(front): ajouter champ promoCode pour commandes --- my-library/src/pages/Orders.jsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/my-library/src/pages/Orders.jsx b/my-library/src/pages/Orders.jsx index 7b8026d..1a428c7 100644 --- a/my-library/src/pages/Orders.jsx +++ b/my-library/src/pages/Orders.jsx @@ -6,6 +6,7 @@ export default function Orders() { const [paymentMethod, setPaymentMethod] = useState('CREDIT_CARD'); const [address, setAddress] = useState({ street: '', city: '', postalCode: '', country: '' }); const [lines, setLines] = useState([{ bookId: '', quantity: 1 }]); + const [promoCode, setPromoCode] = useState(''); const [message, setMessage] = useState(null); const [submitting, setSubmitting] = useState(false); @@ -37,6 +38,7 @@ export default function Orders() { bookId: Number(line.bookId), quantity: Number(line.quantity), })), + ...(promoCode ? { promoCode } : {}), }; createOrder(payload) @@ -77,6 +79,9 @@ export default function Orders() {

Paiement

+ -- 2.54.0 From 24849c641437b4e123c4bc1630b1f8059f744659 Mon Sep 17 00:00:00 2001 From: RKaraMos_EVO Date: Sun, 14 Jun 2026 16:47:24 +0200 Subject: [PATCH 2/2] feat(front): promoCode validation + estimation du total via API --- my-library/src/pages/Orders.jsx | 66 ++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/my-library/src/pages/Orders.jsx b/my-library/src/pages/Orders.jsx index 1a428c7..425ab9d 100644 --- a/my-library/src/pages/Orders.jsx +++ b/my-library/src/pages/Orders.jsx @@ -1,5 +1,6 @@ import { useState } from 'react'; import { createOrder } from '../api/orders'; +import { getBookById } from '../api/books'; export default function Orders() { const [customerId, setCustomerId] = useState(''); @@ -22,6 +23,10 @@ export default function Orders() { setLines((prev) => [...prev, { bookId: '', quantity: 1 }]); } function removeLine(index) { + const [promoValid, setPromoValid] = useState(true); + const [estimate, setEstimate] = useState(null); + const [estimating, setEstimating] = useState(false); + const [estimateError, setEstimateError] = useState(null); setLines((prev) => prev.filter((_, i) => i !== index)); } @@ -65,6 +70,51 @@ export default function Orders() { onChange={(e) => handleLineChange(index, 'bookId', e.target.value)} required /> handleLineChange(index, 'quantity', e.target.value)} required /> + function validatePromo(code) { + if (!code) return true; + // simple format: PROMO10 => 10% , PROMO5 => 5% + return /^PROMO\d{1,2}$/.test(code); + } + + async function handleEstimate() { + setEstimating(true); + setEstimate(null); + setEstimateError(null); + try { + const ids = lines.map((l) => Number(l.bookId)).filter((n) => !Number.isNaN(n) && n > 0); + if (ids.length === 0) { + setEstimateError('Aucun ISBN valide'); + return; + } + const unique = Array.from(new Set(ids)); + const responses = await Promise.all(unique.map((id) => getBookById(id).then((r) => r.data))); + const priceMap = new Map(); + unique.forEach((id, idx) => priceMap.set(id, responses[idx].prix ?? responses[idx].price ?? 0)); + + let total = 0; + for (const l of lines) { + const id = Number(l.bookId); + const qty = Number(l.quantity) || 0; + total += (priceMap.get(id) || 0) * qty; + } + + let discounted = total; + if (promoCode) { + const m = promoCode.match(/^PROMO(\d{1,2})$/); + if (m) { + const pct = Math.max(0, Math.min(100, Number(m[1]))); + discounted = total * (1 - pct / 100); + } + } + + setEstimate({ total, discounted }); + } catch (err) { + setEstimateError('Erreur lors de l\'estimation'); + console.error(err); + } finally { + setEstimating(false); + } + } {lines.length > 1 && ( )} @@ -80,12 +130,26 @@ export default function Orders() {

Paiement

+ {!promoValid &&
Format de code promo invalide (ex: PROMO10)
} +
+ + {estimateError &&
{estimateError}
} + {estimate && ( +
+
Total estimé : {estimate.total.toFixed(2)} €
+
Après promo : {estimate.discounted.toFixed(2)} €
+
+ )} +
+ -- 2.54.0