import { useCallback, useEffect, useState } from 'react'; import { ActivityIndicator, FlatList, Pressable, View } from 'react-native'; import { api, Operation } from '../src/api'; import { useSession, useStore } from '../src/session'; import { Banner, T, euros, unit } from '../src/ui'; import { font, useColors } from '../src/theme'; type Period = 'today' | 'week'; const REASON: Record = { AMOUNT_OVER_LIMIT: 'Refusé : plafond dépassé', TOO_SOON: 'Refusé : délai entre deux crédits', INSUFFICIENT_BALANCE: 'Refusé : solde insuffisant', }; /** Historique (maquettes 9a/9b) : le dirigeant voit tout, l'employé ses opérations. */ export default function History() { const c = useColors(); const { me } = useSession(); const store = useStore(); const isManager = me?.role === 'MANAGER'; const [period, setPeriod] = useState('today'); const [items, setItems] = useState([]); const [cursor, setCursor] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const load = useCallback( async (reset: boolean) => { setLoading(true); setError(null); try { const q = new URLSearchParams({ period }); if (!reset && cursor) q.set('cursor', cursor); const r = await api.get<{ items: Operation[]; nextCursor: string | null }>(`/api/staff/transactions?${q}`); setItems((prev) => (reset ? r.items : [...prev, ...r.items])); setCursor(r.nextCursor); } catch (e) { setError((e as Error).message); } finally { setLoading(false); } }, [period, cursor], ); useEffect(() => { load(true); // eslint-disable-next-line react-hooks/exhaustive-deps }, [period]); const mode = store.design.pointsMode; return ( {(['today', 'week'] as const).map((p) => ( setPeriod(p)} style={{ height: 44, paddingHorizontal: 18, borderRadius: 22, justifyContent: 'center', backgroundColor: period === p ? c.primary : c.surface, borderWidth: 1, borderColor: period === p ? c.primary : c.border, }} > {p === 'today' ? "Aujourd'hui" : '7 jours'} ))} {isManager ? 'Toutes les opérations du magasin' : 'Vos opérations'} {error && {error}} o.id} contentContainerStyle={{ paddingHorizontal: 16, paddingBottom: 32 }} onEndReached={() => cursor && !loading && load(false)} onEndReachedThreshold={0.5} refreshing={loading && items.length === 0} onRefresh={() => load(true)} ListEmptyComponent={!loading ? Aucune opération sur la période. : null} ListFooterComponent={loading && items.length > 0 ? : null} ItemSeparatorComponent={() => } renderItem={({ item: o }) => { const rejected = o.type === 'REJECTED'; const time = new Date(o.createdAt).toLocaleString('fr-FR', { ...(period === 'week' && { weekday: 'short' }), hour: '2-digit', minute: '2-digit', }); const what = o.type === 'REDEEM' ? o.reward?.label ?? 'Récompense' : rejected ? REASON[o.rejectReason ?? ''] ?? 'Refusé' : o.amountCents ? euros(o.amountCents) : 'Passage'; return ( {time} {o.customerFirstName} {what} {isManager ? ` · ${o.staff.displayName}` : ''} 0 ? c.ok : c.text, }} > {rejected ? '—' : `${o.points > 0 ? '+' : '−'}${Math.abs(o.points)} ${unit(mode, Math.abs(o.points)).slice(0, 3)}.`} ); }} /> ); }