MonEspacePro/pro/app/client/[serial]/index.tsx
Tony AVERTY 226f3cedb8 Espace web du Dirigeant, inscription en ligne, employés et cadeaux dans l'app Pro
- Admin : /inscription (formule, magasin, mot de passe, paiement Stripe) et
  /espace (connexion Dirigeant, état de l'abonnement, conception de la carte)
- Admin : formule obligatoire à la création d'un magasin, plus d'essai gratuit,
  lien d'abonnement permanent, titre des campagnes
- App Pro : écran Employés (création avec rôle Employé / Dirigeant), Mon compte
  (mot de passe), titre des campagnes, remise des cadeaux en caisse, accueil
  avant la conception de la carte
- Site : boutons « Choisir » vers l'inscription, avec la périodicité choisie

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

111 lines
4.7 KiB
TypeScript

import { useState } from 'react';
import { ActivityIndicator, Alert, View } from 'react-native';
import { router } from 'expo-router';
import { useStore } from '../../../src/session';
import { useCard } from '../../../src/useCard';
import { api, CardInfo } from '../../../src/api';
import { describe, ErrorScreen } from '../../../src/errors';
import { Banner, Button, Card, Screen, T, unit } from '../../../src/ui';
import { font, useColors } from '../../../src/theme';
function ago(iso: string) {
const min = Math.round((Date.now() - new Date(iso).getTime()) / 60000);
if (min < 1) return "à l'instant";
if (min < 60) return `il y a ${min} min`;
const h = Math.round(min / 60);
if (h < 24) return `il y a ${h} h`;
return `le ${new Date(iso).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long' })}`;
}
/** Fiche client après scan (maquette 4). */
export default function ClientCard() {
const c = useColors();
const store = useStore();
const { serial, card, setCard, error, reload } = useCard();
const [giving, setGiving] = useState<string | null>(null);
if (error) return <ErrorScreen error={error} onRetry={reload} />;
if (!card) return <Screen style={{ justifyContent: 'center' }}><ActivityIndicator /></Screen>;
const mode = store.design.pointsMode;
const cheapest = store.design.rewards[0];
const reachable = store.design.rewards.filter((r) => r.cost <= card.balance).length;
const SOURCE = { BIRTHDAY: 'Anniversaire', REFERRAL: 'Parrainage', REFERRAL_FRIEND: 'Bienvenue (parrainage)' } as const;
const give = (g: CardInfo['gifts'][number]) =>
Alert.alert('Remettre le cadeau ?', `« ${g.label} » à ${card.firstName}`, [
{ text: 'Annuler', style: 'cancel' },
{
text: 'Remis',
onPress: async () => {
setGiving(g.id);
try {
setCard(await api.post<CardInfo>(`/api/staff/cards/${encodeURIComponent(serial)}/gifts/${g.id}/use`));
} catch (e) {
Alert.alert('Impossible', describe(e).detail);
} finally {
setGiving(null);
}
},
},
]);
return (
<Screen scroll>
<View style={{ gap: 4 }}>
<T v="h1">{card.firstName}</T>
<T v="muted">
{card.lastCreditAt ? `Dernière visite ${ago(card.lastCreditAt)}` : 'Première visite'} · client depuis le{' '}
{new Date(card.memberSince).toLocaleDateString('fr-FR', { day: 'numeric', month: 'short', year: 'numeric' })}
</T>
</View>
<Card style={{ alignItems: 'center', paddingVertical: 28, backgroundColor: store.design.backgroundColor, borderColor: store.design.backgroundColor }}>
<T style={{ color: store.design.foregroundColor, fontFamily: font.semibold, fontSize: 14, letterSpacing: 1 }}>SOLDE</T>
<T v="big" style={{ color: store.design.foregroundColor }}>{card.balance}</T>
<T style={{ color: store.design.foregroundColor, fontSize: 16 }}>{unit(mode, card.balance)}</T>
</Card>
{reachable > 0 ? (
<Banner tone="ok">{reachable === 1 ? '1 récompense disponible' : `${reachable} récompenses disponibles`}</Banner>
) : cheapest ? (
<T v="muted" style={{ textAlign: 'center' }}>
Encore {cheapest.cost - card.balance} {unit(mode, cheapest.cost - card.balance)} avant « {cheapest.label} »
</T>
) : null}
{card.gifts?.map((g) => (
<Card key={g.id} style={{ padding: 14, gap: 10, borderColor: c.ok }}>
<View style={{ gap: 2 }}>
<T v="label">🎁 {g.label}</T>
<T v="muted" style={{ fontSize: 13 }}>
{SOURCE[g.source]} · {new Date(g.createdAt).toLocaleDateString('fr-FR', { day: 'numeric', month: 'short' })}
</T>
</View>
<Button label="Marquer comme remis" variant="secondary" loading={giving === g.id} onPress={() => give(g)} />
</Card>
))}
{card.nextCreditAt && (
<Banner tone="warn">
Déjà créditée récemment : nouveau crédit possible à{' '}
{new Date(card.nextCreditAt).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })}.
</Banner>
)}
<View style={{ flex: 1 }} />
<Button
large
label={mode === 'STAMP' ? 'Ajouter un tampon' : 'Créditer'}
onPress={() => router.push({ pathname: '/client/[serial]/credit', params: { serial } })}
/>
<Button
label="Utiliser une récompense"
variant="secondary"
disabled={store.design.rewards.length === 0}
onPress={() => router.push({ pathname: '/client/[serial]/rewards', params: { serial } })}
/>
<T v="muted" style={{ textAlign: 'center', color: c.text3, fontSize: 13 }}>Carte {serial}</T>
</Screen>
);
}