import { useMemo, useState } from 'react' import { usePromotions } from '../context/PromotionsContext.jsx' export default function PromotionsPage() { const { promotions, createPromotion, removePromotion, setPromotionActive } = usePromotions() const [code, setCode] = useState('') const [value, setValue] = useState(10) const [notice, setNotice] = useState(null) const [error, setError] = useState(null) const sorted = useMemo(() => { return [...promotions].sort((a, b) => { if (a.active !== b.active) return a.active ? -1 : 1 return String(a.code).localeCompare(String(b.code)) }) }, [promotions]) function handleSubmit(e) { e.preventDefault() setError(null) setNotice(null) try { createPromotion({ code, value }) setNotice('Promotion créée.') setCode('') setValue(10) } catch (err) { setError(err?.message || 'Impossible de créer la promotion.') } } return (
Équivalent local de POST /api/promotions (aucun backend).
{error}
) : null} {notice ?{notice}
: null}Astuce : le code est normalisé (majuscules, sans espaces).
Aucune promotion créée.
) : ({p.code}
−{p.value}%