- Anniversaire facultatif (jour + mois) à l'inscription ou sur la page de la carte
- Réglages par magasin : activation, points/tampons offerts, message {prenom} {cadeau}
- Passage horaire 9 h–19 h (Paris), un cadeau par an, 29 février fêté le 28
- Notification Apple (Actualité) et Google, solde mis à jour
- Admin : onglet Fidélisation ; app Pro : écran gérant
- Politique de confidentialité mise à jour
- Migration manuelle 2026-10-07-anniversaire.sql
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
127 lines
4.7 KiB
TypeScript
127 lines
4.7 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import { api } from '../api';
|
|
import { RemindersCard } from './RemindersCard';
|
|
import { BirthdayCard } from './BirthdayCard';
|
|
|
|
interface Campaign {
|
|
id: string;
|
|
message: string;
|
|
sentByName: string;
|
|
cards: number;
|
|
appleDevices: number;
|
|
createdAt: string;
|
|
}
|
|
|
|
interface Overview {
|
|
allowed: boolean;
|
|
audience: { cards: number; appleDevices: number };
|
|
nextAllowedAt: string | null;
|
|
items: Campaign[];
|
|
}
|
|
|
|
const MAX = 160;
|
|
const when = (d: string) =>
|
|
new Date(d).toLocaleString('fr-FR', { day: 'numeric', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit' });
|
|
|
|
export function CampaignsTab({ storeId, storeName }: { storeId: string; storeName: string }) {
|
|
const [data, setData] = useState<Overview | null>(null);
|
|
const [message, setMessage] = useState('');
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const load = useCallback(() => {
|
|
api.get<Overview>(`/api/admin/stores/${storeId}/campaigns`).then(setData).catch((e) => setError(e.message));
|
|
}, [storeId]);
|
|
useEffect(load, [load]);
|
|
|
|
if (!data) return error ? <div className="alert error">{error}</div> : <p className="muted">Chargement…</p>;
|
|
|
|
const text = message.trim();
|
|
const blocked = !data.allowed || !!data.nextAllowedAt;
|
|
|
|
async function send() {
|
|
if (!confirm(`Envoyer ce message aux ${data!.audience.cards} cartes de ${storeName} ?\n\n« ${text} »`)) return;
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api.post(`/api/admin/stores/${storeId}/campaigns`, { message: text });
|
|
setMessage('');
|
|
setNotice('Campagne envoyée. Les cartes se mettent à jour dans les minutes qui suivent.');
|
|
load();
|
|
} catch (e) {
|
|
setError((e as Error).message);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{error && <div className="alert error" role="alert">{error}</div>}
|
|
{notice && <div className="alert ok" role="status">{notice}</div>}
|
|
{!data.allowed && <div className="alert warn">Les campagnes sont réservées aux formules Pro et Réseau (abonnement actif).</div>}
|
|
|
|
<section className="card" style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
|
<div>
|
|
<h2>Nouvelle campagne</h2>
|
|
<p className="muted" style={{ margin: 0 }}>
|
|
Le message apparaît en notification sur le téléphone des clients et reste visible sur leur carte.
|
|
Audience : <strong>{data.audience.cards}</strong> cartes, dont <strong>{data.audience.appleDevices}</strong> iPhone joignables (Google Wallet : tous les clients qui ont ajouté la carte).
|
|
</p>
|
|
</div>
|
|
<label htmlFor="campaign-msg" className="sr-only">Message</label>
|
|
<textarea
|
|
id="campaign-msg"
|
|
className="input"
|
|
rows={3}
|
|
maxLength={MAX}
|
|
placeholder="Ex. : -20 % sur les viennoiseries ce samedi, sur présentation de votre carte !"
|
|
value={message}
|
|
onChange={(e) => setMessage(e.target.value)}
|
|
disabled={blocked || busy}
|
|
/>
|
|
<div className="row between">
|
|
<span className="muted" style={{ fontSize: 14 }}>{text.length} / {MAX} caractères · 1 campagne par jour maximum</span>
|
|
<button className="btn" disabled={blocked || busy || text.length < 5} onClick={send}>
|
|
{busy ? 'Envoi…' : 'Envoyer la campagne'}
|
|
</button>
|
|
</div>
|
|
{data.nextAllowedAt && <div className="alert warn">Prochain envoi possible le {when(data.nextAllowedAt)}.</div>}
|
|
</section>
|
|
|
|
<RemindersCard storeId={storeId} />
|
|
<BirthdayCard storeId={storeId} />
|
|
|
|
<section className="card">
|
|
<h2>Historique</h2>
|
|
{data.items.length === 0 ? (
|
|
<p className="muted" style={{ margin: 0 }}>Aucune campagne envoyée pour l'instant.</p>
|
|
) : (
|
|
<div className="table-scroll">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Date</th>
|
|
<th scope="col">Message</th>
|
|
<th scope="col">Envoyée par</th>
|
|
<th scope="col" className="num">Cartes</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{data.items.map((c) => (
|
|
<tr key={c.id}>
|
|
<td className="muted" style={{ whiteSpace: 'nowrap' }}>{when(c.createdAt)}</td>
|
|
<td>{c.message}</td>
|
|
<td>{c.sentByName}</td>
|
|
<td className="num">{c.cards}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</section>
|
|
</>
|
|
);
|
|
}
|