App : page Statistiques retravaillée
Contrôle segmenté de période, bloc « Passages » mis en avant (évolution, graphique, créneau le plus fréquenté), tuiles à pictogrammes, podium des meilleurs clients, exports présentés en lignes d'action. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
98fd599f75
commit
935f3f9354
1 changed files with 97 additions and 47 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import { useCallback, useState } from 'react';
|
||||
import { Alert, Pressable, ScrollView, View } from 'react-native';
|
||||
import { ActivityIndicator, Alert, Pressable, ScrollView, View } from 'react-native';
|
||||
import { useFocusEffect } from 'expo-router';
|
||||
import { api, ApiError } from '../src/api';
|
||||
import { describe } from '../src/errors';
|
||||
import { Banner, Button, Card, T } from '../src/ui';
|
||||
import { Banner, Card, T } from '../src/ui';
|
||||
import { font, useColors } from '../src/theme';
|
||||
|
||||
interface Stats {
|
||||
|
|
@ -26,7 +26,7 @@ interface Stats {
|
|||
top: { firstName: string; visits: number; amountCents: number }[];
|
||||
}
|
||||
|
||||
const PERIODS: [number, string][] = [[30, '30 j'], [90, '3 mois'], [365, '12 mois']];
|
||||
const PERIODS: [number, string][] = [[30, '30 jours'], [90, '3 mois'], [365, '12 mois']];
|
||||
const euros = (c: number) => (c / 100).toLocaleString('fr-FR', { style: 'currency', currency: 'EUR', maximumFractionDigits: 0 });
|
||||
const pctText = (n: number | null) => (n === null ? '' : `${n >= 0 ? '▲' : '▼'} ${Math.abs(n)} %`);
|
||||
|
||||
|
|
@ -77,74 +77,105 @@ export default function StatsScreen() {
|
|||
|
||||
const k = data?.kpis;
|
||||
const max = Math.max(1, ...(data?.series.map((s) => s.visits) ?? [1]));
|
||||
const tiles: [string, string, string?][] = k
|
||||
// Le passage est mis en avant ; les autres indicateurs en tuiles.
|
||||
const tiles: [string, string, string, string?][] = k
|
||||
? [
|
||||
['Passages', String(k.visits), pctText(k.visitsChange)],
|
||||
['Nouvelles cartes', String(k.newCards), `${k.cardsTotal} au total`],
|
||||
['Clients actifs', String(k.activeCustomers), `${k.visitsPerCustomer} passage(s)/client`],
|
||||
['Clients fidèles', k.returningRate === null ? '—' : `${k.returningRate} %`, 'venus 2 fois ou plus'],
|
||||
['🆕', 'Nouvelles cartes', String(k.newCards), `${k.cardsTotal} au total`],
|
||||
['👥', 'Clients actifs', String(k.activeCustomers), `${k.visitsPerCustomer} passage(s) / client`],
|
||||
['🔁', 'Clients fidèles', k.returningRate === null ? '—' : `${k.returningRate} %`, 'venus 2 fois ou plus'],
|
||||
['🎁', 'Récompenses', String(k.rewardsRedeemed), 'utilisées'],
|
||||
...(data!.pointsMode === 'AMOUNT'
|
||||
? ([['Achats', euros(k.revenueCents), k.averageBasketCents === null ? '' : `panier ${euros(k.averageBasketCents)}`]] as [string, string, string][])
|
||||
? ([
|
||||
['💶', 'Achats', euros(k.revenueCents), k.averageBasketCents === null ? '' : `panier moyen ${euros(k.averageBasketCents)}`],
|
||||
] as [string, string, string, string][])
|
||||
: []),
|
||||
['Récompenses', String(k.rewardsRedeemed)],
|
||||
]
|
||||
: [];
|
||||
const trend = k?.visitsChange ?? null;
|
||||
const MEDAL = ['🥇', '🥈', '🥉'];
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
contentContainerStyle={{ padding: 20, gap: 16 }}
|
||||
bounces={false}
|
||||
overScrollMode="never"
|
||||
>
|
||||
<View style={{ flexDirection: 'row', gap: 8 }}>
|
||||
<ScrollView contentContainerStyle={{ padding: 20, gap: 18 }} bounces={false} overScrollMode="never">
|
||||
{/* Période : contrôle segmenté */}
|
||||
<View accessibilityRole="tablist" style={{ flexDirection: 'row', backgroundColor: c.surface, borderRadius: 14, padding: 4, borderWidth: 1, borderColor: c.border }}>
|
||||
{PERIODS.map(([d, label]) => (
|
||||
<Pressable
|
||||
key={d}
|
||||
accessibilityRole="button"
|
||||
accessibilityRole="tab"
|
||||
accessibilityState={{ selected: days === d }}
|
||||
onPress={() => setDays(d)}
|
||||
style={{ paddingVertical: 10, paddingHorizontal: 16, borderRadius: 999, borderWidth: 1, borderColor: days === d ? c.primary : c.border, backgroundColor: days === d ? c.primarySoft : c.surface }}
|
||||
style={{ flex: 1, paddingVertical: 10, borderRadius: 10, alignItems: 'center', backgroundColor: days === d ? c.primary : 'transparent' }}
|
||||
>
|
||||
<T style={{ fontFamily: font.semibold, color: days === d ? c.primary : c.text }}>{label}</T>
|
||||
<T style={{ fontFamily: font.bold, fontSize: 15, color: days === d ? c.onPrimary : c.text }}>{label}</T>
|
||||
</Pressable>
|
||||
))}
|
||||
</View>
|
||||
{error && <Banner tone="error">{error}</Banner>}
|
||||
{!data ? (
|
||||
{!data || !k ? (
|
||||
!error && <T v="muted">Chargement…</T>
|
||||
) : (
|
||||
<>
|
||||
{/* Passages : chiffre clé, évolution et graphique */}
|
||||
<View style={{ backgroundColor: c.primary, borderRadius: 22, padding: 20, gap: 14 }}>
|
||||
<View style={{ flexDirection: 'row', alignItems: 'flex-start', justifyContent: 'space-between' }}>
|
||||
<View>
|
||||
<T style={{ color: c.onPrimary, opacity: 0.85, fontFamily: font.semibold, fontSize: 14 }}>Passages en magasin</T>
|
||||
<T v="big" style={{ color: c.onPrimary }}>{k.visits}</T>
|
||||
</View>
|
||||
{trend !== null && (
|
||||
<View style={{ paddingHorizontal: 10, paddingVertical: 4, borderRadius: 999, backgroundColor: 'rgba(255,255,255,0.18)' }}>
|
||||
<T style={{ color: c.onPrimary, fontFamily: font.bold, fontSize: 14 }}>{pctText(trend)}</T>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
<View style={{ flexDirection: 'row', alignItems: 'flex-end', height: 90, gap: 2 }} accessibilityLabel="Graphique des passages sur la période">
|
||||
{data.series.map((s, i) => (
|
||||
<View
|
||||
key={i}
|
||||
style={{
|
||||
flex: 1,
|
||||
height: `${(s.visits / max) * 100}%`,
|
||||
minHeight: s.visits ? 3 : 1,
|
||||
backgroundColor: i === data.series.length - 1 ? c.accent : 'rgba(255,255,255,0.55)',
|
||||
borderRadius: 2,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</View>
|
||||
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
|
||||
<T style={{ color: c.onPrimary, opacity: 0.8, fontSize: 12 }}>{data.series[0]?.label}</T>
|
||||
<T style={{ color: c.onPrimary, opacity: 0.8, fontSize: 12 }}>{data.series[data.series.length - 1]?.label}</T>
|
||||
</View>
|
||||
{data.busiest && (
|
||||
<T style={{ color: c.onPrimary, fontSize: 14 }}>
|
||||
⏱ Créneau le plus fréquenté : <T style={{ color: c.onPrimary, fontFamily: font.bold, fontSize: 14 }}>{data.busiest}</T>
|
||||
</T>
|
||||
)}
|
||||
</View>
|
||||
|
||||
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 12 }}>
|
||||
{tiles.map(([label, value, hint]) => (
|
||||
<Card key={label} style={{ width: '47%', flexGrow: 1, padding: 14, gap: 2 }}>
|
||||
<T v="muted" style={{ fontSize: 13 }}>{label}</T>
|
||||
<T style={{ fontFamily: font.extrabold, fontSize: 24 }}>{value}</T>
|
||||
{tiles.map(([icon, label, value, hint]) => (
|
||||
<Card key={label} style={{ width: '47%', flexGrow: 1, padding: 14, gap: 4 }}>
|
||||
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 6 }}>
|
||||
<T style={{ fontSize: 16 }}>{icon}</T>
|
||||
<T v="muted" style={{ fontSize: 13 }} numberOfLines={1}>{label}</T>
|
||||
</View>
|
||||
<T style={{ fontFamily: font.extrabold, fontSize: 26 }}>{value}</T>
|
||||
{!!hint && <T v="muted" style={{ fontSize: 12 }}>{hint}</T>}
|
||||
</Card>
|
||||
))}
|
||||
</View>
|
||||
|
||||
<Card style={{ padding: 16, gap: 10 }}>
|
||||
<T v="label">Passages</T>
|
||||
<View style={{ flexDirection: 'row', alignItems: 'flex-end', height: 120, gap: 2 }} accessibilityLabel="Graphique des passages sur la période">
|
||||
{data.series.map((s, i) => (
|
||||
<View key={i} style={{ flex: 1, height: `${(s.visits / max) * 100}%`, minHeight: s.visits ? 3 : 0, backgroundColor: c.primary, borderRadius: 2 }} />
|
||||
))}
|
||||
</View>
|
||||
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
|
||||
<T v="muted" style={{ fontSize: 12 }}>{data.series[0]?.label}</T>
|
||||
<T v="muted" style={{ fontSize: 12 }}>{data.series[data.series.length - 1]?.label}</T>
|
||||
</View>
|
||||
{data.busiest && <T v="muted">Créneau le plus fréquenté : {data.busiest}</T>}
|
||||
</Card>
|
||||
|
||||
{data.top.length > 0 && (
|
||||
<Card style={{ padding: 16, gap: 8 }}>
|
||||
<T v="label">Meilleurs clients</T>
|
||||
<Card style={{ padding: 16, gap: 12 }}>
|
||||
<T v="h2">Meilleurs clients</T>
|
||||
{data.top.slice(0, 5).map((t, i) => (
|
||||
<View key={i} style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
|
||||
<T>{i + 1}. {t.firstName}</T>
|
||||
<T v="muted">
|
||||
<View key={i} style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
|
||||
<View style={{ width: 36, height: 36, borderRadius: 18, backgroundColor: i < 3 ? c.warnBg : c.surface2, alignItems: 'center', justifyContent: 'center' }}>
|
||||
<T style={{ fontSize: i < 3 ? 18 : 14, fontFamily: font.bold, color: c.text3 }}>{MEDAL[i] ?? i + 1}</T>
|
||||
</View>
|
||||
<T v="label" style={{ flex: 1 }} numberOfLines={1}>{t.firstName}</T>
|
||||
<T v="muted" style={{ fontSize: 14 }}>
|
||||
{t.visits} passage{t.visits > 1 ? 's' : ''}
|
||||
{data.pointsMode === 'AMOUNT' ? ` · ${euros(t.amountCents)}` : ''}
|
||||
</T>
|
||||
|
|
@ -153,10 +184,29 @@ export default function StatsScreen() {
|
|||
</Card>
|
||||
)}
|
||||
|
||||
<T v="h2">Exports</T>
|
||||
<T v="muted">Le fichier CSV (ouvrable avec Excel) est envoyé uniquement par e-mail au dirigeant de la société.</T>
|
||||
<Button label="Recevoir le fichier clients" variant="secondary" loading={sending === 'customers'} disabled={!!sending} onPress={() => exportCsv('customers')} />
|
||||
<Button label="Recevoir les opérations de la période" variant="secondary" loading={sending === 'transactions'} disabled={!!sending} onPress={() => exportCsv('transactions')} />
|
||||
<Card style={{ padding: 16, gap: 12 }}>
|
||||
<T v="h2">Exports</T>
|
||||
<T v="muted" style={{ fontSize: 14 }}>Fichier CSV (ouvrable avec Excel), envoyé uniquement par e-mail au dirigeant de la société.</T>
|
||||
{(
|
||||
[
|
||||
['customers', '📇', 'Fichier clients', 'Tous les clients et leur solde'],
|
||||
['transactions', '🧾', 'Opérations de la période', `Les ${PERIODS.find(([d]) => d === days)?.[1]} sélectionnés`],
|
||||
] as const
|
||||
).map(([kind, icon, title, sub]) => (
|
||||
<Pressable key={kind} accessibilityRole="button" disabled={!!sending} onPress={() => exportCsv(kind)}>
|
||||
{({ pressed }) => (
|
||||
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 12, padding: 12, borderRadius: 14, backgroundColor: pressed ? c.primarySoft : c.surface2 }}>
|
||||
<T style={{ fontSize: 22 }}>{icon}</T>
|
||||
<View style={{ flex: 1 }}>
|
||||
<T v="label">{title}</T>
|
||||
<T v="muted" style={{ fontSize: 13 }}>{sub}</T>
|
||||
</View>
|
||||
{sending === kind ? <ActivityIndicator /> : <T style={{ color: c.primary, fontFamily: font.bold }}>Recevoir</T>}
|
||||
</View>
|
||||
)}
|
||||
</Pressable>
|
||||
))}
|
||||
</Card>
|
||||
</>
|
||||
)}
|
||||
</ScrollView>
|
||||
|
|
|
|||
Loading…
Reference in a new issue