From 7192efcfd939d8a8566238d13ed737306421e7f1 Mon Sep 17 00:00:00 2001 From: AISSI-JUDE-CHRIST Date: Mon, 15 Jun 2026 23:42:20 +0200 Subject: [PATCH] correction syntaxe appliquer promo --- my-library/src/pages/Orders.jsx | 133 +++++++++++++++++--------------- 1 file changed, 72 insertions(+), 61 deletions(-) diff --git a/my-library/src/pages/Orders.jsx b/my-library/src/pages/Orders.jsx index cc36f0e..5867e62 100644 --- a/my-library/src/pages/Orders.jsx +++ b/my-library/src/pages/Orders.jsx @@ -8,6 +8,10 @@ export default function Orders() { const [address, setAddress] = useState({ street: '', city: '', postalCode: '', country: '' }); const [lines, setLines] = useState([{ bookId: '', quantity: 1 }]); const [promoCode, setPromoCode] = useState(''); + const [promoValid, setPromoValid] = useState(true); + const [estimate, setEstimate] = useState(null); + const [estimating, setEstimating] = useState(false); + const [estimateError, setEstimateError] = useState(null); const [message, setMessage] = useState(null); const [submitting, setSubmitting] = useState(false); @@ -19,17 +23,66 @@ export default function Orders() { function handleLineChange(index, field, value) { setLines((prev) => prev.map((line, i) => (i === index ? { ...line, [field]: value } : line))); } + function addLine() { 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)); } + function validatePromo(code) { + if (!code) return true; + return /^PROMO\d{1,2}$/.test(code); + } + + function handlePromoChange(e) { + const code = e.target.value; + setPromoCode(code); + setPromoValid(validatePromo(code)); + } + + async function handleEstimate() { + setEstimating(true); + setEstimate(null); + setEstimateError(null); + try { + const ids = lines.map((l) => Number(l.bookId)).filter((n) => !isNaN(n) && n > 0); + if (ids.length === 0) { + setEstimateError('Aucun ISBN valide.'); + return; + } + const unique = [...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); + } + } + function handleSubmit(e) { e.preventDefault(); setSubmitting(true); @@ -47,7 +100,7 @@ export default function Orders() { }; createOrder(payload) - .then((response) => setMessage('Commande créée')) + .then(() => setMessage('Commande créée avec succès !')) .catch((error) => { console.error(error); setMessage(error.response?.data?.message || 'Erreur lors de la commande.'); @@ -65,56 +118,11 @@ export default function Orders() {

Livres

{lines.map((line, index) => ( -
+
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 && ( )} @@ -129,32 +137,35 @@ export default function Orders() {

Paiement

-