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 (

Promotions

Équivalent local de POST /api/promotions (aucun backend).

{error ? (

{error}

) : null} {notice ?

{notice}

: null}

Créer une promotion

Astuce : le code est normalisé (majuscules, sans espaces).

Promotions existantes

{sorted.length === 0 ? (

Aucune promotion créée.

) : (
    {sorted.map((p) => (
  • {p.code}

    −{p.value}%

  • ))}
)}
) }