MonEspacePro/admin/src/pages/MerchantSpace.tsx
Tony AVERTY f51920f1a6 Espace web du Dirigeant retravaillé
Barre du haut fixe, bandeau d'accueil, étapes d'ouverture (abonnement,
carte, programme ouvert), raccourcis : lien d'inscription à copier,
affichette, app MonEspacePro ; conception de la carte en dessous.

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

203 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { FormEvent, useCallback, useEffect, useState } from 'react';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { api, ApiError, loginStaff, logout, Plan, PLAN_LABEL, StoreStatus, SubscriptionStatus } from '../api';
import { Brand } from '../components/Layout';
import { CardWizard } from './CardWizard';
interface MerchantStore {
id: string;
name: string;
status: StoreStatus;
enrollUrl: string;
subscription: { status: SubscriptionStatus; plan: Plan } | null;
subscribeUrl: string | null;
}
/** Connexion du Dirigeant à son espace web. */
export function MerchantLoginPage({ onLogin }: { onLogin: () => void }) {
const navigate = useNavigate();
const [params] = useSearchParams();
const welcome = params.has('bienvenue');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
async function submit(e: FormEvent) {
e.preventDefault();
setBusy(true);
setError(null);
try {
await loginStaff(email, password);
onLogin();
navigate('/espace', { replace: true });
} catch (err) {
// Juste après le paiement, le webhook Stripe peut avoir quelques secondes de retard.
setError(
welcome && err instanceof ApiError && err.status === 401
? 'Votre espace est en cours de création. Réessayez dans quelques secondes.'
: (err as Error).message,
);
} finally {
setBusy(false);
}
}
return (
<main style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', padding: 16 }}>
<form className="card" onSubmit={submit} style={{ width: '100%', maxWidth: 420, display: 'flex', flexDirection: 'column', gap: 20 }}>
<Brand admin={false} />
{welcome && <div className="alert ok">Paiement reçu, merci ! Connectez-vous avec l’e-mail et le mot de passe choisis à l’inscription.</div>}
<div>
<h1 style={{ fontSize: 26 }}>Mon espace</h1>
<p className="muted" style={{ margin: 0 }}>Mêmes identifiants que l’app MonEspacePro.</p>
</div>
{error && <div className="alert error" role="alert">{error}</div>}
<div className="field">
<label htmlFor="m-email">E-mail</label>
<input id="m-email" className="input" type="email" autoComplete="username" required value={email} onChange={(e) => setEmail(e.target.value)} />
</div>
<div className="field">
<label htmlFor="m-password">Mot de passe</label>
<input id="m-password" className="input" type="password" autoComplete="current-password" required value={password} onChange={(e) => setPassword(e.target.value)} />
</div>
<button className="btn" disabled={busy}>{busy ? 'Connexion…' : 'Se connecter'}</button>
<p className="muted" style={{ margin: 0, fontSize: 14, textAlign: 'center' }}>
Pas encore client ? <Link to="/inscription">Créer mon programme</Link>
</p>
</form>
</main>
);
}
/** Espace web du Dirigeant : état du magasin, abonnement, conception de la carte. */
export function MerchantSpace() {
const [store, setStore] = useState<MerchantStore | null>(null);
const [error, setError] = useState<string | null>(null);
const [copied, setCopied] = useState(false);
const [hasCard, setHasCard] = useState<boolean | null>(null);
const reload = useCallback(() => {
api
.get<MerchantStore>('/api/staff/store')
.then(setStore)
.catch((e) => setError(e instanceof ApiError && e.status === 403 ? 'Cet espace est réservé au Dirigeant du magasin. Utilisez l’app MonEspacePro pour scanner les cartes.' : e.message));
api
.get<unknown>('/api/staff/card-design')
.then((d) => setHasCard(!!d))
.catch(() => setHasCard(null));
}, []);
useEffect(reload, [reload]);
function copy(url: string) {
navigator.clipboard?.writeText(url).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
});
}
const paid = !!store && !store.subscribeUrl;
const open = store?.status === 'ACTIVE';
// Parcours d'ouverture : abonnement → carte → programme ouvert.
const steps: [string, string, boolean][] = [
['Abonnement', paid ? `Formule ${store?.subscription ? PLAN_LABEL[store.subscription.plan] : ''} active` : 'En attente du paiement', paid],
['Carte de fidélité', hasCard ? 'Conçue' : 'Logo, couleurs, récompenses', !!hasCard],
['Programme ouvert', open ? 'Vos clients peuvent s’inscrire' : 'S’ouvre automatiquement', open],
];
const current = steps.findIndex(([, , done]) => !done);
return (
<div style={{ minHeight: '100vh' }}>
<header className="m-top">
<Brand admin={false} />
<span className="grow" />
{store && <span className="muted" style={{ fontSize: 14 }}>{store.name}</span>}
{store && <span className="m-avatar" aria-hidden="true">{store.name.charAt(0).toUpperCase()}</span>}
<button type="button" className="btn secondary small" onClick={logout}>Se déconnecter</button>
</header>
<main className="container" style={{ padding: 24, display: 'flex', flexDirection: 'column', gap: 24 }}>
{error && <div className="alert error" role="alert">{error}</div>}
{!store && !error && <p className="muted">Chargement…</p>}
{store && (
<>
<section className="m-hero">
<div>
<h1>{store.name}</h1>
<p>
{store.subscription ? `Formule ${PLAN_LABEL[store.subscription.plan]}` : 'Aucune formule'} ·{' '}
{open ? 'programme ouvert à vos clients' : store.status === 'SUSPENDED' ? 'programme suspendu' : 'programme pas encore ouvert'}
</p>
</div>
{store.subscribeUrl && <a className="btn" href={store.subscribeUrl}>Activer mon abonnement</a>}
</section>
{store.status === 'SUSPENDED' && <div className="alert warn">Programme suspendu. Il rouvre automatiquement dès le paiement de l’abonnement régularisé.</div>}
{/* Étapes d'ouverture */}
<ol className="m-steps" aria-label="Étapes d’ouverture">
{steps.map(([title, sub, done], i) => (
<li key={title} className={`m-step${done ? ' done' : ''}${i === current ? ' current' : ''}`} aria-current={i === current ? 'step' : undefined}>
<span className="dot">{done ? '✓' : i + 1}</span>
<div>
<strong>{title}</strong>
<span>{sub}</span>
</div>
</li>
))}
</ol>
{/* Raccourcis */}
<div className="m-grid">
<section className="card m-tile">
<span className="ico" aria-hidden="true">🔗</span>
<h2>Lien d’inscription</h2>
<p className="muted" style={{ margin: 0, fontSize: 14 }}>Vos clients s’inscrivent ici (c’est le QR code de l’affichette).</p>
<div className="m-copy">
<code>{store.enrollUrl}</code>
<button type="button" className="btn secondary small" onClick={() => copy(store.enrollUrl)}>{copied ? 'Copié ✓' : 'Copier'}</button>
</div>
</section>
<section className="card m-tile">
<span className="ico" aria-hidden="true">🖨️</span>
<h2>Affichette du comptoir</h2>
<p className="muted" style={{ margin: 0, fontSize: 14 }}>À imprimer en A5 ou A6 et à poser près de la caisse.</p>
<div>
{hasCard ? (
<Link className="btn secondary" to="/espace/affichette">Imprimer l’affichette</Link>
) : (
<span className="muted" style={{ fontSize: 14 }}>Disponible une fois la carte conçue.</span>
)}
</div>
</section>
<section className="card m-tile">
<span className="ico" aria-hidden="true">📱</span>
<h2>App MonEspacePro</h2>
<p className="muted" style={{ margin: 0, fontSize: 14 }}>
Scan des cartes, employés, modules (campagnes, anniversaire, relances…) et statistiques : avec les mêmes identifiants.
</p>
</section>
</div>
<section style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
<h2>Ma carte de fidélité</h2>
<CardWizard
store={store}
onSaved={reload}
base="/api/staff/card-design"
posterHref="/espace/affichette"
draftNote={
<p style={{ margin: 0 }}>
{store.subscribeUrl
? 'Votre programme s’ouvrira automatiquement dès votre abonnement activé.'
: 'Enregistrez la carte : votre programme s’ouvre automatiquement à vos clients.'}
</p>
}
/>
</section>
</>
)}
</main>
</div>
);
}