MonEspacePro/admin/src/pages/StoresPage.tsx
Tony AVERTY fe6874458d Socle des formules d'abonnement (Essentiel 19 €, Pro 39 €, Réseau 29 €/magasin)
- Catalogue src/billing/plans.ts : prix mensuels/annuels (2 mois offerts),
  fonctions incluses et limites ; ancien prix 10 € = tarif fondateur
- Formule enregistrée sur l'abonnement, déduite du prix Stripe (lookup_key) :
  un changement fait depuis le portail client est repris automatiquement
- Lien d'abonnement au choix (formule, périodicité), essai 30 jours (une fois
  par magasin), codes promo ; e-mail de fin d'essai (trial_will_end)
- Portail client Stripe (changer de formule, moyen de paiement, résilier)
- EntitlementsService : droits par formule ; appliqué à la limite de
  3 employés en Essentiel et au retrait de « Propulsé par » en Pro
- Factures : libellé de la formule et de la périodicité
- scripts/stripe-setup.ts idempotent (produits, prix, portail, webhook)
- Admin : choix de la formule, badges, espace abonnement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 10:09:53 +02:00

120 lines
5.1 KiB
TypeScript

import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { api, PLAN_LABEL, StoreListItem, StoreStatus } from '../api';
import { STATUS_LABEL, StatusBadge } from '../components/Layout';
const FILTERS: (StoreStatus | 'ALL')[] = ['ALL', 'DRAFT', 'ACTIVE', 'SUSPENDED', 'CLOSED'];
function initials(name: string) {
return name
.split(' ')
.filter((w) => w.length > 2)
.slice(-2)
.map((w) => w[0])
.join('')
.toUpperCase();
}
function subscriptionLabel(s: StoreListItem['subscriptionStatus']) {
if (s === 'ACTIVE') return <span style={{ color: 'var(--ok)', fontWeight: 600 }}>Actif</span>;
if (s === 'PAST_DUE') return <span style={{ color: 'var(--err)', fontWeight: 600 }}>Impayé</span>;
if (s === 'INCOMPLETE') return <span className="muted">En attente</span>;
return <span className="muted">—</span>;
}
export function StoresPage() {
const [q, setQ] = useState('');
const [status, setStatus] = useState<StoreStatus | 'ALL'>('ALL');
const [data, setData] = useState<{ items: StoreListItem[]; counts: Partial<Record<StoreStatus, number>> } | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
// Petite temporisation : pas une requête par frappe.
const t = setTimeout(() => {
const params = new URLSearchParams();
if (q.trim()) params.set('q', q.trim());
if (status !== 'ALL') params.set('status', status);
api
.get<typeof data>(`/api/admin/stores?${params}`)
.then(setData)
.catch((e) => setError(e.message));
}, 250);
return () => clearTimeout(t);
}, [q, status]);
const total = data ? Object.values(data.counts).reduce((a, b) => a + (b ?? 0), 0) : 0;
const unpaid = data?.items.filter((s) => s.subscriptionStatus === 'PAST_DUE').length ?? 0;
return (
<>
<div className="row between" style={{ alignItems: 'flex-end' }}>
<div>
<h1>Magasins</h1>
<p className="muted" style={{ margin: 0 }}>
{total} magasins inscrits · {unpaid} abonnements impayés
</p>
</div>
<Link to="/magasins/nouveau" className="btn">+ Nouveau magasin</Link>
</div>
<div className="row">
<div style={{ flex: '1 1 320px', maxWidth: 420 }}>
<label htmlFor="search" className="sr-only">Rechercher un magasin</label>
<input id="search" type="search" className="input" placeholder="Rechercher par nom ou ville" value={q} onChange={(e) => setQ(e.target.value)} />
</div>
<div role="group" aria-label="Filtrer par statut" className="row" style={{ gap: 8 }}>
{FILTERS.map((f) => (
<button key={f} type="button" className="chip" aria-pressed={status === f} onClick={() => setStatus(f)}>
{f === 'ALL' ? 'Tous' : STATUS_LABEL[f]}
<span style={{ opacity: 0.8, fontWeight: 500 }}>{f === 'ALL' ? total : data?.counts[f] ?? 0}</span>
</button>
))}
</div>
</div>
{error && <div className="alert error">{error}</div>}
<div className="table-wrap">
<div className="table-scroll">
<table style={{ minWidth: 860 }}>
<thead>
<tr>
<th scope="col" style={{ paddingLeft: 24 }}>Magasin</th>
<th scope="col">Ville</th>
<th scope="col">Statut</th>
<th scope="col" className="num">Clients</th>
<th scope="col">Abonnement</th>
<th scope="col">Créé le</th>
</tr>
</thead>
<tbody>
{data?.items.map((s) => (
<tr key={s.id}>
<td style={{ paddingLeft: 24 }}>
<Link to={`/magasins/${s.id}`} style={{ display: 'inline-flex', alignItems: 'center', gap: 12, color: 'var(--text)', fontWeight: 600, textDecoration: 'none' }}>
<span style={{ width: 36, height: 36, borderRadius: 10, background: 'var(--primary-soft)', color: 'var(--primary)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontSize: 13, fontWeight: 800 }}>
{initials(s.name)}
</span>
{s.name}
</Link>
</td>
<td>{s.city}</td>
<td><StatusBadge status={s.status} /></td>
<td className="num">{s.customers}</td>
<td>{subscriptionLabel(s.subscriptionStatus)}{s.plan && <span className="muted" style={{ fontSize: 14 }}> · {PLAN_LABEL[s.plan]}</span>}</td>
<td className="muted">{new Date(s.createdAt).toLocaleDateString('fr-FR', { day: 'numeric', month: 'short', year: 'numeric' })}</td>
</tr>
))}
</tbody>
</table>
</div>
{data && data.items.length === 0 && (
<div style={{ padding: '48px 24px', textAlign: 'center' }}>
<strong>Aucun magasin trouvé</strong>
<p className="muted" style={{ margin: 0 }}>Essayez un autre nom, une autre ville ou retirez le filtre de statut.</p>
</div>
)}
</div>
</>
);
}