Ville et dirigeant sous le nom, abonnement en pastille (payé, en attente, impayé, résilié) avec la formule, tri (plus récents, nom, clients), état vide distinguant « aucun magasin » et « aucun résultat ». Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
7.6 KiB
TypeScript
166 lines
7.6 KiB
TypeScript
import { useEffect, useState } from 'react';
|
||
import { Link, useNavigate } 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 Subscription({ s }: { s: StoreListItem }) {
|
||
const [label, tone] =
|
||
s.subscriptionStatus === 'ACTIVE'
|
||
? ['Payé', 'ACTIVE']
|
||
: s.subscriptionStatus === 'PAST_DUE'
|
||
? ['Impayé', 'CLOSED']
|
||
: s.subscriptionStatus === 'INCOMPLETE'
|
||
? ['En attente', 'SUSPENDED']
|
||
: s.subscriptionStatus === 'CANCELED'
|
||
? ['Résilié', 'DRAFT']
|
||
: ['Aucun', 'DRAFT'];
|
||
return (
|
||
<span className="row" style={{ gap: 8, flexWrap: 'nowrap' }}>
|
||
<span className={`badge ${tone}`}>{label}</span>
|
||
{s.plan && <span className="role-pill mgr">{PLAN_LABEL[s.plan]}</span>}
|
||
</span>
|
||
);
|
||
}
|
||
|
||
type Sort = 'recent' | 'name' | 'customers';
|
||
const SORTS: [Sort, string][] = [['recent', 'Plus récents'], ['name', 'Nom'], ['customers', 'Clients']];
|
||
|
||
export function StoresPage() {
|
||
const navigate = useNavigate();
|
||
const [q, setQ] = useState('');
|
||
const [status, setStatus] = useState<StoreStatus | 'ALL'>('ALL');
|
||
const [sort, setSort] = useState<Sort>('recent');
|
||
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 items = [...(data?.items ?? [])].sort((a, b) =>
|
||
sort === 'name' ? a.name.localeCompare(b.name) : sort === 'customers' ? b.customers - a.customers : b.createdAt.localeCompare(a.createdAt),
|
||
);
|
||
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;
|
||
const customers = data?.items.reduce((n, s) => n + s.customers, 0) ?? 0;
|
||
const pending = data?.counts.DRAFT ?? 0;
|
||
|
||
return (
|
||
<>
|
||
<div className="row between" style={{ alignItems: 'flex-end' }}>
|
||
<div>
|
||
<h1>Magasins</h1>
|
||
<p className="muted" style={{ margin: 0 }}>{total} magasins inscrits</p>
|
||
</div>
|
||
<Link to="/magasins/nouveau" className="btn">+ Nouveau magasin</Link>
|
||
</div>
|
||
|
||
{/* Chiffres clés (liste affichée) */}
|
||
<div className="kpis">
|
||
<div className="kpi ok"><b>{data?.counts.ACTIVE ?? '—'}</b><span>magasins ouverts</span></div>
|
||
<div className={`kpi${pending ? ' warn' : ''}`}><b>{data ? pending : '—'}</b><span>en cours d’ouverture</span></div>
|
||
<div className={`kpi${unpaid ? ' err' : ''}`}><b>{data ? unpaid : '—'}</b><span>abonnements impayés</span></div>
|
||
<div className="kpi"><b>{data ? customers.toLocaleString('fr-FR') : '—'}</b><span>cartes clients</span></div>
|
||
</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>
|
||
<label className="row" style={{ gap: 8, marginLeft: 'auto' }}>
|
||
<span className="muted" style={{ fontSize: 14 }}>Trier par</span>
|
||
<select className="input" style={{ width: 'auto', height: 40 }} value={sort} onChange={(e) => setSort(e.target.value as Sort)}>
|
||
{SORTS.map(([k, label]) => <option key={k} value={k}>{label}</option>)}
|
||
</select>
|
||
</label>
|
||
<div role="group" aria-label="Filtrer par statut" className="row" style={{ gap: 8, flexBasis: '100%' }}>
|
||
{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, width: '36%' }}>Magasin</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>
|
||
{items.map((s) => (
|
||
<tr key={s.id} className="clickable" onClick={() => navigate(`/magasins/${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>
|
||
<span>
|
||
{s.name}
|
||
<span className="muted" style={{ display: 'block', fontWeight: 400, fontSize: 14 }}>{s.city} · {s.contactName}</span>
|
||
</span>
|
||
</Link>
|
||
</td>
|
||
<td><StatusBadge status={s.status} /></td>
|
||
<td className="num" style={{ fontWeight: 700 }}>{s.customers.toLocaleString('fr-FR')}</td>
|
||
<td><Subscription s={s} /></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', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8 }}>
|
||
<span style={{ fontSize: 36 }} aria-hidden="true">🏪</span>
|
||
{total === 0 ? (
|
||
<>
|
||
<strong>Aucun magasin pour l’instant</strong>
|
||
<p className="muted" style={{ margin: 0 }}>Les commerçants s’inscrivent seuls depuis la page Tarif ; vous pouvez aussi en créer un ici.</p>
|
||
<Link to="/magasins/nouveau" className="btn" style={{ marginTop: 8 }}>Créer un magasin</Link>
|
||
</>
|
||
) : (
|
||
<>
|
||
<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>
|
||
</>
|
||
);
|
||
}
|